#!/bin/bash
# $Id: setvars,v 1.15 2004/06/06 20:49:34 Tux Exp $
# Name: setvars
# Goal: initialize global variables (used by all scripts)
# Author: Tux
# Params: -

# === Environment path ===
PATH="/sbin:/usr/sbin:/usr/local/sbin:/bin:/usr/bin:/usr/local/bin:/usr/sbin"
export PATH

# === Paths(1) ===
EU_DIR=/etc/eagle-usb
EU_SCRIPT_DIR=/etc/eagle-usb/scripts
EU_LANG_DIR=/etc/eagle-usb/lang

# === Distrib capability ===
USE_HOTPLUG=1
USE_IFUPDOWN=0

# === Distrib detection ===
DISTRIB=Debian
DISTVER=0
SIMPLE=0

# === Paths(2) ===
SBIN_DIR=/usr/sbin
INIT_DIR=/etc/init.d
PPP_DIR=/etc/ppp
PPP_OPTIONS_DIR=/etc/ppp/peers
PPP_OPTIONS_ADSL=$PPP_OPTIONS_DIR/adsl
PPP_OPTIONS_MIRE=$PPP_OPTIONS_DIR/mire
HOTPLUG_SCRIPT_DIR=/etc/hotplug/usb
HOTPLUG_USERMAP_DIR=/usr/lib/hotplug
HOTPLUG_USERMAP_FILE=/etc/hotplug/usb.usermap
NET_SCRIPT_DIR=/etc/sysconfig/network-scripts

# === Commands ===
STARTADSL=startadsl
STARTMIRE=startmire
STOPADSL=stopadsl
EAGLECTRL=eaglectrl
EAGLESTAT=eaglestat

# === Strings used to determine modem state ===
SEND_DSP="Please send DSP (eaglectrl -d)"
MODULE="eagle-usb"
OPER_STR="Modem is operational"
BOOTING_STR="Modem is booting"
PREFIRMWARE_STR="Pre-firmware device"

# === Encapsulation => pppoa/pppoe/none ===
encapsToPppox() {
	if [ "$1" = "00000001" ] || [ "$1" = "00000002" ] ; then
		pppoe -h 1>/dev/null 2>/dev/null
		if [ $? != 0 ] ; then
			echo -e "\nError: pppoe cannot be launch. Exiting...\n"
			exit 1
		fi
		PPPOX="pppoe"
	elif [ "$1" = "00000003" ] || [ "$1" = "00000004" ] ; then
		PPPOX="none"
	elif [ "$1" = "00000005" ] || [ "$1" = "00000006" ] ; then
		PPPOX="pppoa"
	fi
}

# === Load config from eagle-usb.conf ===
# Language
setStrings() {
	# fr is usually the most up-to-date language
	# => it is better to display some french sentences than nothing!
	. $EU_LANG_DIR/fr
	# auto => use system settings
	if [ "x$EU_LANG" = "xauto" ] ; then
		EU_LANG=${LANG:0:2}
	elif [ "x$EU_LANG" = "xkeep" ] ; then
		readLangFromConfigFile
	fi
	# now read the real language-file
	if [ -z "$EU_LANG" ] || [ ! -f $EU_LANG_DIR/$EU_LANG ] ; then
		EU_LANG=en
	fi
	. $EU_LANG_DIR/$EU_LANG
}
readLangFromConfigFile() {
	if [ -e $EU_DIR/eagle-usb.conf ] && grep -q "^LANG=" $EU_DIR/eagle-usb.conf ; then
		EU_LANG="`grep '^LANG=' $EU_DIR/eagle-usb.conf | cut -d '=' -f2`"
	else
		# if --with-lang was not specified in ./configure, EU_LANG="auto"
		EU_LANG="auto"
	fi
}
readLangFromConfigFile
setStrings

# Linetype
LINETYPE="00000000"
if [ -e $EU_DIR/eagle-usb.conf ] && grep -q "^Linetype=" $EU_DIR/eagle-usb.conf ; then
	LINETYPE="`grep '^Linetype=' $EU_DIR/eagle-usb.conf | cut -d '=' -f2`"
fi

# VPI
VPI="00000000"
if [ -e $EU_DIR/eagle-usb.conf ] && grep -q "^VPI=" $EU_DIR/eagle-usb.conf ; then
	VPI="`grep '^VPI=' $EU_DIR/eagle-usb.conf | cut -d '=' -f2`"
fi

# VCI
VCI="00000000"
if [ -e $EU_DIR/eagle-usb.conf ] && grep -q "^VCI=" $EU_DIR/eagle-usb.conf ; then
	VCI="`grep '^VCI=' $EU_DIR/eagle-usb.conf | cut -d '=' -f2`"
