#!/bin/bash
# $Id: eagleconfig,v 1.20 2004/06/06 20:47:15 Tux Exp $
# Name: eagleconfig
# Goal: configuration utility for Eagle-USB modems
# Author: Tux
# Params:
#	h = display help
#	l = choose language (keep, auto or [LL])
#		keep => use previous language (read $LANG in eagle-usb.conf)
#		auto => use system language (given by $LANG var)
#		LL   => language you want to use (en, fr, pl...)
#	e = expert mode (additional choices)
# Params set by setvars:
#	USE_HOTPLUG=1		=> use hotplug
#	USE_IFUPDOWN=1		=> use ifup/ifdown scripts instead of ifconfig
#	SIMPLE=1		=> don't use hotplug nor ifup/ifdown
# for param in "$@" ; do
# 	param=${param--help/-h}
# 	param=${param--lang=/-l}
# 	param=${param--expert/-e}
# 	shift ; set -- "$@" "$param"
# done

. /etc/eagle-usb/scripts/setvars

SERVICE_MISSING_MSG="\nwarning: 'internet' service is not present, connection on boot is disabled.\n"\
"You should try to install eagle-usb package first."
CRON_MISSING_MSG="\nwarning: /etc/cron.d does not exist. testconnec is disabled."

for param in $* ; do
	if [ $i -gt 0 ] ; then
			param=${param//--help/-h}
			param=${param//--params=/-p}
			# params for eu_config_*
 			param=${param//--lang=/-l}
 			param=${param//--expert/-e}
			PARAMS="$PARAMS $param"
	fi
	let i++
done

evalParams() {
	while getopts "hp:l:e" opt; do
		case $opt in
			h  )	echo -e $EAGLECONFIG_USAGE_MSG ; exit 0 ;;
			p  )	LSTOPTIONS="$OPTARG" ;;
			# params for eu_config_*
			l  )	;;
			e  )	;;
			\? )	echo -e $EAGLECONFIG_USAGE_MSG ; exit 1 ;;
		esac
	done
}
EXPERT=0
TESTCONNEC_DELAY=2	# check every 2min
LSTOPTIONS=""
evalParams $PARAMS

SEND_DSP_NEEDED=0

if [ -z $LSTOPTIONS ] ; then
	exec $EU_SCRIPT_DIR/eu_config_bash "$@"
	# => exit
fi

# params are already initialized by setvars
# can be the real value or "keep"
OLD_IFS=$IFS
IFS=\|
for OPT in $LSTOPTIONS ; do
	NAME=`echo $OPT | cut -d "=" -f1`
	VALUE=`echo $OPT | cut -d "=" -f2`
	OLDNAME=$`echo $NAME`
	OLDVAL=`eval echo $OLDNAME`
	#echo "NAME=$NAME # OLDVAL=$OLDVAL # VALUE=$VALUE"
	case $NAME in
		"LINETYPE" | "VPI" | "VCI" | "ENC" | "ISP" | "FORCE_IF" )
			if [ "x$VALUE" != "xkeep" ] && [ "x$VALUE" != "x$OLDVAL" ]; then
				SEND_DSP_NEEDED=1
				eval $NAME=$VALUE
			fi
			;;
		"EU_LANG" )
			if [ "x$VALUE" != "xkeep" ]; then
				eval $NAME=$VALUE
				setStrings # necessary to take in account $LANG parameter
			fi
			;;
		* )
			if [ "x$VALUE" != "xkeep" ]; then
				eval $NAME="$VALUE"
			fi
			;;
	esac
done
IFS=$OLD_IFS
#echo "VPI=$VPI VCI=$VCI ENC=$ENC ISP=$ISP ISP_LOGIN=$ISP_LOGIN"
#echo "ISP_PWD=$ISP_PWD PWD_ENCRYPT=$PWD_ENCRYPT STATIC_IP=$STATIC_IP"
#echo "USE_TESTCONNEC=$USE_TESTCONNEC EU_LANG=$EU_LANG EXPERT=$EXPERT"
#echo "FORCE_IF=$FORCE_IF SEND_DSP_NEEDED=$SEND_DSP_NEEDED"

