#!/bin/bash
# Allo-GG
# Installation de Allo-GG sur un Pi Zero avec Rasbian.

################################################
# MERCI DE MODIFIER LA CONFIGURATION SUIVANTE :#
################################################
# NOM DU RESEAU WI-FI (SSID)                   #
wifi_ssid='nom-du-reseau-wifi-SSID'
#                                              #
# MOT DE PASSE DU RESEAU WI-FI
wifi_psw='mdp-du-reseau-wifi'
#                                              #
# MOT DE PASSE POUR L'ACCES ALLO-GG            #
allogg_psw='mdp-pour-acces-allogg'
################################################



# Configuration du nom reseau du module
echo "allo-gg" > /etc/hostname

# Configuration du Wi-FI
echo "ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev" > /etc/wpa_supplicant/wpa_supplicant.conf
echo "update_config=1" >> /etc/wpa_supplicant/wpa_supplicant.conf
echo "country=FR" >> /etc/wpa_supplicant/wpa_supplicant.conf
echo >> /etc/wpa_supplicant/wpa_supplicant.conf
echo "network={" >> /etc/wpa_supplicant/wpa_supplicant.conf
echo '	ssid="'$wifi_ssid'"' >> /etc/wpa_supplicant/wpa_supplicant.conf
echo '	psk="'$wifi_psw'"' >> /etc/wpa_supplicant/wpa_supplicant.conf
echo "}" >> /etc/wpa_supplicant/wpa_supplicant.conf

sleep 2
echo
echo "Connexion en cours sur internet ..."
ifdown --force wlan0
sleep 2
ifup wlan0
sleep 2

# tester 5 ping
if ping -c5 1.1.1.1 | grep -q '0% packet loss'
# Si la reponse est 0 paquets perdu, alors OK
then
  echo "[✅ OK] Internet"
# Mais sinon, il y a un probleme
else
  # Tentative de relancer le wifi ...
  sleep 2
  echo
  echo "[❌ ERREUR] Probleme sur le Wifi ? Tentative de relancer la connexion en cours ..."
  ifdown --force wlan0
  sleep 2
  ifup wlan0
  sleep 2

  # ... puis refaire un test, en testant 10 ping cette fois
  if ping -c10 1.1.1.1 | grep -q '0% packet loss'
  # Si la reponse est 0 paquets perdu, alors finalement tout va bien
  then
  echo "[✅ OK] Internet"
  # Mais si le probleme est toujours present, alors on alerte :
  else
  echo "[❌ ERREUR] Internet"
  echo "Merci de verifier la configuration Wi-Fi dans le fichier /etc/wpa_supplicant/wpa_supplicant.conf"
  exit
  fi
fi

# Activation du SSH
systemctl enable ssh
systemctl start ssh

# Installation des utilitaires
cd || exit
apt-get -y install apache2 php7.0 git-core vim tree rsync lsof curl screen

# Installation de log2ram
git clone https://git.heuzef.com/heuzef/log2ram.git
cd log2ram || exit
chmod +x install.sh
./install.sh
cd || exit

# Installation de wiringPi
git clone https://git.heuzef.com/heuzef/WiringPi.git
cd wiringPi || exit
chmod +x build
./build
cd || exit

# Creation des utilisateurs
useradd --create-home allo-gg
adduser allo-gg sudo
echo allo-gg:$allogg_psw | chpasswd
echo root:$allogg_psw | chpasswd

# Suppression de l'utilisateur "pi"
deluser -f --remove-home pi

# Execution automatique de bash a la connexion
echo "bash" >> /home/allo-gg/.profile

# Définition de la Timezone et activation de la synchro NTP
timedatectl set-timezone Europe/Paris
timedatectl set-ntp true
sed -i '$ d' /etc/systemd/timesyncd.conf
echo "FallbackNTP=0.arch.pool.ntp.org 1.arch.pool.ntp.org 2.arch.pool.ntp.org 3.arch.pool.ntp.org" >> /etc/systemd/timesyncd.conf

# Autoriser le reboot du module
echo >> /etc/sudoers
echo "## user is allowed to execute halt and reboot" >> /etc/sudoers
echo "allo-gg ALL=NOPASSWD: /sbin/halt, /sbin/reboot, /sbin/poweroff" >> /etc/sudoers
echo "www-data ALL=NOPASSWD: /sbin/halt, /sbin/reboot, /sbin/poweroff" >> /etc/sudoers

