#!/bin/bash

# Retrieve arguments
domain=${1}
url_path=${2}
wifi_ssid=${3}
wifi_passphrase=${4}
ip6_net=${5}

# Check arguments
if [ -z "${wifi_ssid}" -o -z "${wifi_passphrase}" ]; then
  echo "ERROR: Your Wifi Hotspot needs a name and a password" >&2
  exit 1
fi

wifi_passphrase_length="$(echo -n "${wifi_passphrase}" | wc -c)"
if [ "${wifi_passphrase_length}" -lt 8 -o "${wifi_passphrase_length}" -gt 63 ]; then
  echo "ERROR: Your password must from 8 to 63 characters (WPA2 passphrase)" >&2
  exit 1
fi

echo "${wifi_passphrase}" | grep -qP '[^[:print:]]'
if [ $? -eq 0 ]; then
  echo "ERROR: Only printable ASCII characters are permitted in your password (WPA2 passphrase)" >&2
  exit 1
fi

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

# Install packages
# TODO: Replace isc-dhcp-server by dnsmasq (currently negotiating with the YunoHost team to
# also replace bind9 by dnsmasq)
#sudo apt-get update
sudo apt-get --assume-yes --force-yes install hostapd radvd isc-dhcp-server iptables php5-fpm

# Extra packages
sudo apt-get --assume-yes --force-yes install sipcalc iw

# Compute extra arguments
if [ -z "${ip6_net}" ]; then
  ip6_net=none
  ip6_addr=none
else
  ip6_net=$(bash ../conf/ipv6_expanded "${ip6_net}")

  if [ -z "${ip6_net}" ]; then
    echo "ERROR: The IPv6 Delegated Prefix format looks bad" >&2
    exit 1
  fi

  ip6_addr="$(echo "${ip6_net}" | cut -d: -f1-7):42"
  ip6_net=$(bash ../conf/ipv6_compressed "${ip6_net}")
  ip6_addr=$(bash ../conf/ipv6_compressed "${ip6_addr}")
fi

wifi_device=$(sudo iwconfig 2>&1 | grep 802.11 | head -n1 | awk '{ print $1 }')
wifi_n=0

if [ -z "${wifi_device}" ]; then
  echo "ERROR: No wifi interface found" >&2
  exit 1
fi

sudo iwconfig "${wifi_device}" | grep -q 'n *ESSID'
if [ $? -eq 0 ]; then
  wifi_n=1
fi

# Save arguments
sudo yunohost app setting hotspot wifi_ssid -v "${wifi_ssid}"
sudo yunohost app setting hotspot wifi_passphrase -v "${wifi_passphrase}"
sudo yunohost app setting hotspot wifi_device -v "${wifi_device}"
sudo yunohost app setting hotspot wifi_channel -v 6
sudo yunohost app setting hotspot wifi_n -v "${wifi_n}"
sudo yunohost app setting hotspot ip6_addr -v "${ip6_addr}"
sudo yunohost app setting hotspot ip6_net -v "${ip6_net}"
sudo yunohost app setting hotspot ip6_dns0 -v 2001:913::8
sudo yunohost app setting hotspot ip6_dns1 -v 2001:910:800::12
sudo yunohost app setting hotspot ip4_dns0 -v 80.67.188.188
sudo yunohost app setting hotspot ip4_dns1 -v 80.67.169.12
sudo yunohost app setting hotspot ip4_nat_prefix -v 10.0.242
sudo yunohost app setting hotspot vpnclient -v no

# Install IPv6 scripts
sudo install -o root -g root -m 0755 ../conf/ipv6_expanded /usr/local/bin/
sudo install -o root -g root -m 0755 ../conf/ipv6_compressed /usr/local/bin/

# Copy confs
sudo install -b -o root -g root -m 0644 ../conf/hostapd.conf.tpl /etc/hostapd/
sudo install -b -o root -g root -m 0644 ../conf/radvd.conf.tpl /etc/
sudo install -b -o root -g root -m 0644 ../conf/dhcpd.conf.tpl /etc/dhcp/
sudo install -b -o root -g root -m 0644 ../conf/nginx_wifiadmin.conf "/etc/nginx/conf.d/${domain}.d/wifiadmin.conf"
sudo install -b -o root -g root -m 0644 ../conf/phpfpm_wifiadmin.conf /etc/php5/fpm/pool.d/wifiadmin.conf

# Copy web sources
sudo mkdir -pm 0755 /var/www/wifiadmin/
sudo cp -a ../sources/* /var/www/wifiadmin/

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

# Create user for the web admin
sudo useradd -MUr wifiadmin

# Fix confs
## hostapd
sudo sed 's|^DAEMON_CONF=$|&/etc/hostapd/hostapd.conf|' -i /etc/init.d/hostapd

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

## php-fpm
sudo sed 's|<TPL:PHP_NAME>|wifiadmin|g' -i /etc/php5/fpm/pool.d/wifiadmin.conf
sudo sed 's|<TPL:PHP_USER>|admin|g' -i /etc/php5/fpm/pool.d/wifiadmin.conf
sudo sed 's|<TPL:PHP_GROUP>|admins|g' -i /etc/php5/fpm/pool.d/wifiadmin.conf
sudo sed 's|<TPL:NGINX_REALPATH>|/var/www/wifiadmin/|g' -i /etc/php5/fpm/pool.d/wifiadmin.conf
sudo sed 's|^;\?\s*max_execution_time.\+|max_execution_time = 600|' -i /etc/php5/fpm/php.ini

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

# Copy init script
sudo install -b -o root -g root -m 0755 ../conf/init_ynh-hotspot /etc/init.d/ynh-hotspot

# Set default inits
# The boot order of these services are important, so they are disabled by default
# and the ynh-hotspot service handles them.
# All services are registred by yunohost in order to prevent conflicts after the uninstall.
sudo yunohost service add isc-dhcp-server
sudo yunohost service stop isc-dhcp-server
sudo yunohost service disable isc-dhcp-server

sudo yunohost service add radvd
sudo yunohost service stop radvd
sudo yunohost service disable radvd

sudo yunohost service add hostapd 
sudo yunohost service stop hostapd 
sudo yunohost service disable hostapd 

sudo yunohost service add php5-fpm
sudo yunohost service enable php5-fpm

sudo service nginx reload

# Remove IPv6 address set if there is a VPN installed
if [ "${ip6_addr}" != none ]; then
  sudo ip -6 address show dev tun0 2> /dev/null | grep -q "${ip6_addr}/"
  if [ "$?" -eq 0 ]; then
    sudo ip address delete "${ip6_addr}/128" dev tun0 &> /dev/null
  fi
fi

sudo yunohost service add ynh-hotspot
sudo yunohost service enable ynh-hotspot
sudo service ynh-hotspot start

# Update SSO for wifiadmin
sudo yunohost app ssowatconf

exit 0
