#!/bin/sh --

# dinoconfig
#
#   Ce programme peut être librement distribué suivant les termes de la
#   « General Public License », version 2 ou ultérieure. Voir le fichier
#   COPYING pour les détails. Si vous n'avez pas reçu de copie de cette
#   licence avec le programme, allez voir le site www.gnu.org pour l'obtenir.
#
#   This program can be freely distributed following the terms of the
#   General Public Licence, version 2 or later. See file COPYING for
#   details. If you haven't received a copy of this license with the
#   program, please visit www.gnu.org to obtain it.
#


DIR=$HOME/.dino2
FILE=$HOME/.dino2/config

usage()
{
    cat <<EOF
usage : dinoconfig [-h|--help] [fichier]

EOF

}

do_options()
{
    global=0
    while [ $# != 0 ]; do
	case "$1" in
	--help|-h)
	    usage;
	    exit 0 ;;
	*)
	    global=1
	    FILE="$1"
	esac
	shift
    done
    if [ "$global" = 0 ]; then
	umask 066
	mkdir -p $DIR || exit 1
    fi
}

get_default()
{
    if [ -z "$DINO_LOGIN" ]; then
      LOGIN=jlapin
    else
      LOGIN=$DINO_LOGIN
    fi
    if [ -z "$DINO_PASS" ]; then
      PASSWORD=""
    else
      PASSWORD=$DINO_PASS
    fi
    if [ -z "$DINO_NICK" ]; then
      NICK=""
    else
      NICK=$DINO_NICK
    fi
    if [ -z "$DINO_SERV" ]; then
      SERVEUR=""
    else
      SERVEUR=$DINO_SERV
    fi
    PORT=4030
    if [ -z "$DINO_SIGNE" ]; then
      DEFAUT_SIGNE=0
    else
      DEFAUT_SIGNE=1
    fi
    CACHE=0
    LIRE_CENSURE=0
    if [ -z "$DINO_EDITOR" ]; then
      EDITEUR='pico'
    else
      EDITEUR=$DINO_EDITOR
    fi
    if [ -z "$DINO_BROWSER" ]; then
      BROWSER='lynx'
    else
      BROWSER=$DINO_BROWSER
    fi
    REPONSE='>>> %a a écrit :'

# Paramètres avancés
    RECONNEXION=10
    TIMEOUT=60
    TAILLE_CACHE=200
    BLOC_PARAVANCE=3
    MAX_PARAVANCE=10
    TYPE=1
    FILTER=0
}

