#!/bin/bash

# Retrieve arguments
domain=$1
path=$2

# Check domain/path availability
sudo yunohost app checkurl $domain$path -a agendav
if [[ ! $? -eq 0 ]]; then
    echo "Error : $domain$path is not available"
    exit 1
fi

baikal=0
radicale=0

sudo yunohost app list -f baikal --json | grep '"installed": true'  && baikal=1
sudo yunohost app list -f radicale --json | grep '"installed": true'  && radicale=1

if [ "$baikal" == "1" ] ; then
    echo "Detected Baikal"
    baikal=1
    caldavapp=baikal
elif [ "$radicale" == "1" ] ; then
    echo "Detected Radicale"
    radicale=1
    caldavapp=radicale
else
  echo "Error : You must install Baikal or Radicale before"
  exit 1
fi

# Install dependencies
sudo apt-get update
sudo apt-get install php5-cli -y

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

# Initialize database and store mysql password for upgrade
sudo yunohost app initdb $db_user -p $db_pwd -s $(readlink -e ../sources/sql/mysql.schema.sql)
sudo yunohost app setting agendav mysqlpwd -v $db_pwd

# Copy files to the right place
final_path=/var/www/agendav
sudo mkdir -p $final_path
sudo cp -r ../sources/* $final_path

sudo chown -R admin: $final_path
currentpath=$(pwd)
cd $final_path/web/application && ln -s ../config config && cd $currentpath

# caldav config
caldavdomain=$(sudo yunohost app setting $caldavapp domain)
caldavpath=$(sudo yunohost app setting $caldavapp path)
caldavpath=${caldavpath%/}
if [ $baikal -eq 1 ]; then
  caldav_principal_url="https://$caldavdomain$caldavpath/cal.php/%u/"
  caldav_calendar_url="https://$caldavdomain$caldavpath/cal.php/calendars/%s/"
elif [ $radicale -eq 1 ]; then
  caldav_principal_url="https://$caldavdomain$caldavpath/%u/"
  caldav_calendar_url="https://$caldavdomain$caldavpath/%s/"
fi
echo "----- $caldav_principal_url"
sed -i "s@YNH_CALDAV_PRINCIPAL_URL@$caldav_principal_url@g" ../conf/caldav.php
sed -i "s@YNH_CALDAV_CALENDAR_URL@$caldav_calendar_url@g" ../conf/caldav.php

# database config
sed -i "s/YNH_DB_USER/$db_user/g" ../conf/database.php
sed -i "s/YNH_DB_PWD/$db_pwd/g" ../conf/database.php
sed -i "s/YNH_DB_NAME/$db_user/g" ../conf/database.php

# main config
sed -i "s@YNH_DOMAIN@$domain@g" ../conf/config.php
sed -i "s@YNH_WWW_PATH@$path@g" ../conf/config.php
logdir=/var/log/agendav
sudo mkdir -p $logdir
sudo chown www-data: $logdir
sudo chmod 755 $logdir
sed -i "s@YNH_LOG_PATH@$logdir@g" ../conf/config.php
encryptkey=$(dd if=/dev/urandom bs=1 count=200 2> /dev/null | tr -c -d 'A-Za-z0-9' | sed -n 's/\(.\{24\}\).*/\1/p')
sudo yunohost app setting agendav encryptkey -v $encryptkey
sed -i "s/YNH_ENCRYPT_KEY/$encryptkey/g" ../conf/config.php
sed -i "s@YNH_COOKIE_PREFIX@${path#/}@g" ../conf/config.php
sed -i "s/YNH_COOKIE_DOMAIN/$domain/g" ../conf/config.php
logout_redirect="https://$domain/yunohost/sso"
sed -i "s@YNH_LOGOUT_REDIRECT_TO@$logout_redirect@g" ../conf/config.php
lang=$(sudo bash -c "echo $LANG | cut -d '.' -f 1")
sed -i "s@YNH_LANG@$lang@g" ../conf/config.php
timezone=$(cat /etc/timezone)
sed -i "s@YNH_TIMEZONE@$timezone@g" ../conf/config.php

# copy config files
sudo cp ../conf/{config.php,database.php,caldav.php} $final_path/web/config/

# execute database shema update
sudo /var/www/agendav/bin/agendavcli dbupdate

# Set permissions to agendav directory
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

# Modify Nginx configuration file and copy it to Nginx conf directory
sed -i "s@YNH_WWW_PATH@$path@g" ../conf/nginx.conf
sed -i "s@YNH_ALIAS@$final_path/web/public@g" ../conf/nginx.conf
nginxconf=/etc/nginx/conf.d/$domain.d/agendav.conf
sudo cp ../conf/nginx.conf $nginxconf
sudo chown root: $nginxconf
sudo chmod 600 $nginxconf

# Reload Nginx and regenerate SSOwat conf
sudo service nginx reload
sudo yunohost app ssowatconf
