#!/bin/bash

# Retrieve arguments
domain=$1
path=$2
is_public=$3
admin=$4
password=$5

# '/ghost' is refused by the application itself. This is used as the route to the admin section
if [[ "$path" = "/ghost" ]]; then
    echo "Error : the path cannot be '/ghost', please choose another location"
    exit 1
fi

# Check that admin user is an existing account
sudo yunohost user list | python ../conf/user_list.py | grep "^$admin$" >/dev/null
if [[ ! $? -eq 0 ]]; then
    echo "Error : the chosen admin user does not exist"
    exit 1
fi

# 'password' must be >= 8 characters
if [[ $(expr length $password) -lt "8" ]]; then
    echo "Error : the password must be >= 8 characters"
    exit 1
fi

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

echo "Install dependencies..."
sudo apt-get update
sudo apt-get install nodejs-legacy npm -y

version=0.4.1
echo "Downloading Ghost $version..."
mkdir ../tmp
sudo wget -O ../tmp/ghost-$version.zip "https://en.ghost.org/zip/ghost-$version.zip"

echo "Deploying source files..."
unzip ../tmp/ghost-$version.zip -d ../tmp/ghost
final_path=/var/www/ghostblog
sudo mkdir -p $final_path
sudo useradd -d $final_path ghostblog
sudo cp -r ../tmp/ghost $final_path
sudo chown -R ghostblog: $final_path

echo "Install Ghost with NPM..."
sudo su --shell /bin/bash --command "cd $final_path/ghost && npm install --production --registry http://registry.npmjs.eu" ghostblog

echo "Cleaning up install tree..."
sudo rm -rf $final_path/.npm
find $final_path -type d | grep "test$" | xargs sudo rm -rf
find $final_path -type d | grep "tests$" | xargs sudo rm -rf
sudo rm -rf $final_path/ghost/node_modules/sqlite3/build
sudo rm -rf $final_path/tmp

echo "Setting up database..."
db_name=ghostblog
db_user=ghostblog
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')
sudo yunohost app initdb $db_user -d $db_name -p $db_pwd
sudo yunohost app setting ghostblog mysqlpwd -v $db_pwd

echo "Deploying configuration..."
sed -i "s@YNH_DOMAIN@$domain@g" ../conf/config.js
sed -i "s@YNH_LOCATION@${path%/}@g" ../conf/config.js
sed -i "s@YNH_DBNAME@$db_name@g" ../conf/config.js
sed -i "s@YNH_DBUSER@$db_user@g" ../conf/config.js
sed -i "s@YNH_DBPWD@$db_pwd@g" ../conf/config.js
sudo cp ../conf/config.js $final_path/ghost
sudo chown ghostblog: $final_path/ghost/config.js
sudo chmod 644 $final_path/ghost/config.js


echo "Init script..."
logfile=/var/log/ynh-ghostblog.log
sudo touch $logfile
sudo chown ghostblog: $logfile
sed -i "s@YNH_FINALPATH@$final_path@g" ../conf/init-script
sed -i "s@YNH_LOGFILE@$logfile@g" ../conf/init-script
sudo cp ../conf/init-script /etc/init.d/ynh-ghostblog
sudo chmod +x /etc/init.d/ynh-ghostblog
sudo update-rc.d ynh-ghostblog defaults
sudo service ynh-ghostblog start
sudo yunohost app service ynh-ghostblog -l $logfile

echo "Setting up logrotate configuration..."
sed -i "s@YNH_LOGFILE@$logfile@g" ../conf/logrotate
sudo cp ../conf/logrotate /etc/logrotate.d/ynh-ghostblog

echo "Nginx configuration (sso disabled)..."
sed -i "s@YNH_LOCATION@$path@g" ../conf/nginx.conf
sudo cp ../conf/nginx.conf /etc/nginx/conf.d/$domain.d/ghostblog.conf
sudo yunohost app setting ghostblog skipped_uris -v "/"

echo "Reloading Nginx (sso disabled)..."
sudo service nginx reload
sudo yunohost app ssowatconf

echo "Registering admin..."
sleep 5
admin_name=$(sudo yunohost user list | python ../conf/user_getname.py $admin)
admin_mail=$(sudo yunohost user list | python ../conf/user_getmail.py $admin)
curl -kL -X GET https://$domain${path%/}/ghost/signup --cookie-jar cookie.txt > signup_get
sleep 5
csrf_token=$(cat signup_get | egrep csrf-param | egrep "content=\".+\"" -o | egrep "\".+\"" -o | cut -d '"' -f 2)
curl -kL -X POST https://$domain${path%/}/ghost/signup/ \
  --cookie cookie.txt  --header "X-CSRF-Token: $csrf_token" \
  --data-urlencode "name=$admin_name" \
  --data-urlencode "email=$admin_mail" \
  --data-urlencode "password=$password" \
  > /dev/null 2>&1
sudo yunohost app setting ghostblog admin -v $admin
sudo yunohost app setting ghostblog password -v $password

echo "Nginx configuration (sso enabled)..."
# clean up previous settings
sudo yunohost app setting ghostblog skipped_uris -d

if [ $is_public = "Yes" ]; then
  # The main site is accessible from anyone
  sudo yunohost app setting ghostblog skipped_uris -v "/"
  # The admin section is accessible only to Yunohost users
  sudo yunohost app setting ghostblog protected_uris -v "/ghost"
fi
sudo yunohost app setting ghostblog is_public -v $is_public

echo "Reloading Nginx (sso enabled)..."
sudo service nginx reload
sudo yunohost app ssowatconf

echo "Success ! You can go to https://$domain$path/ghost to write your posts"