# ============== APPLY CHANGES TO SYSTEM =================
# should /etc/eagle-usb/eagle-usb.conf be created?
if [ ! -e $EU_DIR/eagle-usb.conf ] ; then
	#echo -e "\nThe file $EU_DIR/eagle-usb.conf is missing!\nYou should reinstall this driver!\n"
	cp $EU_SCRIPT_DIR/eagle-usb.conf.template $EU_DIR/eagle-usb.conf
fi
if ! grep -q "STATIC_IP=" $EU_DIR/eagle-usb.conf ; then
	echo "STATIC_IP=" >> $EU_DIR/eagle-usb.conf
fi
if ! grep -q "ISP=" $EU_DIR/eagle-usb.conf ; then
	echo "ISP=" >> $EU_DIR/eagle-usb.conf
fi
if ! grep -q "LANG=" $EU_DIR/eagle-usb.conf ; then
	echo "LANG=" >> $EU_DIR/eagle-usb.conf
fi
if ! grep -q "ASYNCHRONOUS_START=" $EU_DIR/eagle-usb.conf ; then
	echo "ASYNCHRONOUS_START=1" >> $EU_DIR/eagle-usb.conf
fi

# language, VPI, VCI, encapsulation
cat $EU_DIR/eagle-usb.conf | sed \
	-e "s|STATIC_IP *=.*|STATIC_IP=$STATIC_IP|g" \
	-e "s|ISP *=.*|ISP=$ISP|g" \
	-e "s|LANG *=.*|LANG=$EU_LANG|g" \
	-e "s|Linetype *=.*|Linetype=$LINETYPE|g" \
	-e "s|VCI *=.*|VCI=$VCI|g" \
	-e "s|VPI *=.*|VPI=$VPI|g" \
	-e "s|Encapsulation *=.*|Encapsulation=$ENC|g" > $EU_DIR/tmp
cat $EU_DIR/tmp > $EU_DIR/eagle-usb.conf

# $ENC => $PPPOX
encapsToPppox "$ENC"

if [ "$PPPOX" = "none" ] ; then
	if [ "$STATIC_IP" = "none" ] ; then
	# degroup - dhcp
		# if /etc/resolv.conf is a link (probably -> /etc/ppp/resolv.conf), it is removed
		if [ -h /etc/resolv.conf ] ; then
			rm -f /etc/resolv.conf
		fi
	fi
