#!/bin/bash
# $Id: eu_config_bash,v 1.5 2004/05/30 01:54:30 Tux Exp $
# Name: eu_config_bash
# Goal: apply configuration to the system
# Author: Tux
# Params:

. /etc/eagle-usb/scripts/setvars

for param in $* ; do
	if [ $i -gt 0 ] ; then
 			param=${param//--help/-h}
 			param=${param//--lang=/-l}
 			param=${param//--expert/-e}
 			PARAMS="$PARAMS $param"
 	fi
 	let i++
done
evalParams() {
	while getopts "hl:e" opt; do
		case $opt in
			h  )	echo -e $EAGLECONFIG_USAGE_MSG ; exit 0 ;;
			l  )	EU_LANG="$OPTARG" ;;
			e  )	EXPERT=1 ;;
			\? )	echo -e $EAGLECONFIG_USAGE_MSG ; exit 1 ;;
		esac
	done
}
#FORCE_LANG="$EU_LANG"
EXPERT=0
evalParams $PARAMS
setStrings # necessary to take in account $LANG parameter

echo -e "\n\n${SEP_MSG}"
echo -e "${GREETING_MSG}"
echo -e "${SEP_MSG}"
echo -e "${INFO_MSG}"

# Connexion details
echo -e $ISP_LIST_MSG
echo -en \
"------------------------------------------------------------------\n"\
"1:  Belgium      Belgacom                08  23  06  PPPoA VCmux  \n"\
"2:  Deutschland  DT (D.Telecom, 1&1...)  01  20  01  PPPoE LLC    \n"\
"3:  Espaa       Telefonica              08  20  01  PPPoE LLC    \n"\
"4:  Espaa       Telefonica ip fija      08  20  03  ROUTED IP LLC\n"\
"5:  Espaa       Retevision, Eresmas...  08  23  06  PPPoA VCmux  \n"\
"6:  Espaa       Tiscali                 01  20  ??  ?            \n"\
"7:  Finland      Sonera                  08  23  01  PPPoE LLC    \n"\
"8:  France       FT (Free, Wanadoo...)   08  23  06  PPPoA VCmux  \n"\
"9:  France       FT (Free, Wanadoo...)   08  23  01  PPPoE LLC    \n"\
"10: France       Tiscali 128k            08  23  05  PPPoA LLC    \n"\
"11: France       Free dgroup           08  24  04  ROUTED IP VC \n"\
"12: France       9telecom dgroup       08  23  06  PPPoA VCmux  \n"\
"13: Hungary      Matav                   01  20  01  PPPoE LLC    \n"\
"14: Italia       Telecom Italia          08  23  06  PPPoA VCmux  \n"\
"15: Italia       MClink                  08  4B  ??  ?            \n"\
"16: Netherlands  KPN                     08  30  06  PPPoA VCmux  \n"\
"17: Polska       Telekomunikacja Polska  00  23  06  PPPoA VCmux  \n"\
"18: Portugal     PT                      00  23  01  PPPoE LLC    \n"\
"19: Sweden       Telia                   08  23  01  PPPoE LLC    \n"\
"20: UK           BT (Tiscali...)         00  26  06  PPPoA VCmux  \n"\
"------------------------------------------------------------------\n"\
"?"

isp_to_no $ISP
if [ $NOISP != 0 ] ; then
	echo -n "[$NOISP]"
fi

read REP

if [ -z "$REP" ] ; then REP=$NOISP ; fi
LINETYPE="00000001"
if [ $REP == 1 ] ; then		VPI="00000008" ; VCI="00000023" ; ENC="00000006" ; ISP="BE"
elif [ $REP == 2 ] ; then	VPI="00000001" ; VCI="00000020" ; ENC="00000001" ; LINETYPE="00000002" ; ISP="DE"
elif [ $REP == 3 ] ; then	VPI="00000008" ; VCI="00000020" ; ENC="00000001" ; ISP="ESTE"
elif [ $REP == 4 ] ; then	VPI="00000008" ; VCI="00000020" ; ENC="00000003" ; ISP="ESTEIPF"
elif [ $REP == 5 ] ; then	VPI="00000008" ; VCI="00000023" ; ENC="00000006" ; ISP="ESRE"
elif [ $REP == 6 ] ; then	VPI="00000001" ; VCI="00000020" ; ENC="????????" ; ISP="ESTIS"
elif [ $REP == 7 ] ; then	VPI="00000008" ; VCI="00000023" ; ENC="00000001" ; ISP="FI"
elif [ $REP == 8 ] ; then	VPI="00000008" ; VCI="00000023" ; ENC="00000006" ; ISP="FRFTA"
elif [ $REP == 9 ] ; then	VPI="00000008" ; VCI="00000023" ; ENC="00000001" ; ISP="FRFTE"
elif [ $REP == 10 ] ; then	VPI="00000008" ; VCI="00000023" ; ENC="00000005" ; ISP="FRTIS128"
elif [ $REP == 11 ] ; then	VPI="00000008" ; VCI="00000024" ; ENC="00000004" ; ISP="FRFREEDEG"
elif [ $REP == 12 ] ; then	VPI="00000008" ; VCI="00000023" ; ENC="00000006" ; ISP="FR9"
elif [ $REP == 13 ] ; then	VPI="00000001" ; VCI="00000020" ; ENC="00000001" ; ISP="HU"
elif [ $REP == 14 ] ; then	VPI="00000008" ; VCI="00000023" ; ENC="00000006" ; ISP="IT"
elif [ $REP == 15 ] ; then	VPI="00000008" ; VCI="0000004B" ; ENC="????????" ; ISP="ITMC"
elif [ $REP == 16 ] ; then	VPI="00000008" ; VCI="00000030" ; ENC="00000006" ; ISP="NE"
elif [ $REP == 17 ] ; then	VPI="00000000" ; VCI="00000023" ; ENC="00000006" ; ISP="PL"
elif [ $REP == 18 ] ; then	VPI="00000000" ; VCI="00000023" ; ENC="00000001" ; ISP="PO"
elif [ $REP == 19 ] ; then	VPI="00000008" ; VCI="00000023" ; ENC="00000001" ; ISP="SW"
elif [ $REP == 20 ] ; then	VPI="00000000" ; VCI="00000026" ; ENC="00000006" ; ISP="UK"
else
	echo -e $WRONG_CHOICE_MSG
	exit 1
