#!/bin/bash

# Retrieve arguments
domain=$1
path=$2
admin=$3
password=$4
language=$5
public_site=$6
port=$7

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

# Check port availability
sudo yunohost app checkport $port
if [[ ! $? -eq 0 ]]; then
exit 1
fi

# Check user availability
sudo yunohost user info $admin
if [[ ! $? -eq 0 ]]; then
exit 1
fi

# Check password not empty
if [[ -z "$password" ]]; then
printf "Error empty admin password, aborting"
exit 1
fi

sudo yunohost app setting movim admin -v $admin
sudo yunohost app setting movim public_site -v $public_site
sudo yunohost app setting movim port -v $port

# 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')

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

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

# Install packages
sudo apt-get install php5-gd php5-curl php5-imagick php5-cli -y

# Install Movim files
final_path=/var/www/movim
sudo mkdir -p $final_path
sudo cp -a ../sources/* $final_path

# Create movim system user and set permissions
sudo useradd -d /var/www/movim -s /bin/sh movim
sudo chown -R movim:www-data $final_path

# Install PHP dependencies
sudo su -c "curl -sS https://getcomposer.org/installer | php -- --install-dir=$final_path" movim
sudo su -c "cd $final_path && php composer.phar install" movim

# Movim configuration
sudo cp $final_path/config/db.example.inc.php $final_path/config/db.inc.php
sudo sed -i "s@'username'    => 'username'@'username'    => '$db_user'@g" $final_path/config/db.inc.php
sudo sed -i "s@'password'    => 'password'@'password'    => '$db_pwd'@g" $final_path/config/db.inc.php
sudo sed -i "s@/ws/@$path/ws/@g" $final_path/app/assets/js/movim_websocket.js
sudo su -c "cd $final_path && php mud.php db set" movim
sudo su -c "cd $final_path && php mud.php config locale:$language" movim
sudo su -c "cd $final_path && php mud.php config loglevel:1" movim
sudo su -c "cd $final_path && php mud.php config environment:production" movim
sudo su -c "cd $final_path && php mud.php config timezone:`cat /etc/timezone`" movim
sudo su -c "cd $final_path && php mud.php config username:$admin" movim
sudo su -c "cd $final_path && php mud.php config password:`echo -n $password | sha1sum | awk '{print $1}'`" movim
sudo yunohost app addaccess movim -u $admin

# Copy init script or systemd service
sudo sed -i "s@YHURL@$domain$path@g" ../conf/movim.{service,init}
sudo sed -i "s@YHDIR@$final_path@g" ../conf/movim.{service,init}
sudo sed -i "s@YHPORT@$port@g" ../conf/movim.{service,init}
initcheck=`pgrep -ox systemd`
if [ "$initcheck" = "1" ];
then
	sudo cp ../conf/movim.service /lib/systemd/system/
	sudo systemctl enable movim.service
	sudo systemctl start movim.service
else
	sudo cp ../conf/movim.init /etc/init.d/movim
	sudo chmod 755 /etc/init.d/movim
	sudo update-rc.d movim defaults
	sudo /etc/init.d/movim start
fi

# Nginx configuration
sed -i "s@PATHTOCHANGE@$path@g" ../conf/nginx.conf
sed -i "s@ALIASTOCHANGE@$final_path/@g" ../conf/nginx.conf
sed -i "s@YHPORT@$port@g" ../conf/nginx.conf
sudo cp ../conf/nginx.conf /etc/nginx/conf.d/$domain.d/movim.conf

# SSOwat Configuration
sudo yunohost app setting movim skipped_uris -v "/"  
if [ $public_site = "No" ];
then              
	sudo yunohost app setting movim protected_uris -v "/" 
else              
	sudo yunohost app setting movim protected_uris -v "/?q=admin"
fi
sudo yunohost app ssowatconf

# Start Movim
sudo service nginx reload
sudo service php5-fpm restart