fi

# ENC + PPPOX (pppoe/pppoa/none)
ENC="00000000"
PPPOX="pppoa"
if [ -e $EU_DIR/eagle-usb.conf ] && grep -q "^Encapsulation=" $EU_DIR/eagle-usb.conf ; then
	ENC="`grep '^Encapsulation=' $EU_DIR/eagle-usb.conf | cut -d '=' -f2`"
	encapsToPppox $ENC
fi

# ISP
ISP=""
if [ -e $EU_DIR/eagle-usb.conf ] && grep -q "^ISP=" $EU_DIR/eagle-usb.conf ; then
	ISP="`grep '^ISP=' $EU_DIR/eagle-usb.conf | cut -d '=' -f2`"
fi

# ISP_Login
ISP_LOGIN=""
if [ -e $PPP_OPTIONS_ADSL ] && grep -q "^user" $PPP_OPTIONS_ADSL ; then
	ISP_LOGIN=`grep '^user' $PPP_OPTIONS_ADSL | sed "s/\"/_/g" | sed "s/ /_/g" | sed "s/\t/_/g" | sed -r "s/_+/_/g" | cut -d "_" -f2`
fi

# ISP_Password
ISP_PWD=""
if [ ! -z "$ISP_LOGIN" ] ; then
	if [ -e $PPP_DIR/chap-secrets ] ; then
		ISP_PWD=`grep $ISP_LOGIN $PPP_DIR/chap-secrets | sed "s/ /_/g" | sed "s/\t/_/g" | sed -r "s/_+/_/g" | cut -d "_" -f3`
	fi
	if [ -z "$ISP_PWD" ] && [ -e $PPP_DIR/pap-secrets ] ; then
		ISP_PWD=`grep $ISP_LOGIN $PPP_DIR/pap-secrets | sed "s/ /_/g" | sed "s/\t/_/g" | sed -r "s/_+/_/g" | cut -d "_" -f3`
	fi
fi

# Password encryption (CHAP)
PWD_ENCRYPT=0
if [ -e $PPP_DIR/chap-secrets ] && [ ! -z $ISP_LOGIN ] ; then
	if grep -q "$ISP_LOGIN" $PPP_DIR/chap-secrets ; then
		PWD_ENCRYPT=1
	fi
fi

# Static IP
STATIC_IP="none"
if [ -e $EU_DIR/eagle-usb.conf ] && grep -q "^STATIC_IP=" $EU_DIR/eagle-usb.conf ; then
	STATIC_IP="`grep '^STATIC_IP=' $EU_DIR/eagle-usb.conf | cut -d '=' -f2`"
fi

# Asynchronous start
ASYNCHRONOUS_START=1
if [ -e $EU_DIR/eagle-usb.conf ] && grep -q "^ASYNCHRONOUS_START=" $EU_DIR/eagle-usb.conf ; then
	ASYNCHRONOUS_START="`grep '^ASYNCHRONOUS_START=' $EU_DIR/eagle-usb.conf | cut -d '=' -f2`"
fi

# Start on boot?
START_ON_BOOT=0
if [ -e /etc/rc5.d/S*internet ] || [ -e /etc/rc5.d/S*eagle-usb ] || [ -e /etc/init.d/rc5.d/S*eagle-usb ] ; then
	START_ON_BOOT=1
fi

# Testconnec enabled?
USE_TESTCONNEC=0
if [ -e /etc/cron.d/eagle-usb ] ; then
	USE_TESTCONNEC=1
fi

# Virtual interface name?
FORCE_IF="auto"
VAR="`uname -r`"
if [ "x{$VAR:0:3}" = "x2.4" ] ; then
	MODCONF=/etc/modules.conf
else
	MODCONF=/etc/modprobe.conf
fi
if [ -e $MODCONF ] && grep -q "eagle-usb" $MODCONF ; then
	FORCE_IF="`grep 'eagle-usb' $MODCONF | cut -d '=' -f2`"
	FORCE_IF=`echo $FORCE_IF | sed 's/"//g'`
	if echo $FORCE_IF | grep -q "\"" ; then
		echo -e "warning: if_name line in $MODCONF should not contain quotation marks!"
	fi
fi

# debug
# echo "VPI=$VPI VCI=$VCI ENC=$ENC ISP=$ISP"
# echo "ISP_LOGIN=$ISP_LOGIN ISP_PWD=$ISP_PWD STATIC_IP=$STATIC_IP"
# echo "ASYNCHRONOUS_START=$ASYNCHRONOUS_START START_ON_BOOT=$START_ON_BOOT USE_TESTCONNEC=$USE_TESTCONNEC"
# echo "FORCE_IF=$FORCE_IF"
# exit 123

