#!/bin/bash MY_PATH="`dirname \"$0\"`" # relative MY_PATH="`( cd \"$MY_PATH\" && pwd )`" # absolutized and normalized [[ -f $MY_PATH/../.profile ]] && source $MY_PATH/../.profile unset err args=$@ [[ $args =~ noask ]] && noask=o && askSSL=o IP=$(ifconfig | grep "inet " | grep -v "127.0.0.1" | awk '{ print $2 }') [[ ! $KSMS_DOMAIN ]] && echo -e "${c_yellow}Choisissez un nom de domaine pour KALKUN (Sinon $IP sera choisi): $c_" && read KSMS_DOMAIN [[ ! $KSMS_DOMAIN ]] && KSMS_DOMAIN=$IP && askSSL=n && noask=o DOMAIN=$KSMS_DOMAIN ## Install PHP + MySQL $MY_PATH/1-install_requirements.sh php ## Create database if [[ $(sudo mysql -e "select * from mysql.user;" | grep gammu) ]]; then pwdDB=$(cat /etc/gammu-smsdrc | grep "password" | grep -v "# " | awk '{ print $3 }') else pwdDB=$(cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 42 | head -n 1) sudo mysql -e "CREATE USER 'gammu'@'localhost' IDENTIFIED BY '$pwdDB';" fi if [[ ! $(sudo mysql -e "show databases;" | grep gammu) ]]; then echo -e "${c_yellow}Création de la base de donnée...$c_" sudo mysql -e "CREATE DATABASE gammu" sudo mysql -e "GRANT ALL PRIVILEGES ON gammu.* TO 'gammu'@'localhost';" sudo mysql -e "FLUSH PRIVILEGES;" fi if [[ -z $(sudo mysql gammu -e "SHOW TABLES LIKE 'outbox';") ]]; then echo -e "${c_yellow}Importation du dump gammu...$c_" sudo mysql gammu < $MY_PATH/templates/4a/mysql.sql || err+="Import dump MySQL" fi if [[ -z $(sudo mysql gammu -e "SHOW TABLES LIKE 'kalkun';") ]]; then echo -e "${c_yellow}Importation du dump kalkun...$c_" [[ ! $ADRESSE ]] && ADRESSE=G1GROUPE sed -i "s/_GROUPDEF/$ADRESSE/g" $MY_PATH/templates/4a/kalkun.sql sudo mysql gammu < $MY_PATH/templates/4a/kalkun.sql || err+="Import dump Kalkun" fi ## Clone repo KALKUN [[ -d $MY_PATH/kalkun ]] && sudo rm -rf $MY_PATH/kalkun echo -e "${c_yellow}Clonage du dépot de Kalkun...$c_" git clone https://github.com/back2arie/Kalkun.git $MY_PATH/kalkun cp $MY_PATH/templates/4a/database.php $MY_PATH/kalkun/application/config/ cp $MY_PATH/templates/4a/daemon.sh $MY_PATH/templates/4a/daemon.php $MY_PATH/kalkun/scripts sed -i "s/_PWD/$pwdDB/g" $MY_PATH/kalkun/application/config/database.php || err+="Sed database password" sed -i "s/_DOMAIN/$DOMAIN/g" $MY_PATH/templates/4a/daemon.php || err+="Sed domaine to daemon.php" # touch $MY_PATH/kalkun/install ## Place le dossier kalkun dans /var/www/ [[ -d /var/www/kalkun ]] && sudo rm -rf /var/www/kalkun sudo mv $MY_PATH/kalkun /var/www/ || err+="Move kalkun to /var/www" sudo chown -R www-data:www-data /var/www/kalkun || err+="Chown www-data to kalkun" sudo chmod u+x /var/www/kalkun/scripts/* ## Add daemon launcher in sms_received.sh [[ ! $(grep "Execute Kalkun daemon" $MY_PATH/../sms_received.sh) ]] && sed -i "/^export YOU/i # Execute Kalkun daemon\n/var/www/kalkun/scripts/daemon.sh &\n" $MY_PATH/../sms_received.sh ## Active gammu in MySQL mode if [[ $(grep "service = files" /etc/gammu-smsdrc) ]]; then echo -e "${c_yellow}Activation de gammu-smsd en mode MySQL...$c_" sudo sed -i "/^service = files/a service = sql\ndriver = native_mysql\nhost = localhost\nuser = gammu\npassword = $pwdDB\ndatabase = gammu" /etc/gammu-smsdrc || err+="Sed gammu-smsd" sudo sed -i "/service = files/d" /etc/gammu-smsdrc || err+="Sed delete in gammu-smsd" $MY_PATH/../debug/gammu-restart.sh || err+="Restart gammu" fi ## Config NGINX echo -e "${c_yellow}Configuration de Nginx...$c_" [[ ! -d /etc/nginx/def_conf ]] && sudo mkdir /etc/nginx/def_conf/ sudo cp $MY_PATH/templates/rproxy/proxypass.conf /etc/nginx/def_conf/ || err+="Copy proxypass.conf" sudo cp $MY_PATH/templates/4a/kalkun.conf /etc/nginx/conf.d/ || err+="Copy kalkun.conf" sudo sed -i "s/_DOMAIN/$DOMAIN/g" /etc/nginx/conf.d/kalkun.conf || err+="Sed domain in kalkun.conf" ## Active SSL [[ ! $noask ]] && printf "${c_yellow}Voulez-vous installer et activer un certificat SSL maintenant pour $DOMAIN ? (o/n) $c_" && read askSSL if [[ $askSSL =~ ^(o|y|yes|oui|Y|O|YES)$ ]]; then if sudo test ! -f /etc/letsencrypt/live/$DOMAIN/fullchain.pem; then $MY_PATH/ssl.sh certif APP=kalkun DOMAIN=$DOMAIN || err+="Create SSL certification" fi if sudo test -f /etc/letsencrypt/live/$DOMAIN/fullchain.pem; then $MY_PATH/ssl.sh on APP=kalkun DOMAIN=$DOMAIN || err+="Enable SSL certification" else echo -e "${c_red}Une erreur s'est produite, basculement vers le mode non SSL$c_" $MY_PATH/ssl.sh off APP=kalkun DOMAIN=$DOMAIN || err+="Disable SSL certification" fi http=https sudo sed -i "s/http/https/g" /var/www/kalkun/scripts/daemon.php || err+="Sed http to https in daemon.php" else $MY_PATH/ssl.sh off APP=kalkun DOMAIN=$DOMAIN || err+="Disable SSL certification" http=http fi echo -e "${c_yellow}Redémarrage de Nginx...$c_" sudo service nginx restart || err+="Restart Nginx" echo -e "${c_yellow}Redémarrage de gammu-smsd...$c_" $MY_PATH/../debug/gammu-restart.sh || err+="Restart gammu" if [[ $err ]]; then echo -e "${c_red}Installation incomplète: $err$c_" exit 1 else echo -e "${c_green}Kalkun est accessible via l'URL $http://$DOMAIN$c_" exit 0 fi