#!/bin/bash

# PirateBox app for YunoHost 
# Copyright (C) 2015 Julien Vaubourg <julien@vaubourg.com>
# Contribute at https://github.com/labriqueinternet/piratebox_ynh
# 
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
# 
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

# This is an upgrade?
upgrade=$([ "${PIRATEBOX_UPGRADE}" == 1 ] && echo true || echo false)

# Retrieve arguments
domain=${1}
url_path=${2}
opt_domain=${3}
opt_name=${4}
opt_deleting=${5}
opt_renaming=${6}
opt_chat=${7}

if [ "${opt_deleting}" == yes ]; then
  opt_deleting=1
else
  opt_deleting=0
fi

if [ "${opt_renaming}" == yes ]; then
  opt_renaming=1
else
  opt_renaming=0
fi

if [ "${opt_chat}" == yes ]; then
  opt_chat=1
else
  opt_chat=0
fi

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

if ! $upgrade; then

  # Save arguments
  sudo yunohost app setting piratebox service_enabled -v 0
  sudo yunohost app setting piratebox opt_domain -v "${opt_domain}"
  sudo yunohost app setting piratebox opt_name -v "${opt_name}"
  sudo yunohost app setting piratebox opt_renaming -v "${opt_renaming}"
  sudo yunohost app setting piratebox opt_maxspace -v 90
  sudo yunohost app setting piratebox opt_deleting -v "${opt_deleting}"
  sudo yunohost app setting piratebox opt_chat -v "${opt_chat}"
  sudo yunohost app setting piratebox wifi_device_id -v -1

fi

# Install packages
packages='php5-fpm iptables libnet-dns-perl git'
export DEBIAN_FRONTEND=noninteractive

sudo apt-get --assume-yes --force-yes install ${packages}

if [ $? -ne 0 ]; then
  sudo apt-get update
  sudo apt-get --assume-yes --force-yes install ${packages}
fi

# Copy confs
sudo mkdir -pm 0755 /var/log/nginx/
sudo mkdir -pm 0755 /var/spool/piratebox/
sudo chown www-data: /var/spool/piratebox/

sudo install -b -o root -g root -m 0644 ../conf/nginx_captive-piratebox.conf /etc/nginx/captive-piratebox.conf
sudo install -b -o root -g root -m 0644 ../conf/nginx_piratebox.conf "/etc/nginx/conf.d/${domain}.d/piratebox.conf"
sudo install -b -o root -g root -m 0644 ../conf/phpfpm_piratebox.conf /etc/php5/fpm/pool.d/piratebox.conf
sudo install -b -o root -g root -m 0644 ../conf/phpfpm_captive-piratebox.conf /etc/php5/fpm/pool.d/captive-piratebox.conf

# Copy web sources (piratebox)
sudo git clone https://github.com/jvaubourg/php-piratebox.git /var/www/piratebox/

if [ ! -z "${PIRATEBOX_GITCOMMIT_UPSTREAM}" ]; then
  git --work-tree /var/www/piratebox/ --git-dir /var/www/piratebox/.git/ reset --hard "${PIRATEBOX_GITCOMMIT_UPSTREAM}"
fi

sudo chown -R root: /var/www/piratebox/
sudo chmod -R 0644 /var/www/piratebox/
sudo find /var/www/piratebox/ -type d -exec chmod +x {} \;

sudo rm -f /var/www/piratebox/config.php
sudo install -b -o root -g root -m 0644 ../conf/piratebox_config.php.tpl /var/www/piratebox/config.tpl.php

gitcommit_upstream=$(git --work-tree /var/www/piratebox/ --git-dir /var/www/piratebox/.git/ rev-parse HEAD)
sudo yunohost app setting piratebox gitcommit_upstream -v "${gitcommit_upstream}"