get() #récupère les anciens paramètres; retourne 1 si pas encore enregistré
{
	touch "$FILE" || exit 1
	LOGIN2=`cat "$FILE" | awk '/^login/ {for(i=2;i<NF;i++) printf("%s ",$i); printf("%s\n",$NF) }'`
	if [ -z "$LOGIN2" ]; then
	  LOGIN2=$LOGIN
	fi
	PASSWORD2=`cat "$FILE" | awk '/^password/ {for(i=2;i<NF;i++) printf("%s ",$i); printf("%s\n",$NF) }'`
	if [ -z "$PASSWORD2" ]; then
	  PASSWORD2=$PASSWORD
	fi
	NICK2=`cat "$FILE" | awk '/^nick/ {for(i=2;i<NF;i++) printf("%s ",$i); printf("%s\n",$NF) }'`
	if [ -z "$NICK2" ]; then
	  NICK2=$NICK
	fi
	SERVEUR2=`cat "$FILE" | awk '/^serveur/ {for(i=2;i<NF;i++) printf("%s ",$i); printf("%s\n",$NF) }'`
	if [ -z "$SERVEUR2" ]; then
	  if [ -z "$SERVEUR" ]; then
	    SERVEUR2=gw1.dino.ens-lyon.org:gw2.dino.ens-lyon.org:gw3.dino.ens-lyon.org:gw4.dino.ens-lyon.org
	  else
	    SERVEUR2=$SERVEUR
	  fi
	fi
	PORT2=`cat "$FILE" | awk '/^port/ {for(i=2;i<NF;i++) printf("%s ",$i); printf("%s\n",$NF) }'`
	if [ -z "$PORT2" ]; then
	  PORT2=$PORT
	fi
	DEFAUT_SIGNE2=`cat "$FILE" | awk '/^defaut_signe/ {for(i=2;i<NF;i++) printf("%s ",$i); printf("%s\n",$NF) }'`
	if [ -z "$DEFAUT_SIGNE2" ]; then
	  DEFAUT_SIGNE2=$DEFAUT_SIGNE
	fi
	CACHE2=`cat "$FILE" | awk '/^cache/ {for(i=2;i<NF;i++) printf("%s ",$i); printf("%s\n",$NF) }'`
	if [ -z "$CACHE2" ]; then
	  CACHE2=$CACHE
	fi
	LIRE_CENSURE2=`cat "$FILE" | awk '/^lire_censure/ {for(i=2;i<NF;i++) printf("%s ",$i); printf("%s\n",$NF) }'`
	if [ -z "$LIRE_CENSURE2" ]; then
	  LIRE_CENSURE2=$LIRE_CENSURE
	fi
	EDITEUR2=`cat "$FILE" | awk '/^editeur/ {for(i=2;i<NF;i++) printf("%s ",$i); printf("%s\n",$NF) }'`
	if [ -z "$EDITEUR2" ]; then
	  EDITEUR2=$EDITEUR
	fi
	BROWSER2=`cat "$FILE" | awk '/^browser/ {for(i=2;i<NF;i++) printf("%s ",$i); printf("%s\n",$NF) }'`
	if [ -z "$BROWSER2" ]; then
	  BROWSER2=$BROWSER
	fi
	REPONSE2=`cat "$FILE" | awk '/^reponse/ {for(i=2;i<NF;i++) printf("%s ",$i); printf("%s\n",$NF) }'`
	if [ -z "$REPONSE2" ]; then
	  REPONSE2=$REPONSE
	fi
	RECONNEXION2=`cat "$FILE" | awk '/^reconnexion/ {for(i=2;i<NF;i++) printf("%s ",$i); printf("%s\n",$NF) }'`
	if [ -z "$RECONNEXION2" ]; then
	  RECONNEXION2=$RECONNEXION
	fi
	TIMEOUT2=`cat "$FILE" | awk '/^timeout/ {for(i=2;i<NF;i++) printf("%s ",$i); printf("%s\n",$NF) }'`
	if [ -z "$TIMEOUT2" ]; then
	  TIMEOUT2=$TIMEOUT
	fi
	TAILLE_CACHE2=`cat "$FILE" | awk '/^taille_cache/ {for(i=2;i<NF;i++) printf("%s ",$i); printf("%s\n",$NF) }'`
	if [ -z "$TAILLE_CACHE2" ]; then
	  TAILLE_CACHE2=$TAILLE_CACHE
	fi
	BLOC_PARAVANCE2=`cat "$FILE" | awk '/^bloc_paravance/ {for(i=2;i<NF;i++) printf("%s ",$i); printf("%s\n",$NF) }'`
	if [ -z "$BLOC_PARAVANCE2" ]; then
	  BLOC_PARAVANCE2=$BLOC_PARAVANCE
	fi
	MAX_PARAVANCE2=`cat "$FILE" | awk '/^max_paravance/ {for(i=2;i<NF;i++) printf("%s ",$i); printf("%s\n",$NF) }'`
	if [ -z "$MAX_PARAVANCE2" ]; then
	  MAX_PARAVANCE2=$MAX_PARAVANCE
	fi
	TYPE2=`cat "$FILE" | awk '/^type/ {for(i=2;i<NF;i++) printf("%s ",$i); printf("%s\n",$NF) }'`
	if [ -z "$TYPE2" ]; then
	  TYPE2=$TYPE
	fi
	FILTER2=`cat "$FILE" | awk '/^filter/ {for(i=2;i<NF;i++) printf("%s ",$i); printf("%s\n",$NF) }'`
	if [ -z "$FILTER2" ]; then
	  FILTER2=$FILTER
	fi
}