else
	# ensure that /etc/ppp/peers exists
	mkdir -p $PPP_OPTIONS_DIR
	touch $PPP_OPTIONS_ADSL
	touch $PPP_OPTIONS_MIRE
	if [ "$ISP_LOGIN" = "keep" ] ; then
	# keep user/password
		# does /etc/ppp/peers need to be updated?
		NEED_UPDATE=0
		if grep -q "/usr/sbin/adictrl" $PPP_OPTIONS_ADSL ; then
			NEED_UPDATE=1
		fi
		if ! grep -q "$SBIN_DIR/fctStartAdsl" $PPP_OPTIONS_ADSL ; then
			NEED_UPDATE=1
		fi
		if [ $NEED_UPDATE == 1 ] ; then
			# remove outdated or unnecessary lines
			cat $PPP_OPTIONS_ADSL | \
				grep -v "adictrl" | \
				grep -v "eaglectrl" | \
				grep -v "kdebug" | \
				grep -v "noccp" | \
				grep -v "holdoff" | \
				grep -v "maxfail" > $EU_DIR/tmp
			cat $EU_DIR/tmp > $PPP_OPTIONS_ADSL
			# then add the new line
			echo "pty \"$SBIN_DIR/pppoa -I \`$SBIN_DIR/fctStartAdsl -i\`\"" >> $PPP_OPTIONS_ADSL	# `
		fi
	else
	# user/password not found or user wants to change there
		echo "user \"$ISP_LOGIN\"" > $PPP_OPTIONS_ADSL
		echo "pty \"$SBIN_DIR/pppoa -I \`$SBIN_DIR/fctStartAdsl -i\`\"" >> $PPP_OPTIONS_ADSL	# `
		cat $EU_SCRIPT_DIR/options >> $PPP_OPTIONS_ADSL
		# Now we must be careful:
		umask 0077
		if [ $PWD_ENCRYPT == 0 ] ; then
			PAP_CHAP="pap"
		else
			PAP_CHAP="chap"
		fi
		if [ -f $PPP_DIR/$PAP_CHAP-secrets ] ; then
			grep -v "freeadsl" $PPP_DIR/$PAP_CHAP-secrets > $PPP_DIR/$PAP_CHAP-secrets.temp
			grep -v "$ISP_LOGIN" $PPP_DIR/$PAP_CHAP-secrets.temp > $PPP_DIR/$PAP_CHAP-secrets
			rm -f $PPP_DIR/$PAP_CHAP-secrets.temp
		else
			echo "# Secrets for authentication using $PAPCHAP" > $PPP_DIR/$PAP_CHAP-secrets
			echo "# client        server  secret          IP addresses" >> $PPP_DIR/$PAP_CHAP-secrets
			echo "# userlogin     *       password        *" >> $PPP_DIR/$PAP_CHAP-secrets
		fi
		echo "$ISP_LOGIN *       $ISP_PWD *" >> $PPP_DIR/$PAP_CHAP-secrets
		# mire
		if ! grep -q "adsl@adsl" $PPP_DIR/$PAP_CHAP-secrets ; then
			echo "adsl@adsl	*	adsl	*" >> $PPP_DIR/$PAP_CHAP-secrets
		fi
		umask 0022
		echo "user \"adsl@adsl\"" > $PPP_OPTIONS_MIRE
		echo "pty \"$SBIN_DIR/pppoa -I \`$SBIN_DIR/fctStartAdsl -i\`\"" >> $PPP_OPTIONS_MIRE	# `
		cat $EU_SCRIPT_DIR/options >> $PPP_OPTIONS_MIRE
	fi

	# DNS
	if [ "$DISTRIB" = "Mandrake" ] ; then
		# Mandrake use the ifup script to update /etc/resolv.conf
		touch /etc/resolv.conf
		chmod a+r /etc/resolv.conf
	else
		# if resolv.conf exists and is not a link, it is saved
		if [ -e /etc/resolv.conf ] && [ ! -h /etc/resolv.conf ] ; then
			cat /etc/resolv.conf >> /etc/resolv.conf.old
		fi
		ln -sf $PPP_DIR/resolv.conf /etc/resolv.conf
	fi

	# to allow "pppd call adsl", adsl must contains the right app
	sed "s|/pppo[a-z]|/$PPPOX|" $PPP_OPTIONS_ADSL > $EU_DIR/tmp
	cat $EU_DIR/tmp > $PPP_OPTIONS_ADSL
	if [ -e $PPP_OPTIONS_MIRE ] ; then
		sed "s|/pppo[a-z]|/$PPPOX|" $PPP_OPTIONS_MIRE > $EU_DIR/tmp
		cat $EU_DIR/tmp > $PPP_OPTIONS_MIRE
	fi
fi
touch /etc/resolv.conf


# === hotplug ===
if [ $USE_HOTPLUG == 1 ] && [ $SIMPLE == 0 ] ; then
	ln -sf $EU_SCRIPT_DIR/usb.usermap $HOTPLUG_SCRIPT_DIR/eagle-usb.usermap
	ln -sf $EU_SCRIPT_DIR/eu_dsp $HOTPLUG_SCRIPT_DIR/eagle-usb
fi


# === init.d ===
if [ "$DISTRIB" = "Fedora" ] || [ "$DISTRIB" = "Redhat" ] || [ "$DISTRIB" = "Suse" ] ; then
	ln -sf $EU_SCRIPT_DIR/eu_init $INIT_DIR/eagle-usb
	if [ $START_ON_BOOT == 1 ] ; then
		chkconfig --add eagle-usb >/dev/null
	else
		chkconfig --del eagle-usb >/dev/null
	fi
elif [ "$DISTRIB" = "Debian" ] ; then
	ln -sf $EU_SCRIPT_DIR/eu_init $INIT_DIR/eagle-usb
	if [ $START_ON_BOOT == 1 ] ; then
		update-rc.d eagle-usb start 99 2 3 4 5 . stop 01 0 1 6 . &>/dev/null
	else
		update-rc.d -f eagle-usb remove &>/dev/null
	fi
