#!/bin/bash

# causes the shell to exit if any subcommand or pipeline returns a non-zero status
set -e

# This is a multi-instance app, meaning it can be installed several times independently
# The id of the app as stated in the manifest is available as $YNH_APP_ID
# The instance number is available as $YNH_APP_INSTANCE_NUMBER (equals "1", "2", ...)
# The app instance name is available as $YNH_APP_INSTANCE_NAME
#    - the first time the app is installed, YNH_APP_INSTANCE_NAME = ynhexample
#    - the second time the app is installed, YNH_APP_INSTANCE_NAME = ynhexample__2
#    - ynhexample__{N} for the subsequent installations, with N=3,4, ...
# The app instance name is probably what you are interested the most, since this is
# guaranteed to be unique. This is a good unique identifier to define installation path,
# db names, ...
app=$YNH_APP_INSTANCE_NAME

# Retrieve app settings
domain=$(sudo yunohost app setting $app domain)
path=$(sudo yunohost app setting $app path)
admin=$(sudo yunohost app setting $app admin)
is_public=$(sudo yunohost app setting $app is_public)

# Remove trailing slash to path
path=${path%/}
#force location to be / or /foo
location=${path:-/}

# admin default value, if not set
if [ -z "$admin" ];
then
    admin=$(sudo yunohost user list | grep 'username' -m1 | awk '{print $2}')
    sudo yunohost app setting $app is_public -v "$is_public"
fi


# Modify dokuwiki conf
sed -i "s@YNH_ADMIN_USER@$admin@g" ../conf/dokuwiki.php

# Copy files to the right place
final_path=/var/www/$app
sudo mkdir -p $final_path
sudo cp -a ../sources/* $final_path
sudo cp ../conf/dokuwiki.php $final_path/conf

# Do not override ACL configuration file
if [ ! -f "$final_path/conf/acl.auth.php" ]; then
    sudo cp ../conf/acl.auth.php $final_path/conf
fi

# Remove upgrade notification
# See https://www.dokuwiki.org/update_check
sudo touch $final_path/doku.php

# Remove deleted files
# See https://www.dokuwiki.org/install:unused_files
grep -Ev '^($|#)' ../sources/data/deleted.files | xargs -I {} sudo rm -vrf $final_path/{}

# Files owned by root, www-data can just read
sudo find $final_path -type f -print0 | sudo xargs -0 chmod 0644
sudo find $final_path -type d -print0 | sudo xargs -0 chmod 0755
sudo chown -R root: $final_path

# except for conf, data, some data subfolders, and lib/plugin, where www-data must have write permissions
sudo chown -R www-data:root $final_path/{conf,data,data/attic,data/cache,data/index,data/locks,data/media*,data/meta,data/pages,data/tmp,lib/plugins,lib/tpl}
sudo chmod -R 700 $final_path/{conf,data,data/attic,data/cache,data/index,data/locks,data/media*,data/meta,data/pages,data/tmp,lib/plugins,lib/tpl}

# Modify Nginx configuration file and copy it to Nginx conf directory
sed -i "s@YNH_WWW_LOCATION@$location@g" ../conf/nginx.conf
sed -i "s@YNH_WWW_PATH@$path@g" ../conf/nginx.conf
sed -i "s@YNH_WWW_ALIAS@$final_path/@g" ../conf/nginx.conf
sudo cp ../conf/nginx.conf /etc/nginx/conf.d/$domain.d/$app.conf

if [ "$is_public" = "Yes" ];
then
  sudo yunohost app setting $app skipped_uris -d
  sudo yunohost app setting $app unprotected_uris -v "/"
fi

sudo service nginx reload