fi

if [ "$VPI" = "????????" ] || [ "$VCI" = "????????" ] || [ "$ENC" = "????????" ] ; then
	echo -e "\nThere are missing data for your ISP.\nPlease notify this on http://www.eagle-usb.fr.st\n"
	exit 1
fi

encapsToPppox "$ENC"

if [ "$PPPOX" = "none" ] ; then
# ================== WITHOUT PPPD =====================
	echo -en $STATIC_IP_MSG
	read STATIC_IP
	if [ -z "$STATIC_IP" ] ; then STATIC_IP="none" ; fi
	if [ "$STATIC_IP" != "none" ] ; then
		if [ -e /etc/resolv.conf ] ; then
			echo -e "\nYour /etc/resolv.conf contains the following lines:"
			echo $SEP_MSG
			cat -s /etc/resolv.conf
			echo $SEP_MSG
			echo -e "You can modify this file manually if it contains wrong DNS."
		else
			echo -e "\nwarning: /etc/resolv.conf doesn't exist!"
		fi
	fi
else
# =================== WITH PPPD =======================
	#STATIC_IP="none"
	# Search for existing User&Password
	OLD_PWD_FOUND=0
	if [ -e $PPP_OPTIONS_ADSL ] ; then
		# the user name has to be in /etc/ppp/peers/adsl and (pap-secrets or chap-secrets)
		NAME_IN_OPTIONS=`grep user $PPP_OPTIONS_ADSL | cut -d ' ' -f2 | sed 's_"__g'`
		if [ ! -z $NAME_IN_OPTIONS ] ; then
			if [ -e $PPP_DIR/pap-secrets ] && grep -q "$NAME_IN_OPTIONS" $PPP_DIR/pap-secrets ; then
				OLD_PWD_FOUND=1
			fi
			if [ -e $PPP_DIR/chap-secrets ] && grep -q "$NAME_IN_OPTIONS" $PPP_DIR/chap-secrets ; then
				OLD_PWD_FOUND=1
			fi
		fi
	fi
	# Old password found?
	if [ $OLD_PWD_FOUND == 1 ] ; then
		if ! confirm "$KEEP_OLD_PWD_MSG" 1 ; then
			OLD_PWD_FOUND=0
		fi
	fi
	# No User/Password or don't want to keep existing
	if [ $OLD_PWD_FOUND == 0 ] ; then
		# Ask for user and password:

		echo -e $LOGIN_MSG
		read ISP_LOGIN
		echo -e $PWD_MSG
		read -s ISP_PWD
		# Ask for data encryption or not
		if confirm "$PWD_ENCRYPT_MSG" 1 ; then
			PWD_ENCRYPT=1
		else
			PWD_ENCRYPT=0
		fi
	else
		ISP_LOGIN="keep"
		ISP_PWD="keep"
		PWD_ENCRYPT="keep"
	fi
fi

# === init.d ===
if confirm "$START_ON_BOOT_MSG" $START_ON_BOOT ; then
	START_ON_BOOT=1
else
	START_ON_BOOT=0
fi

if [ $EXPERT == 1 ] ; then
	# === testconnec ===
	if confirm "$USE_TESTCONNEC_MSG" $USE_TESTCONNEC ; then
		USE_TESTCONNEC=1
	else
		USE_TESTCONNEC=0
	fi
	# === virtual interface name ===
	echo -e $FORCE_INTERFACE_MSG
	echo -n "[$FORCE_IF]"
	read REP
	if [ "x$REP" = "xauto" ] ; then
		FORCE_IF="auto"
	elif [ ! -z "$REP" ] ; then
		FORCE_IF="$REP"
	fi
fi
# --------------------------------------------------------

LSTOPTIONS="LINETYPE=$LINETYPE|VPI=$VPI|VCI=$VCI|ENC=$ENC|ISP=$ISP|ISP_LOGIN=$ISP_LOGIN|ISP_PWD=$ISP_PWD"
LSTOPTIONS="$LSTOPTIONS|PWD_ENCRYPT=$PWD_ENCRYPT|STATIC_IP=$STATIC_IP|START_ON_BOOT=$START_ON_BOOT"
LSTOPTIONS="$LSTOPTIONS|USE_TESTCONNEC=$USE_TESTCONNEC|EU_LANG=$EU_LANG|EXPERT=$EXPERT|FORCE_IF=$FORCE_IF"
exec eagleconfig $PARAMS --params="$LSTOPTIONS"


#***************************************************************************
# $Log: eu_config_bash,v $
# Revision 1.5  2004/05/30 01:54:30  Tux
# - vars name "FAI..." => "ISP..."
#
# Revision 1.4  2004/05/24 21:26:35  Tux
# - wrong variable name (LT => LINETYPE)
#
# Revision 1.3  2004/05/23 20:02:28  Tux
# - ask for virtual interface name (expert mode only)
# - use "exec" to call eagleconfig
#
# Revision 1.2  2004/04/18 09:47:27  Tux
# - added expert param
#
# Revision 1.1  2004/04/04 18:48:51  Tux
# Initial revision
#
#***************************************************************************/