elif [ "$DISTRIB" = "Slackware" ] ; then
	ln -sf $EU_SCRIPT_DIR/rc.eagle-usb /etc/rc.d/rc.eagle-usb
	if [ $START_ON_BOOT == 1 ] ; then
		chmod a+x /etc/rc.d/rc.eagle-usb
	else
		chmod a-x /etc/rc.d/rc.eagle-usb
	fi
elif [ "$DISTRIB" = "Mandrake" ] ; then
	if [ -e /etc/init.d/internet ] ; then
		if [ $START_ON_BOOT == 1 ] ; then
			chkconfig --add internet
		else
			chkconfig --del internet
		fi
	else
		echo -e $SERVICE_MISSING_MSG
	fi
fi


# === testconnec ===
if [ $USE_TESTCONNEC == 1 ] ; then
	# add testconnec in /etc/cron.d
	if [ ! -d /etc/cron.d ] ; then
		echo -e $CRON_MISSING_MSG
	else
		echo "*/${TESTCONNEC_DELAY} * * * * root /etc/eagle-usb/scripts/testconnec -d1" > /etc/cron.d/eagle-usb
		chmod 755 /etc/cron.d/eagle-usb
	fi
else
	# remove testconnec in /etc/cron.d
	rm -f /etc/cron.d/eagle-usb
fi


# === custom interface name ===
if [ "$FORCE_IF" != "auto" ] ; then
	VAR="`uname -r`"
	if [ "x{$VAR:0:3}" = "x2.4" ] ; then
		MODCONF=/etc/modules.conf
	else
		MODCONF=/etc/modprobe.conf
	fi
	if [ -e $MODCONF ] ; then
		if [ ! -e "$MODCONF.old" ] ; then
			cp "$MODCONF" "$MODCONF.old"
		fi
		grep -v "eagle-usb" $MODCONF > $EU_DIR/tmp
		echo -e "options eagle-usb if_name=eagle_usb" >> $EU_DIR/tmp
		cat $EU_DIR/tmp > $MODCONF
	else
		echo -e "warning: $MODCONF was not found!"
	fi
fi


# === load module, dsp... ===

# note: on kernel 2.6 "lsmod" display "eagle_usb" instead of "eagle-usb"
if ! lsmod | sed s/_/-/g | grep -q "${MODULE}" ; then
	echo -en "\n\n${MODULE_MSG}"
	depmod -a
	modprobe $MODULE 1>/dev/null 2>/dev/null
	RES=$?
	sleep 5s
	if [ $RES == 0 ] ; then
		echo $OK_MSG
		# modem was not operationnal => send DSP
		SEND_DSP_NEEDED=1
	else
		echo $ERR_MSG
		exit 1
	fi
fi

# only send DSP whether it's necessary (1st time or line option changed)
SEND_DSP=$SEND_DSP_NEEDED
#echo "SEND_DSP_NEEDED=$SEND_DSP_NEEDED"

if $EAGLESTAT | grep -q "$PREFIRMWARE_STR" ; then
	# this problem occurs when the modem is not plugged
	echo -en "\n$DSP_MSG"
	echo $ERR_MSG
	if [ $EXPERT == 0 ] ; then
		# normal => quit
		exit 1
	else
		# expert => ask for interface (should be the next available)
		# it allow advanded users to finish configuration manually
		#echo -e $DSP_CRASHED_MSG
		#read INTERFACE
		if [ -z "$IF_FORCE" ] || [ "x$IF_FORCE" = "xauto" ] ; then exit 1 ; fi
		INTERFACE=$IF_FORCE
		SEND_DSP=0
	fi
fi

if [ $SEND_DSP == 1 ] ; then
	# === DSP & options are send to take in account new params ==
	echo -en "\n$DSP_MSG"
	# stop adsl (else eaglectrl -d can't send DSP)
	fctStopAdsl -sf
	$EAGLECTRL -d 1>/dev/null 2>/dev/null
	if [ $? == 0 ] ; then
		echo $OK_MSG

		# ==== waiting for "operational state" ===
		echo -en "\n${WAIT_OPER_MSG}"
		# wait 5s to see if modem has crashed.
		# (5s is the maximum delay between "booting" and "initializing" states)
		$EAGLECTRL -s5 1>/dev/null 2>/dev/null
		if $EAGLESTAT | grep -q "${BOOTING_STR}" ; then
			# crashed when "booting"?
			echo_log "== Modem crashed! Rebooting... =="
			sleep 1
			$EAGLECTRL -w 1>/dev/null 2>/dev/null
		else
			# not crashed => wait until end of synchro (or timeout)
			$EAGLECTRL -s 1>/dev/null 2>/dev/null
		fi
		if [ $? == 0 ] ; then
			echo -e "$OK_MSG\n"
		else
			echo $ERR_MSG
			exit 1
		fi
		INTERFACE=`${EAGLECTRL} -i 2>/dev/null`
		RES=$?
	else
		echo $ERR_MSG
		exit 1
	fi
