#! /bin/bash

# causes the shell to exit if any subcommand or pipeline returns a non-zero status
set -e

# retrieve arguments
domain=$1
path=$2
is_public=$3
default_lang=$4
filesize=$5
version=$6
admin=$7
password=$8

# definie useful vars
app=bozon
parent_path=/var/www
final_path=$parent_path/$app
data_path=/home/yunohost.app/$app

# remove trailing slash
[ "$path" != "/" ] && path=${path%/}

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

# check that admin user is an existing account
sudo yunohost user list --json | grep -q "\"username\": \"$admin\""
if [[ ! $? -eq 0 ]]; then
    echo "Error : the chosen admin user does not exist"
    exit 1
fi

# save app settings
sudo yunohost app setting $app admin_user -v "$admin"
sudo yunohost app setting $app is_public -v "$is_public"
sudo yunohost app setting $app domain -v "$domain"
sudo yunohost app setting $app path -v "$path"

# download bozon
if [ "$version" = "Latest" ];
then
    sudo wget https://github.com/broncowdd/BoZoN/archive/master.zip -O $parent_path/master.zip
else
    stable=$(curl https://raw.githubusercontent.com/ewilly/bozon_ynh/master/BoZoN-stable)
    sudo wget https://github.com/broncowdd/BoZoN/archive/$stable.zip -O $parent_path/master.zip
fi
sudo unzip $parent_path/master.zip -d $parent_path/
sudo rm $parent_path/master.zip
sudo mv $parent_path/BoZoN-* $parent_path/$app

# add required packages
sudo apt-get install php5-curl php5-gd

# copy files to final folder and set permissions
sudo find $final_path -type f -name ".htaccess" | xargs sudo rm
sudo chown -R root: $final_path
sudo find $final_path -type f | xargs sudo chmod 644
sudo find $final_path -type d | xargs sudo chmod 755

# configure config file
sudo sed -i "s@default_language='en';@default_language='$default_lang';@g" $final_path/config.php

# create data folders
sudo mkdir -p $final_path/private
sudo mkdir -p $data_path/uploads
sudo ln -s $data_path/uploads $final_path/uploads
sudo mkdir -p $data_path/thumbs
sudo ln -s $data_path/thumbs $final_path/thumbs
sudo chown -R www-data: $final_path/private
sudo chown -R www-data: $data_path/uploads
sudo chown -R www-data: $data_path/thumbs

# configure nginx settings
## file upload size limit
postsize=$(echo "${filesize%?}.1${filesize: -1}")
sudo sed -i "s@YNH_FILE_SIZE@$filesize@g" ../conf/nginx.conf
sudo sed -i "s@YNH_POST_SIZE@$postsize@g" ../conf/nginx.conf
## path
folder_path=${path%/}
sudo sed -i "s@YNH_EXAMPLE_PATH@$path@g" ../conf/nginx.conf
# if path is only / (without subfolder), add trailing slash to alias
alias_path=$final_path
[ "$path" == '/' ] && alias_path=$alias_path'/'
sudo sed -i "s@YNH_EXAMPLE_ALIAS@$alias_path@g" ../conf/nginx.conf
sudo sed -i "s@YNH_EXAMPLE_FOLDER@$folder_path@g" ../conf/nginx.conf

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

# create the superadmin
## set temporary public access
sudo yunohost app setting $app unprotected_uris -v "/"
## start app
sudo service nginx reload
sudo yunohost app ssowatconf
## fill the superadmin creation form
curl_path=$([ "$path" == "/" ] || echo $path)
curl -k -X POST \
  --data-urlencode creation="1" \
  --data-urlencode login="$admin" \
  --data-urlencode pass="$password" \
  --data-urlencode confirm="$password" \
  https://$domain$curl_path/index.php?p=admin > /dev/null 2>&1

# if app is private, remove url to SSOWat conf from skipped_uris
if [ "$is_public" = "No" ];
then
    # redirect to SSOwat login in
    sudo yunohost app setting $app unprotected_uris -d
    sudo yunohost app setting $app unprotected_regex -v "$domain$path/index.php%?f=.*$","$domain$path/index.php%?zipfolder=.*$","$domain$path/core/js/.*$","$domain$path/templates/.*$"
else
    sudo yunohost app setting $app unprotected_uris -v "/"
fi

# restart services
sudo service nginx reload
sudo yunohost app ssowatconf