#!/bin/sh

# Messages:
GREETING="Gnration des fichiers de configuration contenant les paramtres de connexion."
ROOTMSG="Vous devez tre root pour excuter ce script !"
LOGINMSG="\nVeuillez entrer le nom d'utilisateur pour la connexion au fournisseur d'accs :"
PWDMSG="\nVeuillez entrer le mot de passe pour cet utilisateur :"
GENERMSG="\nGnration des fichiers de configuration russie.\n"
USEINITTABMSG="\nVoulez vous que la connexion soit lance  chaque dmarrage?\no/[n]"
USEINITTABMSGYES="\nVotre fichier /etc/inittab a t mis  jour. En cas de problme,\nune copie a t place dans /etc/inittab.old\n"
USEINITTABMSGNO="\nPour lancer la connexion au dmarrage, il vous suffira d'ajouter\nle contenu de /etc/inittab.sample dans /etc/inittab.\n"
KEEPOLDPWDMSG="\nUn login et mot de passe FreeAdsl ont t trouvs dans le fichier de configuration de ppp.\nVoulez-vous conserver ce login/mot de passe ?\n[o]/n"
PWDENCRYPTMSG="\nVotre fournisseur d'accs supporte t il le cryptage du mot de passe?\n[o]/n"
DEGROUPEMSG="\nEtes vous dgroup?\no/[n]"

# Paths:
HOTPLUGDIR=/etc/hotplug
PPPDIR=/etc/ppp

if [ -e /usr/sbin/update-usb.usermap ]; then
	USBUSERMAP=usb.usermap.local
else
	USBUSERMAP=usb.usermap
fi

# Install script:
echo
echo $GREETING
echo

# Check we are root:
if [ `id -ur` != 0 ] ; then
	echo $ROOTMSG
	echo
	exit 1
fi

echo -en $DEGROUPEMSG
read REP
DEGROUPE=0
if [ -z $REP ] ; then REP="-" ; fi
if [ $REP = "o" ] ; then
	DEGROUPE=1
fi

if [ $DEGROUPE = 0 ] ; then

	# Ask for user and password:
	OLDPWDFOUND=0
	if [ -f $PPPDIR/chap-secrets ] ; then
		if grep -q freeadsl $PPPDIR/chap-secrets ; then OLDPWDFOUND=1; fi
	fi
	if [ -f $PPPDIR/pap-secrets ] ; then
		if grep -q freeadsl $PPPDIR/pap-secrets ; then OLDPWDFOUND=1; fi
	fi
	if [ -f $PPPDIR/options.adsl ] ; then
		if ! grep -q freeadsl $PPPDIR/options.adsl ; then OLDPWDFOUND=0; fi
	fi
	# Old password found?
	if [ $OLDPWDFOUND = 1 ] ; then
		echo -en $KEEPOLDPWDMSG
		read REP
		if [ -z $REP ] ; then REP="-" ; fi
		if [ $REP = "n" ]; then
			OLDPWDFOUND=0
		fi
	fi
	if [ $OLDPWDFOUND = 0 ] ; then
		echo -e $LOGINMSG
		read FAILOGIN
		echo
		echo -e $PWDMSG
		read -s FAIPWD
		echo
		echo "user \"$FAILOGIN\"" > options.adsl
		cat ppp/options >> options.adsl
	fi
	echo "user \"adsl@adsl\"" > options.mire
	cat ppp/options >> options.mire

	# Now we must be carefull:
	if [ $OLDPWDFOUND = 0 ] ; then
		umask 0077

		echo -e $PWDENCRYPTMSG
		read REP
		if [ -z $REP ] ; then REP="-" ; fi
		PWDENCRYPT=1
		if [ $REP = "n" ] ; then
			PWDENCRYPT=0
		fi

		if [ $PWDENCRYPT = 0 ] ; then
			if [ -f $PPPDIR/pap-secrets ] ; then
				grep -v "$FAILOGIN" $PPPDIR/pap-secrets > pap-secrets
			else
				cp ppp/pap-secrets .
			fi
			echo "$FAILOGIN	*	$FAIPWD	*" >> pap-secrets
			if ! grep -q "adsl@adsl" pap-secrets ; then
				echo "adsl@adsl	*	adsl	*" >> pap-secrets
			fi
		else
			if [ -f $PPPDIR/chap-secrets ] ; then
				grep -v "$FAILOGIN" $PPPDIR/chap-secrets > chap-secrets
			else
				cp ppp/chap-secrets .
			fi
			echo "$FAILOGIN	*	$FAIPWD	*" >> chap-secrets
			if ! grep -q "adsl@adsl" chap-secrets ; then
				echo "adsl@adsl	*	adsl	*" >> chap-secrets
			fi
		fi
		umask 0022
	fi

fi

echo $DEGROUPE > /etc/eagleusb.conf

# hotplug
if [ -f $HOTPLUGDIR/usb.usermap ] ; then
	grep -v adiusb $HOTPLUGDIR/$USBUSERMAP > $USBUSERMAP
	cat hotplug/usb.usermap >> $USBUSERMAP
else
	cp hotplug/usb.usermap ./$USBUSERMAP
fi

# inittab
grep -v adsl.inittab /etc/inittab > /etc/inittab.temp
if [ ! -f /etc/inittab.old ] ; then
	mv /etc/inittab /etc/inittab.old
fi
mv -f /etc/inittab.temp /etc/inittab

echo -en $USEINITTABMSG
read REP
USEINITTAB=0
if [ -z $REP ] ; then REP="-" ; fi
if [ $REP = "o" ] ; then USEINITTAB=1; fi

if [ $USEINITTAB = 1 ] ; then
	echo "#adsl.inittab:"$INITTABMSG >> /etc/inittab
	echo "adsl:2345:respawn:/usr/sbin/adsl.inittab" >> /etc/inittab
	/sbin/telinit Q
	echo -e $USEINITTABMSGYES>message
else
	echo "#adsl.inittab:"$INITTABMSG >> /etc/inittab.sample
	echo "adsl:2345:respawn:/usr/sbin/adsl.inittab" >> /etc/inittab.sample
	echo -e $USEINITTABMSGNO>message
fi

# OK, all files are written:
echo -e $GENERMSG
