SCRIPTPATH="$( cd "$(dirname "$0")" ; pwd -P )"

if [ "$(command -v sqlite3)" == "" ]
then
    echo "SQLite3 is not installed. Install it and try again."
    exit -1
fi

if [ "$(command -v php)" == "" ]
then
    echo "PHP is not installed. Install it and try again."
    exit -2
fi

if [ "$(php -m | grep sqlite3)" == "" ]
then
    echo "PHP module sqlite3 is not installed. Install it and try again."
    exit -3
fi

if [ "$(command -v node)" == "" ]
then
    echo "NODE.js is not installed. Install it and try again."
    exit -4
fi

if ! node -r requireg /dev/null 2> /dev/null
then
    echo "NODE.js module requireg is not installed. Install it and try again."
    exit -5
fi


if ! node -r dateformat /dev/null 2> /dev/null
then
    echo "NODE.js module dateformat is not installed. Install it and try again."
    exit -5
fi

if ! node -r request /dev/null 2> /dev/null
then
    echo "NODE.js module request is not installed. Install it and try again."
    exit -6
fi

if ! node -r querystring /dev/null 2> /dev/null
then
    echo "NODE.js module querystring is not installed. Install it and try again."
    exit -7
fi

if ! node -r http /dev/null 2> /dev/null
then
    echo "NODE.js module http is not installed. Install it and try again."
    exit -8
fi

if ! node -r fs /dev/null 2> /dev/null
then
    echo "NODE.js module fs is not installed. Install it and try again."
    exit -9
fi


if [ -f $SCRIPTPATH/application/sw3.sqlite ]
then
    echo "Database exists! Not overwritting. Use the --clean argument to force."
    exit -10
fi

echo "Configuring database..."
sqlite3 $SCRIPTPATH/application/sw.sqlite < $SCRIPTPATH/install_data/setup.sql
echo "Done. You can start the web server by typing ./start_server or edit the config file for the server options"