else
	INTERFACE=`${EAGLECTRL} -i 2>/dev/null`
	RES=0
fi

# === ifup / ifdown ===
if [ $RES == 0 ] ; then
	if [ $USE_IFUPDOWN == 1 ] && [ $SIMPLE == 0 ] ; then
		rm -f $NET_SCRIPT_DIR/ifcfg-$INTERFACE
		touch $NET_SCRIPT_DIR/ifcfg-$INTERFACE
		echo "DEVICE=$INTERFACE" >> $NET_SCRIPT_DIR/ifcfg-$INTERFACE
		if [ $PPPOX != "none" ] ; then
			echo "BOOTPROTO=static" >> $NET_SCRIPT_DIR/ifcfg-$INTERFACE
			echo "IPADDR=0.0.0.0" >> $NET_SCRIPT_DIR/ifcfg-$INTERFACE
			echo "NETMASK=255.255.255.255" >> $NET_SCRIPT_DIR/ifcfg-$INTERFACE
		else
			echo "BOOTPROTO=dhcp" >> $NET_SCRIPT_DIR/ifcfg-$INTERFACE
		fi
		echo "ONBOOT=no" >> $NET_SCRIPT_DIR/ifcfg-$INTERFACE
		echo "MII_NOT_SUPPORTED=yes" >> $NET_SCRIPT_DIR/ifcfg-$INTERFACE
	fi
fi

rm -f $EU_DIR/tmp*
rm -f $EU_SCRIPT_DIR/lock


echo -e $GENER_MSG
if [ $SEND_DSP == 1 ] ; then
	echo -e $EAGLECONFIG_END_MSG
fi
echo

#***************************************************************************
# $Log: eagleconfig,v $
# Revision 1.20  2004/06/06 20:47:15  Tux
# - added custom interface name
#
# Revision 1.19  2004/05/30 01:51:46  Tux
# - fixed bug with parameters
# - vars name "FAI..." => "ISP..."
#
# Revision 1.18  2004/05/23 20:11:38  Tux
# - improved $LSTOPTIONS loop (Cyril Olivier Martin)
# - do not ask questions anymore
#
# Revision 1.17  2004/05/11 19:44:17  Tux
# - netmask for pppd users = 255.255.255.255
#
# Revision 1.16  2004/04/24 10:57:14  Tux
# - network mask changed from 255.255.255.255 to 255.255.255.0 for non-pppd users
#
# Revision 1.15  2004/04/21 20:07:52  Tux
# *** empty log message ***
#
# Revision 1.14  2004/04/21 19:57:56  Tux
# - virtual interface does not use an IP anymore
# - bug fix: wrong variable names
#
# Revision 1.13  2004/04/21 18:18:53  Tux
# - some changes with ">/dev/null"
#
# Revision 1.12  2004/04/21 17:41:51  Tux
# - prevent empty values from crashing the script
# - fix bug: $INTERFACE is not initialized when $SEND_DSP=0
#
# Revision 1.11  2004/04/18 09:46:56  Tux
# - bugfix (lot of crashes)
#
# Revision 1.10  2004/04/03 16:12:27  Tux
# - cut eagleconfig into 2 parts: eagleconfig & eu_config_bash
# - removed $USE_UPD_USB_USERMAP
# - only stopadsl & send DSP when it's necessary
#
# Revision 1.9  2004/03/23 20:16:06  Tux
# - fixed bug "This language (C) is not supported!"
# - autostart on slackware
# - expert mode allow eagleconfig to finish even if modem is not plugged
#
# Revision 1.8  2004/03/22 21:10:25  Tux
# new usb.usermap location (/etc/hotplug/usb/)
#
#***************************************************************************/
