set -eux

# Retrieve arguments
app=$YNH_APP_INSTANCE_NAME
number=$YNH_APP_INSTANCE_NUMBER
domain=$YNH_APP_ARG_DOMAIN
path=$YNH_APP_ARG_PATH

# Check domain/path availability
sudo yunohost app checkurl $domain/$path -a $app

# Add config for nginx
sudo sed -i "s@PATHTOCHANGE@$path@g" ../conf/nginx.conf
sudo sed -i "s@FOLDER@$app/@g" ../conf/nginx.conf
sudo cp ../conf/nginx.conf /etc/nginx/conf.d/$domain.d/$app.conf

# Make directory for app web data
sudo mkdir /var/www/$app
sudo cp ../conf/index.html /var/www/$app

sudo yunohost app checkport 12345
if [[ ! $? -eq 0 ]]; then
   echo "Ogod noez the port 12345 aint available!!"
   exit 1
fi

# Create a dummy mysql db
db_user=$app
db_pwd="yoloswag42"
sudo yunohost app initdb "$db_user" -p $db_pwd
sudo yunohost app setting $app mysqlpwd -v $db_pwd

# Other custom stuff
sudo cp ../sources/importantfile /etc/

# Set the app as public
sudo yunohost app setting $app unprotected_uris -v "/"

# Reload Nginx and regenerate SSOwat conf
sudo service nginx reload
#sudo yunohost app ssowatconf
