#!/bin/bash

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

# Check user parameter
sudo yunohost user list --json | grep -qi "\"username\": \"$user\""
if [[ ! $? -eq 0 ]]; then
    echo "Wrong user"
    exit 1
fi
sudo yunohost app setting piwigo admin_user -v $user

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

# 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 'piwigo' as database name and user
db_user=piwigo

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


# Modify Nginx configuration file and copy it to Nginx conf directory

final_path=/var/www/piwigo

sed -i "s@PATHTOCHANGE@$path@g" ../conf/nginx.conf
sed -i "s@ALIASTOCHANGE@$final_path/@g" ../conf/nginx.conf
sudo cp ../conf/nginx.conf /etc/nginx/conf.d/$domain.d/piwigo.conf

# Copy files to the right place


datapath=/home/yunohost.app/piwigo

sudo mkdir -p $final_path
sudo mkdir -p $datapath
sudo mkdir -p $datapath/galleries
sudo mkdir -p $datapath/upload
sudo cp -a ../sources/* $final_path
sudo ln -sd $datapath/galleries $final_path/galleries
sudo ln -sd $datapath/upload $final_path/upload
sudo cp ../conf/index.php $final_path/galleries/index.php

sudo cp -R ../plugins/Ldap_Login $final_path/plugins/Ldap_Login


# set permission

sudo chown -R www-data:www-data $final_path
sudo chown -R www-data:www-data $datapath
sudo chmod 777 $final_path/_data
sudo chmod 777 $final_path/upload
sudo chmod 755 -R $final_path/galleries


# Reload Nginx and regenerate SSOwat conf

sudo service nginx reload
sudo yunohost app setting piwigo skipped_uris -v "/"
sudo yunohost app ssowatconf

# Generate random password for admin

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

#configure piwigo via curl
echo "127.0.0.1 $domain #yunopiwigo" | sudo tee -a /etc/hosts
sleep 2
curl -kL -X POST https://$domain$path/install.php?language=fr_FR --data "install=true&dbuser=$db_user&dbpasswd=$db_pwd&dbname=$db_user&admin_name=$user&admin_pass1=$adm_pwd&admin_pass2=$adm_pwd&admin_mail=webmaster@$domain" > /home/admin/test

#change variable in local/config/database.inc.php

sudo sed -i "s@DBTOCHANGE@$db_user@g" ../conf/database.inc.php
sudo sed -i "s@USERTOCHANGE@$db_user@g" ../conf/database.inc.php
sudo sed -i "s@PASSTOCHANGE@$db_pwd@g" ../conf/database.inc.php

sudo cp ../conf/database.inc.php $final_path/local/config/database.inc.php

#activate ldap plugin

mysql -u $db_user -p$db_pwd $db_user -e "INSERT INTO plugins (id,state,version) VALUES ('Ldap_Login','active','1.1');"

#protect URIs

if [ $is_public = "No" ];
then
	sudo cp ../conf/config.inc.php $final_path/local/config/
        sudo yunohost app setting piwigo protected_uris -v "/"
        sudo yunohost app ssowatconf
fi

# Remove temporary entry in /etc/hosts
sudo sed '/yunopiwigo/d' /etc/hosts > /tmp/hosts.tmp
sudo cp /tmp/hosts.tmp /etc/hosts ; sudo rm -f /tmp/hosts.tmp