# Autorisation de clefs SSH publiques
mkdir /home/allo-gg/.ssh/
echo "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAACAQDhcGWA/uNEz7AFTfSZJBrxZjSB3KVHNNl0xnRfQWlEkJvqNeap64kv39DWajCKUsGpx2TDvJjQVoeMZFKFiA952S6+rkboO4piWOe7PswWwHAD/UrJYzTOEt+Dk8RnYH+FbMsj8gFrJZCJeOtjcflVIaaGAKX0gIDvf/1RAlBKM9KrsOLs/ELBzfGgIfa4ODF1qm3x6UnEu/X339Qk6A4OwzCkKtkzDIFjXt8BymO/iUiFe5DAaBk2IwOrnloQqHL6ROSDvtaHDsWkuLt8M6p1HB+7VdFMSgKfYXmiRxOyxnAYA4fcy8PLmyzStOkpAsy0BxoIeNsehTA4HtL14tgsJMWVhZ33guIP6F2d2cg9lzl0q2qSiKNc+XX+923LbWr6AlSaWyWQk0Z60jkAL1xyfJlUJ3r4kz+AYtp8AuV+9L7I8t8QWtDfjE8RNDwhjZNKyciDUHggbl+qIJ2e7del3izEtE9IBdIqWTKYTWk1canUCGYvGuZP+cacP8p6lFFSIVcoO6rvnbhBW8BcqkZl3rkhmGifYG40r+0a7wpZ/JJh/heQ2BRUI+xtmNBgJcF/6DhtqWdSZdRXAsBZPiX99EJR+RlFRi+l1MBnLb0fZafn6H3zKuOEv2NYlmBHccW5ZrFIWyQ4yUnKJ75i1wXtsoJ4BYlUhv7EaHPp4CvpYQ== Heuzef" >> /home/allo-gg/.ssh/authorized_keys
echo "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDkdi/Nt+Remfwx5vM/lBmnTUOQ3+UJtbT4P1P+sOXt0dADUHj7gRvZxfb73n7A8y6rfocDiV4IRf1yvg8JWrAFC7/aDCMYDAOMAld9fKFU7u73beWl0zMSTVSGxt2MTSpyU+POx6VvucWppJQJN8R2VtAPHheUZlpLEf0n2gzgyYEYuHEOPHfx91u3omu4QCYRVWv4ZFRCCvznC2Cd+GW2S8uNqe6oxe2HuXtghAIRO+XXDSfwJc0lsm7uKMJ9rtQMYSYUI0f3HErHArf5ssQMCMcHivdJuoWEmvK2UarQdT5kKlytDUjFMrjj4Ks6+Ew6mdvp6H4/PeRjONaDWOQ3 karimcisse@Karims-MacBook-Air.local" >> /home/allo-gg/.ssh/authorized_keys

# Desactiver toutes authentifications SSH par mot de passe
echo "PasswordAuthentication no" >> /etc/ssh/sshd_config
echo "PermitRootLogin no" >> /etc/ssh/sshd_config

# Desactiver les services innutiles
systemctl stop bluetooth
systemctl disable bluetooth
systemctl mask bluetooth.service

# Installation de Allo-GG
rm -vf /var/www/html/index.html
chown -R allo-gg:allo-gg /var/www/html/
su -l allo-gg -c "git clone https://git.heuzef.com/heuzef/Allo-GG.git /var/www/html/"
mv -vf /var/www/html/log2ram.conf /etc/
mv /etc/cron.hourly/log2ram /etc/cron.daily/log2ram

# Definition du mot de passe
sed -i "s/#allogg_psw#/$allogg_psw/g" /var/www/html/ui/index.php

# Lancement automatique du script de demarrage
cp -a /etc/rc.local /etc/rc.local.bak
echo "#!/bin/sh -e" > /etc/rc.local
echo "#" >> /etc/rc.local
echo "# rc.local" >> /etc/rc.local
echo "" >> /etc/rc.local
echo "/bin/bash /var/www/html/scripts/boot.sh &" >> /etc/rc.local
echo "" >> /etc/rc.local
echo "exit 0" >> /etc/rc.local

# Mise en place de la routine de verification de l'etat des services
echo '*/20 * * * * /var/www/html/scripts/check_services.sh > /dev/null' | crontab -u allo-gg -

# Creation du fichier de log
date > /var/www/html/ui/allo-gg.log
echo "Installation du module ALLO-GG" >> /var/www/html/ui/allo-gg.log
echo "----------------------------" >> /var/www/html/ui/allo-gg.log
echo >> /var/www/html/ui/allo-gg.log

echo
echo "----------------------------"
echo "Installation OK ! Redemarrage dans 10 secondes ..."
echo "L'interface web est maintenant accessible sur l'adresse IP du module :"
echo "----------------------------"
hostname -I
echo "----------------------------"
sleep 10
sh /var/www/html/scripts/reboot.sh
