#!/bin/bash
echo "#######################################"
echo "# Checking PHP and Java requirements  #"
echo "#######################################"
echo ""

ERROR=0
echo "Checking PHP"
PHP=$(php -version)
if [ ! $? -eq 0 ]; then
    echo "  PHP (cli) is missing"
    ERROR=1
else
    echo "  [OK]"
fi

echo ""
echo "Checking JAVA"
JAVA=$(java)
if [ ! $? -eq 0 ]; then
    echo "  JAVA is missing"
    ERROR=1
else
    echo "  [OK]"
fi

if [ $ERROR -eq 1 ]; then
    echo "[ERROR] Requirements are not met. Please install required software"
    exit 1
fi

echo ""
echo "Checking JAVA_HOME"
NEW_JAVA=""
if [ -z $JAVA_HOME ]; then
    NEW_JAVA="N"
else 
    echo ""
    echo "  Is JAVA_HOME correct?"
    echo "  JAVA_HOME: "$JAVA_HOME
    read -r -p "  * [N]o, set a new one. [ANY other key] Yes: " NEW_JAVA
fi

if [ "$NEW_JAVA" = "N" ]; then
    read -r -p "  * New JAVA_HOME: " JAVA_HOME_UCOMMAND
else 
    JAVA_HOME_UCOMMAND=$JAVA_HOME
fi

echo ""
echo "#######################################"
echo "# Enter your UrbanCode Deploy details #"
echo "#######################################"
echo ""
    read -r -p "  * Server URL: " URL
    read -r -p "  * Port (ENTER for default 8443): " PORT
    read -r -p "  * Username: " USER
    read -r -s -p "  * Password: " PASSWORD

if [ -z "$PORT" ]; then
    PORT="8443"
fi

WEBURL="https://$URL:$PORT"

echo ""
echo ""
echo "#######################################"
echo "#  Download last version of udclient  #"
echo "#######################################"
echo ""
echo "  * Downloading udclient from $WEBURL/tools/udclient.zip"
curl -u "$USER:$PASSWORD" -k -s https://$URL:$PORT/tools/udclient.zip -o udclient.zip
echo "  * Extracting udclient.zip"
unzip -q udclient.zip

echo ""
echo "#######################################"
echo "#  Validating username and password   #"
echo "#######################################"
echo ""
./udclient/udclient -weburl $WEBURL -username $USER -password $PASSWORD login

if [ ! "$?" -eq 0 ]; then
    echo "[ERROR] Invalid username/password";
    exit 1
else
    echo "[OK]"
fi

echo ""
echo "#######################################"
echo "# Generate an authtoken for uCommand? #"
echo "#######################################"
echo ""
read -r -p "[N]o, I use an existing token, [H]elp, [ANY other key] to generate token: " GENERATE

if [ "$GENERATE" = "H" ]; then
    echo ""
    echo "[AUTHTOKEN]"
    echo "Authtoken is the most secure way to connect to UrbanCode Deploy via CLI. It is used in combination with it's related username."
    echo "To generate an authtoken, you can either user this tool or from you browser go to https://"$URL":"$PORT"/#security/tokens (you need to have administrator privileges)."
    read -r -p "[N]o, I use an existing token, [ANY other key] to generate token: " GENERATE
fi

if [ "$GENERATE" = "N" ]; then
    read -r -p "  * TOKEN: " TOKEN
fi

if [ ! "$GENERATE" = "N" ] && [ ! "$GENERATE" = "N" ] ; then
    echo "Enter the details for the authtoken: "
    read -r -p "  * Expiration date (YYYY-MM-DD): " DATE
    IP=$(ifconfig eth0 | grep 'inet addr:' | cut -d: -f2 | awk '{ print $1}')
    echo "Your IP: $IP"
    read -r -p "  * IP Host (blank to set your current IP, 0.0.0.0 to allow all): " HOST

    if [ -z "$HOST" ]; then
        HOST=$IP
    fi

    # Get user ID
    BASE=$(echo "$USER:$PASSWORD" | base64)
    OUT=$(udclient/udclient -username $USER -password $PASSWORD -weburl $WEBURL getUser -user $USER);
    ID=$(php INIT/jsonGet.php "id" "$OUT")

    # Get Token
    EXP=$(php INIT/strtotime.php "$DATE")
    JSON='{"description":"uCommand Setup Authtoken","expDate":"'$DATE'T00:00:00.000Z","expTime":"'$DATE'T00:00:00.000Z","host":"'$HOST'","expiration":"'$EXP'000","userId":"'$ID'"}'
    URL="https://"$URL":"$PORT"/security/authtoken"
    TOKEN_GET=$(curl -u "$USER:$PASSWORD" -H "Content-Type: application/json" -k -s -X PUT -d "$JSON" "$URL")

    TOKEN_ERROR=$(echo $TOKEN_GET | grep -i "Error")
    TOKEN_ERROR_DATE=$(echo $TOKEN_GET | grep -i "The expiration date must be after the current date")
    if [ ! -z "$TOKEN_ERROR" ] || [ ! -z "$TOKEN_ERROR_DATE" ] ; then
        echo ""
        echo "[ERROR] There was a problem while requesting a new authtoken."
        echo "        Please provide an existing authtoken to continue the setup. Otherwise, CTRL+C to quit"
        read -r -p "TOKEN: " TOKEN
    else 
        TOKEN=$(php INIT/jsonGet.php "token" "$TOKEN_GET")
        echo ""
        echo "YOUR TOKEN: "$TOKEN
    fi
fi

echo ""
echo "#######################################"
echo "#  Setting up uCommand configuration  #"
echo "#######################################"
echo ""
echo "  weburl: "$WEBURL
echo "  username: "$USER
echo "  authtoken: " $TOKEN
echo "  java_home: " $JAVA_HOME_UCOMMAND
echo ""
cd config
cat ucd.config.php | grep -v username | grep -v java_home | grep -v authtoken | grep -v weburl > ucd.config.temp.php
echo "\$config['username'] = '$USER';" >> ucd.config.temp.php
echo "\$config['java_home'] = '$JAVA_HOME_UCOMMAND';" >> ucd.config.temp.php
echo "\$config['authtoken'] = '$TOKEN';" >> ucd.config.temp.php
echo "\$config['weburl'] = '$WEBURL';" >> ucd.config.temp.php
mv -f ucd.config.temp.php ucd.config.php
PWD=$(pwd)
echo "Configuration saved in $PWD/ucd.config.php"
echo ""
echo ""
echo "[CONFIGURATION COMPLETED]"
