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

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

# MAJ et installation des utilitaires
cd || exit
apt-get -y update
apt-get -y upgrade
apt-get -y install apache2 php7.0 git-core vim tree rsync lsof curl screen

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

# Installation de wiringPi
git clone git://git.drogon.net/wiringPi
cd wiringPi || exit
chmod +x build
./build
cd || exit

# Creation de l'utilisateur
useradd --create-home allo-gg
adduser allo-gg sudo
echo allo-gg:allo-gg | chpasswd
echo root:allo-gg | chpasswd

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

# 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 AAAAB3NzaC1yc2EAAAADAQABAAABAQDbvr8J95SXj0byVhBSOt/iShxzf0TVGKaxvo4d+pdrEaiDFxQEn/Iim9qEH9FXV9kQ8frUuYmVbh7Rqvh8QpTQM77eLHeZgXejw7St0OMmZSGpUsomIANZQNspDQ/qQbXFXmKnGJiC7TCEqaNoDTmHrdZ4Hvei4ly16e8Zic5pjcMruNeixcVkZOLJZWViGgbMlZN8jSll9pFfNRYKhIp6boalK5DexzgUZkUCk4HkPHo0I70LyElINmt9PkpHwzAFbsTRmI27Z9C5xI5OZ86aL+Kq4lm+MoD+f3hDiOdAByNqIEU1LU7tnMpQHbKSYSoVHlySF1+tuw3O1E4HQO45 heuzef@x240" >> /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 disable bluetooth

# 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://github.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

# Lancement automatique du script de demarrage
sed -i '1 i #!/bin/sh -e' /etc/rc.local
sed -i '$ d' /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 'MAILTO=""' | crontab -u allo-gg -
echo '*/20 * * * * allo-gg /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 en cours ..."

sleep 3
sh /var/www/html/scripts/reboot.sh