write_info()
{
	echo -n "Génération du fichier : "

	echo '# Fichier de configuration généré par dinoconfig' > $FILE
	echo "# N'hésitez pas à l'éditer à la main" >> $FILE
	echo >> $FILE
	if [ $global = 0 ]; then
	  echo "# Informations sur l'utilisateur" >> $FILE
	  if [ -z "$LOGIN2" ]; then
	    echo -n ""
	  elif [ "$LOGIN" != "$LOGIN2" ]; then
	    echo "login: $LOGIN2" >> $FILE
	  fi
	  if [ -z "$PASSWORD2" ]; then
	    echo -n ""
	  elif [ "$PASSWORD" != "$PASSWORD2" ]; then
	    echo "password: $PASSWORD2" >> $FILE
	  fi
	  if [ -z "$NICK2" ]; then
	    echo -n ""
	  elif [ "$NICK" != "$NICK2" ]; then
	    echo "nick: $NICK2" >> $FILE
	  fi
	fi
	if [ -z "$SERVEUR2" ]; then
	  echo -n ""
	elif [ "$SERVEUR" != "$SERVEUR2" ]; then
	  echo >> $FILE
	  echo '# Serveur pour faire la connexion' >> $FILE
	  echo "serveur: $SERVEUR2" >> $FILE
	fi
	if [ -z "$PORT2" ]; then
	  echo -n ""
	elif [ "$PORT" != "$PORT2" ]; then
	  echo "port: $PORT2" >> $FILE
	fi
	if [ -z "$DEFAUT_SIGNE2" ]; then
	  echo -n ""
	elif [ "$DEFAUT_SIGNE" != "$DEFAUT_SIGNE2" ]; then
	  echo >> $FILE
	  echo '# Signer les messages par défaut' >> $FILE
	  echo "defaut_signe: $DEFAUT_SIGNE2" >> $FILE
	fi
	if [ -z "$CACHE2" ]; then
	  echo -n ""
	elif [ "$CACHE" != "$CACHE2" ]; then
	  echo >> $FILE
	  echo '# Rester caché' >> $FILE
	  echo "cache: $CACHE2" >> $FILE
	fi
	if [ -z "$LIRE_CENSURE2" ]; then
	  echo -n ""
	elif [ "$LIRE_CENSURE" != "$LIRE_CENSURE2" ]; then
	  echo >> $FILE
	  echo '# Lire les messages censurés' >> $FILE
	  echo "lire_censure: $LIRE_CENSURE2" >> $FILE
	fi
	if [ -z "$EDITEUR2" ]; then
	  echo -n ""
	elif [ "$EDITEUR" != "$EDITEUR2" ]; then
	  echo >> $FILE
	  echo '# Éditeur pour la composition' >> $FILE
	  echo "editeur: $EDITEUR2" >> $FILE
	fi
	if [ -z "$BROWSER2" ]; then
	  echo -n ""
	elif [ "$BROWSER" != "$BROWSER2" ]; then
	  echo >> $FILE
	  echo '# Navigateur web' >> $FILE
	  echo "browser: $BROWSER2" >> $FILE
	fi
	if [ -z "$REPONSE2" ]; then
	  echo -n ""
	elif [ "$REPONSE" != "$REPONSE2" ]; then
	  echo >> $FILE
	  echo '# En-tête des réponses de messages' >> $FILE
	  echo "reponse: $REPONSE2" >> $FILE
	fi
	if [ -z "$FILTER2" ]; then
	  echo -n ""
	elif [ "$FILTER" != "$FILTER2" ]; then
	  echo >> $FILE
	  echo '# Filter avec la blacklist/whitelist' >> $FILE
	  echo "filter: $FILTER2" >> $FILE
	fi
# Paramètres avancés
	echo >> $FILE
	if [ -z "$RECONNEXION2" ]; then
	  echo -n ""
	elif [ "$RECONNEXION" != "$RECONNEXION2" ]; then
	  echo >> $FILE
	  echo '# Maintien de la connexion' >> $FILE
	  echo "reconnexion: $RECONNEXION2" >> $FILE
	fi
	if [ -z "$TIMEOUT2" ]; then
	  echo -n ""
	elif [ "$TIMEOUT" != "$TIMEOUT2" ]; then
	  echo "timeout: $TIMEOUT2" >> $FILE
	fi
	if [ -z "$TAILLE_CACHE2" ]; then
	  echo -n ""
	elif [ "$TAILLE_CACHE" != "$TAILLE_CACHE2" ]; then
		  echo >> $FILE
	  echo '# Paramètres du cache' >> $FILE
	  echo "taille_cache: $TAILLE_CACHE" >> $FILE
	fi
	if [ -z "$BLOC_PARAVANCE2" ]; then
	  echo -n ""
	elif [ "$BLOC_PARAVANCE" != "$BLOC_PARAVANCE2" ]; then
	  echo "bloc_paravance: $BLOC_PARAVANCE" >> $FILE
	fi
	if [ -z "$MAX_PARAVANCE2" ]; then       
	  echo -n ""            
	elif [ "$MAX_PARAVANCE" != "$MAX_PARAVANCE2" ]; then
	  echo "max_paravance: $MAX_PARAVANCE" >> $FILE
	fi
	if [ -z "$TYPE2" ]; then
	  echo -n ""
	elif [ "$TYPE" != "$TYPE2" ]; then
	  echo '# Type des messages postés :' >> $FILE
	  echo '#                            0 HTML brut' >> $FILE
	  echo '#                            1 HTML avec vérification' >> $FILE
	  echo '#                            2 Texte brut' >> $FILE
	  echo "type: $TYPE" >> $FILE
	fi

	echo "$FILE"
}

