#!/bin/bash
PROVIDER="MySql"

SMTP_HOST=""
SMTP_PORT=25
ENABLESSL="true"
SMTP_USER_NAME=""
SMTP_PASSWORD=""


while [ -n "$1" ]
do
        if [ -z "$2" ]  
        then
                echo "ERROR! no values specified after "$1" !" | tee -a $LOG_FILE
                echo "$USAGE"
                exit 1
        fi
        param="$2"

        case "$1" in
                --provider)
                        PROVIDER=$param
                        shift ;;
                --logfile)
                        LOGFILE=$param
                        shift ;;
                --domain_name)
                        DOMAIN_NAME=$param
                        shift ;;
                --database_name)
                        DATABASE_NAME=$param
                        shift ;;
                --use_mysql_root_password)
                        USE_MYSQL_ROOT_PASSWORD=$param
                        shift ;;
                --mysql_root_password)
                        MYSQL_ROOT_PASSWORD=$param
                        shift ;;
                --smtp_host)
                        SMTP_HOST=$param
                        shift ;;
                --smtp_port)
                        SMTP_PORT=$param
                        shift ;;
                --enablessl)
                        ENABLESSL=$param
                        shift ;;
                --smtp_user_name)
                        SMTP_USER_NAME=$param
                        shift ;;
                --smtp_password)
                        SMTP_PASSWORD=$param
                        shift ;;
                --) shift
                        break ;;
                        *) echo "ERROR! $1 is not an option"
                        echo "$USAGE"
                        exit 1
                esac
                shift
done



EMAIL=$(jq -n \
             --arg Host "$SMTP_HOST" \
             --argjson Port  $SMTP_PORT \
             --argjson EnableSSL $ENABLESSL \
             --arg UserName "$SMTP_USER_NAME" \
             --arg Password "$SMTP_PASSWORD" \
              '$ARGS.named'
)


QUERY="USE $DATABASE_NAME; DELETE FROM AppSettings WHERE id='email'; INSERT INTO AppSettings (\`Id\`, \`Value\`) VALUES ('email', '$EMAIL');"

#echo $QUERY

if [ "$USE_MYSQL_ROOT_PASSWORD" = "1" ] ; then
    mysql -u root -p"$MYSQL_ROOT_PASSWORD" --execute="$QUERY"
else
    mysql -u root --execute="$QUERY"
fi

