#!/bin/bash

######################################################################
# VALEURS A MODIFIER SI NECESSAIRE 

# mettre SPOOF=true pour activer le hostname spoofing
SPOOF=false

# options pour le configure
#confopt="--with-gmplib=/bahamut/lib/libgmp.a --with-gmpinc=/bahamut/include/gmp.h"
confopt=""
######################################################################

change_admin_email () {
    read -ep "Email de l'administrateur : " admin_email
}

change_ecole () {
    read -ep "Ecole : " ecole
    ecole=$(echo "$ecole" | tr A-Z a-z)
}

change_spoofpass () {
    if "$SPOOF"; then
	read -esp "Spoof password : " spoofpass
    fi
}

change_confrep () {
    echo "Dans quel répertoire doivent être installés"
    echo "les fichiers de configuration ?"
    read -ep "> " confrep
    confrep=$(echo "$confrep" | sed 's,\([^/]\)$,\1/,')
}

change_binpath () {
    echo "Quel est le chemin où doit être installé l'exécutable ircd ?"
    echo "(cela doit pointer sur un fichier, pas sur un répertoire)"
    read -ep "> " binpath
}

change_all () {
    change_admin_email
    change_ecole
    change_spoofpass
    change_confrep
    change_binpath
}

display_menu () {
    echo "Paramètres courants:"
    echo "(ea) Email de l'administrateur: $admin_email"
    echo "(ec) Ecole : $ecole"
    $SPOOF && echo "(sp) Spoof password : <*>"
    echo "(rc) Répertoire des fichiers de conf : $confrep"
    echo "(ei) Exécutable ircd : $binpath"
    echo "(q)  Quitter sans configurer"
    echo "(x)  Configurer et quitter"
}

sur () {
    local answer
    read -ep "Êtes-vous sûr? " answer
    case "$answer" in
	(o*|y*)
	return 0;;
    esac
    return 1
}

loop () {
    local done=false
    while ! "$done"; do
	local answer
	display_menu
	read -ep "Votre choix : " answer
	case "$answer" in
	    (ea) change_admin_email;;
	    (ec) change_ecole;;
	    (rc) change_confrep;;
	    (ei) change_binpath;;
	    (q) if sur; then exit 0; fi;;
	    (x) if sur; then configurer; exit 0; fi;;
	    (sp)
	    if "$SPOOF"; then
		change_spoofpass
	    else
		echo "Mauvaise réponse"
	    fi;;
	    (*) echo "Mauvaise réponse";;
	esac
    done
}

configurer () {
./configure $confopt || exit 0

./config << EOCONF
1024 
1000
24
$confrep
$binpath
Yes
No 
No 
No 
No 
$admin_email
flash@minet.net 
Yes
Yes
Yes
Yes
No
5050000
(4 * MAXSENDQLENGTH)
No
No
services.rezosup.net 
10 
Yes 
Yes 
EOCONF

ed -s include/config.h <<EOF
/#define STAFF_ADDRESS/s/staff[^\"]*\"/staff.$ecole.rezosup.net\"/
/#undef *RK_NOTICES/s/#undef/#define/
/#define *CONNECTFAST/s/#define/#undef/
wq
EOF

if $SPOOF; then
  ed -s include/config.h <<EOF
/^\/\/.*HOSTNAME_SPOOFING/s,^//,,
/HOSTNAME_SPOOFING_PWD/s/\"[^\"]*\"/\"$spoofpass\"/
wq
EOF
fi

echo "Le fichier include/config.h a été modifié."
echo "Vous pouvez lancer make install."
}

change_all

loop