# Copy web sources (admin)
sudo mkdir -pm 0755 /var/www/piratebox_admin/
sudo cp -a ../sources/* /var/www/piratebox_admin/

sudo chown -R root: /var/www/piratebox_admin/
sudo chmod -R 0644 /var/www/piratebox_admin/*
sudo find /var/www/piratebox_admin/ -type d -exec chmod +x {} \;

# Fix permissions
sudo chown -R www-data: /var/www/piratebox/public/uploads/
sudo chown -R www-data: /var/www/piratebox/public/chat/

# Fix php-piratebox configuration
sudo sed 's|<TPL:NGINX_REALPATH>|/var/www/piratebox/|g' -i /var/www/piratebox/config.tpl.php

# Fix confs
## nginx captive portal
sudo sed "s|<TPL:DOMAIN>|${opt_domain}|g" -i /etc/nginx/captive-piratebox.conf
sudo sed 's|<TPL:NGINX_REALPATH>|/var/www/piratebox/|g' -i /etc/nginx/captive-piratebox.conf
sudo sed 's|<TPL:PHP_NAME>|piratebox|g' -i /etc/nginx/captive-piratebox.conf

## nginx admin
sudo sed "s|<TPL:NGINX_LOCATION>|${url_path}|g" -i "/etc/nginx/conf.d/${domain}.d/piratebox.conf"
sudo sed 's|<TPL:NGINX_REALPATH>|/var/www/piratebox_admin/|g' -i "/etc/nginx/conf.d/${domain}.d/piratebox.conf"
sudo sed 's|<TPL:PHP_NAME>|piratebox_admin|g' -i "/etc/nginx/conf.d/${domain}.d/piratebox.conf"

## php-fpm captive portal
sudo sed 's|<TPL:PHP_NAME>|piratebox|g' -i /etc/php5/fpm/pool.d/captive-piratebox.conf
sudo sed 's|<TPL:PHP_USER>|www-data|g' -i /etc/php5/fpm/pool.d/captive-piratebox.conf
sudo sed 's|<TPL:PHP_GROUP>|www-data|g' -i /etc/php5/fpm/pool.d/captive-piratebox.conf
sudo sed 's|<TPL:NGINX_REALPATH>|/var/www/piratebox/|g' -i /etc/php5/fpm/pool.d/captive-piratebox.conf

## php-fpm admin
sudo sed 's|<TPL:PHP_NAME>|piratebox_admin|g' -i /etc/php5/fpm/pool.d/piratebox.conf
sudo sed 's|<TPL:PHP_USER>|admin|g' -i /etc/php5/fpm/pool.d/piratebox.conf
sudo sed 's|<TPL:PHP_GROUP>|admins|g' -i /etc/php5/fpm/pool.d/piratebox.conf
sudo sed 's|<TPL:NGINX_REALPATH>|/var/www/piratebox_admin/|g' -i /etc/php5/fpm/pool.d/piratebox.conf

# Fix sources
sudo sed "s|<TPL:NGINX_LOCATION>|${url_path}|g" -i /var/www/piratebox_admin/config.php

# Install fakedns
sudo install -o root -g root -m 0755 ../conf/piratebox_fakedns /usr/local/bin/

# Copy init script
sudo install -o root -g root -m 0755 ../conf/ynh-piratebox /usr/local/bin/
sudo install -o root -g root -m 0644 ../conf/ynh-piratebox.service /etc/systemd/system/

# Update firewall
sudo yunohost firewall allow --no-upnp UDP 4253
sudo yunohost firewall allow --no-upnp TCP 4280

# Set default inits
sudo systemctl enable php5-fpm
sudo systemctl restart php5-fpm

sudo systemctl reload nginx

sudo systemctl enable ynh-piratebox
sudo yunohost service add ynh-piratebox

if ! $upgrade; then
  sudo systemctl start ynh-piratebox

  echo "WARNING: PirateBox is not started because you need to define an associated wifi hotspot through the web admin" >&2
fi

sudo yunohost app ssowatconf

exit 0