read_value()
{
    echo "$1 [$2] :"
    read REP
    if [ "$REP" = "" ]; then
	REP="$2"
    fi
    if [ "$REP" = "ERASE" ]; then
	REP=""
    fi
}

read_choix()
{
    n1="$1" ; n2="$2"
    shift; shift
    while : ; do
	echo "$n1 [$n2]"
	echo "Vous devez choisir entre :"
	num=0;
	for ch in $@ ; do
	    echo "$num) $ch"
	    num=`expr $num + 1`
	done
	read i
	if [ "$i" = "" ]; then
	    REP="$n2"
	    return 0
	fi
	num=0;
	for ch in $@ ; do
	    if [ "$i" = $num ]; then
		REP="$num"
		return 0
	    fi
	    num=`expr $num + 1`
	done
	echo
	echo "Réponse invalide, veuillez corriger"
    done
}

yesno_() 
{
    echo -n -e $1
    if [ "$2" = 1 ]; then
	echo " [O/n] ? "
    else
	echo " [o/N] ? "
    fi

    read line;
    until test "$line" = O -o "$line" = Y -o "$line" = N \
	-o "$line" = o -o "$line" = y -o "$line" = n -o "$line" = "";
    do echo Répondez par "O" ou "N" suivis de la touche entrée.
    read line; done

    if test "$line" = O -o "$line" = o -o "$line" = Y -o "$line" = y -o \
	\( "$line" = "" -a "$2" = 1 \); then
	return 0
    else
	return 1
    fi
}

yesno()
{
    yesno_ "$1" 1
}

noyes()
{
    yesno_ "$1" 0
}

read_info()
{
    echo
    echo "Ce script va générer le fichier de configuration $FILE"
    echo "  Les valeurs entre crochet telle que [donnée] seront utilisées"
    echo "si vous tapez directement la touche [Entrée]"
    echo "--> Pour effacer une donnée, introduire la valeur 'ERASE'"
    echo

    if [ $global = 0 ]; then
      read_value "Votre login" "$LOGIN2"
      if [ "$REP" != "$LOGIN2" ]; then
	LOGIN2="$REP"
	read_value "Votre mot de passe (attention, il apparaît en clair)" "carotte"
	PASSWORD2="$REP"
      fi
      if [ -z "$NICK2" ]; then
        NICK2=$LOGIN2
      fi
      read_value "Surnom" "$NICK2"
      NICK2="$REP"
    fi

    read_value "Serveur pour la connexion" "$SERVEUR2"
    SERVEUR2="$REP"
    read_value "Port TCP de la connexion" "$PORT2"
    PORT2="$REP"
    if yesno_ "Signer vos messages par défaut" $DEFAUT_SIGNE2 ; then
      DEFAUT_SIGNE2=1
    else
      DEFAUT_SIGNE2=0
    fi
    if yesno_ "Resté caché en vous connectant" $CACHE2 ; then
      CACHE2=1
    else
      CACHE2=0
    fi
    if yesno_ "Lire les messages censurés" $LIRE_CENSURE2 ; then
      LIRE_CENSURE2=1
    else
      LIRE_CENSURE2=0
    fi
    read_value "Éditeur pour la composition des messages" "$EDITEUR2"
    EDITEUR2="$REP"
    read_value "Navigateur pour lire les adresses internet" "$BROWSER2"
    BROWSER2="$REP"
    read_value "En-tête pour les réponses aux messages" "$REPONSE2"
    REPONSE2="$REP"
    if yesno_ "Filtrer les messages avec un dispositif blacklist/whitelist" $FILTER2 ; then
      FILTER2=1
    else
      FILTER2=0
    fi

    if noyes "Éditer les paramètres avancés" ;  then
      read_value "Intervalle en minutes entre deux vérifications de nouveaux messages" "$RECONNEXION2"
      RECONNEXION2="$REP"
      read_value "Temps en minutes au bout duquel on ne vérifie plus" "$TIMEOUT2"
      TIMEOUT2="$REP"
      read_value "Taille du cache en messages" "$TAILLE_CACHE2"
      TAILLE_CACHE2="$REP"
      read_value "Taille des blocs de message à lire en avance" "$BLOC_PARAVANCE2"
      BLOC_PARAVANCE2="$REP"
      read_value "Nombre maximal de messages à lire par avance" "$MAX_PARAVANCE2"
      MAX_PARAVANCE2="$REP"
      read_choix "Type des messages postés" "$TYPE2" "HTML_Brut" "HTML_avec_vérification" "Texte_brut"
      TYPE2="$REP"
    fi
    echo
}

do_options $@

echo "Bienvenue dans dinoconfig"

get_default
get

read_info

if yesno "Enregistrer ces informations" ; then
write_info
fi