# === Generic confirm function ===
confirm() {
	echo -en $1
	if [ $2 == 1 ] ; then
		echo -en " $YES_DEFAULT_MSG"
	else
		echo -en " $NO_DEFAULT_MSG"
	fi
	read REP
	if [ -z "$REP" ] ; then return `expr 1 - $2` ; fi
	if echo "$REP" | grep -qiE "o|y|t|s" ; then return 0 ; fi
	if [ "$REP" = "n" ] ; then return 1 ; fi
	return `expr 1 - $2`
}

# === Generic ping -w function ===
ping_test() {
	touch /tmp/setvars_ping
	ping -n -q -c 1 -i 1 $1 1>/dev/null 2>/dev/null
	if [ $? == 0 ] ; then
		rm -f /tmp/setvars_ping
	fi
}
kill_ping() {
	pid_ping=$1
	sleep $2
	kill $pid_ping 2>/dev/null
}
ping_w() {
	ping_test $1 &
	PID_PING=$!
	kill_ping $PID_PING $2 &
	wait ${PID_PING} 2>/dev/null
	if [ -e /tmp/setvars_ping ] ; then
		rm -f /tmp/setvars_ping
		return 1
	fi
	return 0
}

# === Debug mode ===
echo_log() {
	if [ "x$DEBUG" = "x2" ] || [ "x$2" = "x2" ] ; then
		echo $1
	elif [ "x$DEBUG" = "x1" ] || [ "x$2" = "x1" ] ; then
		logger $1
	fi
}

# === ISP TO NO ===
isp_to_no() {
	if [ -z "$1" ] ; then			NOISP=0
	elif [ "$1" = "BE" ] ; then		NOISP=1
	elif [ "$1" = "DE" ] ; then		NOISP=2
	elif [ "$1" = "ESTE" ] ; then		NOISP=3
	elif [ "$1" = "ESTEIPF" ] ; then	NOISP=4
	elif [ "$1" = "ESRE" ] ; then		NOISP=5
	elif [ "$1" = "ESTIC" ] ; then		NOISP=6
	elif [ "$1" = "FI" ] ; then		NOISP=7
	elif [ "$1" = "FRFTA" ] ; then		NOISP=8
	elif [ "$1" = "FRFTE" ] ; then		NOISP=9
	elif [ "$1" = "FRTIS128" ] ; then	NOISP=10
	elif [ "$1" = "FRFREEDEG" ] ; then	NOISP=11
	elif [ "$1" = "FR9" ] ; then		NOISP=12
	elif [ "$1" = "HU" ] ; then		NOISP=13
	elif [ "$1" = "IT" ] ; then		NOISP=14
	elif [ "$1" = "ITMC" ] ; then		NOISP=15
	elif [ "$1" = "NE" ] ; then		NOISP=16
	elif [ "$1" = "PL" ] ; then		NOISP=17
	elif [ "$1" = "PO" ] ; then		NOISP=18
	elif [ "$1" = "SW" ] ; then		NOISP=19
	elif [ "$1" = "UK" ] ; then		NOISP=20
	else NOISP=0
	fi
}

# delay for pppd to stop properly. After that, killall -9 is invoqued.
DELAY_KILL_PPPD=3

# used later to extract params
i=1
PARAMS=""

#***************************************************************************
# $Log: setvars,v $
# Revision 1.15  2004/06/06 20:49:34  Tux
# - display a warning when interface name contains quotation marks
#
# Revision 1.14  2004/05/30 01:55:52  Tux
# - vars name "FAI..." => "ISP..."
#
# Revision 1.13  2004/05/23 20:01:07  Tux
# - get virtual interface name ($FORCE_IF)
#
# Revision 1.12  2004/05/16 19:23:39  Tux
# *** empty log message ***
#
# Revision 1.11  2004/05/16 17:48:14  Tux
# - fixed 2 bugs with grep calls
#
# Revision 1.10  2004/04/28 17:15:20  Tux
# - remove unuseful data :-D
#
# Revision 1.9  2004/04/21 20:02:14  Tux
# - added $FAI_LOGIN, $FAI_PWD, $ENCRYPT
# - bug with separators ("cut" command is not very powerful!)
#
# Revision 1.8  2004/04/21 18:19:59  Tux
# - prevent unitialized vars from crashing the script
#
# Revision 1.7  2004/04/20 19:57:28  Tux
# - ensure that $PWD_ENCRYPT always exists
#
# Revision 1.6  2004/04/03 16:17:09  Tux
# - removed $USE_UPD_USB_USERMAP
# - better language management
# - now detect the whole modem parameters
#
# Revision 1.5  2004/03/22 21:19:31  Tux
# bug "Language C not found" fixed?
#
#***************************************************************************/
