#!/bin/bash

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

# 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 owncloud admin_user -v $user

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

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

# Install dependencies
sudo apt-get install acl smbclient -y -qq

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

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

# Create owncloud user
sudo useradd -d /var/www/owncloud owncloud

# Copy files to the right place
final_path=/var/www/owncloud
data_path=/home/yunohost.app/owncloud/data
sudo mkdir -p $final_path
sudo mkdir -p $data_path
sudo cp -a ../sources/* $final_path
sudo cp ../conf/nginx.conf /etc/nginx/conf.d/$domain.d/owncloud.conf
sudo cp ../conf/php-fpm.conf /etc/php5/fpm/pool.d/owncloud.conf
sudo cp ../conf/mount.json $data_path
sudo chown -hR owncloud:www-data $final_path
sudo chown -hR owncloud:www-data $data_path
sudo chown owncloud:www-data /home/yunohost.app/owncloud
sudo chmod 755 /home/yunohost.app
sudo chmod 664 -R $final_path
sudo find $final_path -type d -print0 | xargs -0 sudo chmod 775
sudo chmod -R 770 $data_path

# Change variables in Owncloud configuration
sudo sed -i "s@PATHTOCHANGE@$path@g" /etc/nginx/conf.d/$domain.d/owncloud.conf
sudo sed -i "s@ALIASTOCHANGE@$final_path/@g" /etc/nginx/conf.d/$domain.d/owncloud.conf
sudo sed -i "s@NAMETOCHANGE@owncloud@g" /etc/nginx/conf.d/$domain.d/owncloud.conf
sudo sed -i "s@NAMETOCHANGE@owncloud@g" /etc/php5/fpm/pool.d/owncloud.conf

# Set permissions to owncloud directories and /home directories + add Home external storage
for i in $(ls /home)
do
    sudo yunohost user list --json | grep -q "\"username\": \"$i\""
    if [[ $? -eq 0 ]];
    then
        sudo setfacl -m g:owncloud:rwx /home/$i
        sudo mkdir $data_path/$i
    fi
done

# Reload Nginx and regenerate SSOwat conf
sudo service php5-fpm restart
sudo service nginx reload
sudo yunohost app setting owncloud unprotected_uris -v "/"
sudo yunohost app ssowatconf

# Owncloud installation via curl
curl -kL -H "Host: $domain" -X POST https://127.0.0.1/$path/index.php --data "install=true&adminlogin=admin&adminpass=$db_pwd&directory=/home/yunohost.app/owncloud/data&dbtype=mysql&dbuser=$db_user&dbpass=$db_pwd&dbname=$db_user&dbhost=localhost" > /dev/null 2>&1

# Enable plugins
sleep 5
curl -kL -H "Host: $domain" -X POST https://127.0.0.1/$path/index.php/settings/ajax/enableapp.php --data "appid=files_external" -u "admin:$db_pwd" > /dev/null 2>&1
curl -kL -H "Host: $domain" -X POST https://127.0.0.1/$path/index.php/settings/ajax/enableapp.php --data "appid=user_ldap" -u "admin:$db_pwd" > /dev/null 2>&1

# Check if the Mysql database is initialized & running
mysql -u $db_user -p $db_pwd $db_user -e "select * from oc_appconfig;" > /dev/null 2>&1
result=$?
loop_number=1
while [ $result != 0 ] && [ $loop_number -lt 5 ];
do
    sleep 5
    mysql -u $db_user -p $db_pwd $db_user -e "select * from oc_appconfig;" > /dev/null 2>&1
    let result=$?
    if [ $loop_number -eq 4 ];
    then
        echo "Web installation failed"
        exit 1
    fi
    let loop_number++
done

# Configure LDAP plugin
mysql -u $db_user -p $db_pwd $db_user < ../conf/ldap_config.sql

sudo chown -hR owncloud:owncloud $final_path
sudo chown -hR owncloud:owncloud $data_path
sudo chmod 755 /home/yunohost.app
sudo find $final_path -type f -print0 | xargs -0 sudo chmod 664
sudo find $final_path -type d -print0 | xargs -0 sudo chmod 775
sudo chmod -R 770 $data_path

# Make an LDAP user as admin
mysql -u $db_user -p $db_pwd $db_user -e "INSERT INTO oc_group_user VALUES ('admin','$user');"

sudo yunohost app ssowatconf
