#!/bin/bash

set -eu

# Retrieve arguments
domain=$1
path=${2%/}
language=$3

# Source common variables and helpers
source ./_common.sh

# Set app specific variables
app="agendav"
dbname=$app
dbuser=$app

# Check domain/path availability
sudo yunohost app checkurl "${domain}${path}" -a "$app" \
  || exit 1

# Set and store language
language=${LANGUAGES[$3]}
ynh_app_setting_set "$app" language "$language"

# Check destination directory
DESTDIR="/var/www/${app}"
[[ -d "$DESTDIR" ]] && ynh_die \
"The destination directory '${DESTDIR}' already exists.\
 You should safely delete it before installing this app."

# Check whether Baïkal or Radicale is installed
if sudo yunohost app list --installed -f baikal | grep -q id ; then
    caldav_app="baikal"
    caldav_baseurl="/cal.php/"
elif sudo yunohost app list --installed -f radicale | grep -q id ; then
    caldav_app="radicale"
    caldav_baseurl="/"
else
    ynh_die "You must install Baïkal or Radicale before"
fi

# Install dependencies
ynh_package_is_installed "php5-cli" \
  || ynh_package_install "php5-cli"

# Create tmp directory and fetch app inside
TMPDIR=$(ynh_mkdir_tmp)
extract_agendav "$TMPDIR"

# Generate random password and encryption key
dbpass=$(ynh_string_random)
encryptkey=$(ynh_string_random 24)
ynh_app_setting_set "$app" encryptkey "$encryptkey"
ynh_app_setting_set "$app" mysqlpwd "$dbpass"

# Create log directory
LOGDIR=/var/log/agendav
sudo install -m 750 -o www-data -d "$LOGDIR"

# Copy and set AgenDAV configuration
conf_path="${TMPDIR}/web/config/settings.php"
cp ../conf/settings.php "$conf_path"
sed -i "s/{DBUSER}/${dbuser}/g"              "$conf_path"
sed -i "s/{DBPASS}/${dbpass}/g"              "$conf_path"
sed -i "s/{DBNAME}/${dbname}/g"              "$conf_path"
sed -i "s/{ENCRYPTKEY}/${encryptkey}/g"      "$conf_path"
sed -i "s@{LOGDIR}@${LOGDIR}@g"              "$conf_path"
sed -i "s@{TIMEZONE}@$(cat /etc/timezone)@g" "$conf_path"
sed -i "s@{LANGUAGE}@${language}@g"          "$conf_path"

# CalDAV config
caldav_domain=$(ynh_app_setting_get "$caldav_app" domain)
caldav_path=$(ynh_app_setting_get "$caldav_app" path)
caldav_url="https://${caldav_domain}${caldav_path%/}"
sed -i "s@{CALDAV_BASEURL}@${caldav_url}${caldav_baseurl}@g" "$conf_path"
sed -i "s@{CALDAV_DOMAIN}@${caldav_domain}@g"                "$conf_path"

# Install files and set permissions
sudo mv "$TMPDIR" "$DESTDIR"
sudo chown -hR root: "$DESTDIR"
sudo chown -hR www-data: "${DESTDIR}/web"
sudo chmod -R 750 "${DESTDIR}/web/var"

# Initialize database
ynh_mysql_create_db "$dbname" "$dbuser" "$dbpass"
(cd "$DESTDIR" && sudo sudo -u www-data \
    php agendavcli migrations:migrate --no-interaction) \
  || ynh_die "Unable to create AgenDAV tables"

# Copy and set nginx configuration
nginx_conf="/etc/nginx/conf.d/${domain}.d/${app}.conf"
sed -i "s@{PATH}@${path}@g"        ../conf/nginx.conf
sed -i "s@{LOCATION}@${path:-/}@g" ../conf/nginx.conf
sed -i "s@{DESTDIR}@${DESTDIR}@g"  ../conf/nginx.conf
# comment redirection in case of an installation at root
[[ -n "$path" ]] || sed -i '$s/^/#/' ../conf/nginx.conf
sudo cp ../conf/nginx.conf "$nginx_conf"

# Copy and set php-fpm configuration
phpfpm_conf="/etc/php5/fpm/pool.d/${app}.conf"
sed -i "s@{POOLNAME}@${app}@g"     ../conf/php-fpm.conf
sed -i "s@{DESTDIR}@${DESTDIR}/@g" ../conf/php-fpm.conf
sudo cp ../conf/php-fpm.conf "$phpfpm_conf"

# Reload services
sudo service php5-fpm restart
sudo service nginx reload
