#!/bin/bash

# Retrieve arguments
domain=$1
path=$2
is_public=$3

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

# Save specific settings
sudo yunohost app setting etherpadlite is_public -v $is_public

# Remove trailing "/" for next commands
path=${path%/}

# Generate random password
db_pwd=$(dd if=/dev/urandom bs=1 count=200 2> /dev/null | tr -c -d '[A-Za-z0-9]' | sed -n 's/\(.\{24\}\).*/\1/p')

# Generate random key
key=$(dd if=/dev/urandom bs=1 count=200 2> /dev/null | tr -c -d '[A-Za-z0-9]' | sed -n 's/\(.\{40\}\).*/\1/p')

# Use 'etherpadlite' as database name and user
db_user=etherpadlite

# Initialize database and store mysql password for upgrade
sudo yunohost app initdb $db_user -p $db_pwd
sudo yunohost app setting etherpadlite mysqlpwd -v $db_pwd

# Install dependances
sudo apt-get install nodejs-legacy npm -y

# Copy files to the right place
final_path=/var/www/etherpadlite
sudo mkdir -p $final_path
sudo cp -a ../sources/* $final_path
sudo cp ../conf/settings.json $final_path
sudo cp ../conf/etherpad-lite /etc/init.d/
sudo chmod +x /etc/init.d/etherpad-lite
sudo update-rc.d etherpad-lite defaults
sudo mkdir /var/log/etherpad-lite/
sudo touch /var/log/etherpad-lite/etherpad-lite.log
sudo chown www-data /var/log/etherpad-lite/etherpad-lite.log

sudo npm cache clear
sudo $final_path/bin/installDeps.sh > /dev/null 2>&1
sudo npm install forever -g > /dev/null 2>&1

# Change variables in Wordpress configuration
sudo sed -i "s/yunouser/$db_user/g" $final_path/settings.json
sudo sed -i "s/yunopass/$db_pwd/g" $final_path/settings.json
sudo sed -i "s/yunobase/$db_user/g" $final_path/settings.json
sudo sed -i "s/KEY/$key/g" $final_path/settings.json

# Set permissions to roundcube directory
sudo chown -R www-data: $final_path

# Modify Nginx configuration file and copy it to Nginx conf directory
sed -i "s@PATHTOCHANGE@$path@g" ../conf/nginx.conf*
sed -i "s@ALIASTOCHANGE@$final_path/@g" ../conf/nginx.conf*
if [ "$path" = "" ];
then
	sudo cp ../conf/nginx.conf-nosub /etc/nginx/conf.d/$domain.d/etherpadlite.conf
else
	sudo cp ../conf/nginx.conf /etc/nginx/conf.d/$domain.d/etherpadlite.conf
fi

# Reload Nginx and regenerate SSOwat conf
sudo service nginx reload
if [ "$is_public" = "Yes" ];
then
	sudo yunohost app setting etherpadlite skipped_uris -v "/"
fi
sudo yunohost app ssowatconf
sudo service etherpad-lite start