set -eux

app=$YNH_APP_INSTANCE_NAME
number=$YNH_APP_INSTANCE_NUMBER

# Retrieve arguments
domain=$YNH_APP_ARG_DOMAIN
path=$YNH_APP_ARG_PATH

# Check domain/path availability
yunohost app checkurl $domain$path -a $app
if [[ ! $? -eq 0 ]]; then
    exit 1
fi

if [[ "$path" == "" ]]; then
  sed -i "s@PATHTOCHANGE@/@g" ../conf/nginx.conf
else
  sed -i "s@PATHTOCHANGE@$path@g" ../conf/nginx.conf
fi

sed -i "s@FOLDER@$app/@g" ../conf/nginx.conf

cp ../conf/nginx.conf /etc/nginx/conf.d/$domain.d/$app.conf

mkdir -p /var/www/$app

cp ../conf/index.html /var/www/$app

# Reload Nginx and regenerate SSOwat conf
service nginx reload

yunohost app setting $app unprotected_uris -v "/"
yunohost app ssowatconf
