#!/bin/bash
# $Id: setvars,v 1.27 2004/08/29 21:20:19 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:@SBIN_DIR@"
export PATH

# === Paths(1) ===
EU_DIR=@EU_DIR@
EU_SCRIPT_DIR=@EU_SCRIPT_DIR@
EU_LANG_DIR=@EU_LANG_DIR@

# === Distrib capability ===
USE_HOTPLUG=@USE_HOTPLUG@
USE_IFUPDOWN=@USE_IFUPDOWN@

# === Distrib detection ===
DISTRIB=@DISTRIB@
DISTVER=@DISTVER@
SIMPLE=@SIMPLE@

# === Paths(2) ===
SBIN_DIR=@SBIN_DIR@
INIT_DIR=@INIT_DIR@
PPP_DIR=@PPP_DIR@
PPP_OPTIONS_DIR=@PPP_OPTIONS_DIR@
PPP_OPTIONS_ADSL=@PPP_OPTIONS_ADSL@
PPP_OPTIONS_MIRE=@PPP_OPTIONS_MIRE@
HOTPLUG_SCRIPT_DIR=@HOTPLUG_SCRIPT_DIR@
#HOTPLUG_USER MAP_DIR=@HOTPLUG_USER MAP_DIR@
#HOTPLUG_USER MAP_FILE=@HOTPLUG_USER MAP_FILE@
NET_SCRIPT_DIR=@NET_SCRIPT_DIR@
SYSCONF_FILE=@SYSCONF_FILE@

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

# === Strings used to determine modem state ===
SEND_DSP_STR="Please send DSP (eaglectrl -d)"
MODULE_STR="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
}

# === Language, load default from eagle-usb.conf ===
setStrings() {
	# auto => use system settings
	if [ "x$EU_LANG" = "xauto" ] ; then
		EU_LANG=`echo $LANG | cut -c 1-2`
		# instead of "fr", should we use simplified language file? (without french chars)
		if [ "x$EU_LANG" = "xfr" ] && [ "x$DISTRIB" = "xSuse" ] ; then
			EU_LANG=f2
		fi
	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="@EU_LANG@"
	fi
}
# fr is usually the most up-to-date language
# => it is better to display some french sentences than nothing!
. $EU_LANG_DIR/f2
readLangFromConfigFile
setStrings

# === Load config from eagle-usb.conf ===
# 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 -r 's/( |")+/\t/g' | cut -f2`
fi

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

# Password encryption (CHAP)
PWD_ENCRYPT=0
if [ -e $PPP_DIR/chap-secrets ] && [ ! -z $ISP_LOGIN ] ; then
	if grep -q -e "$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 [ -f /etc/rc5.d/S*internet ] || [ -f /etc/rc5.d/S*eagle-usb ] || [ -f /etc/init.d/rc5.d/S*eagle-usb ] ; then
	START_ON_BOOT=1
fi
if [ -f /etc/rc.d/rc.local ] && grep -q "eagle-usb" /etc/rc.d/rc.local ; then
	START_ON_BOOT=1
fi
if [ -f $NET_SCRIPT_DIR/ifcfg-ppp0 ] && grep -q "ONBOOT=yes" $NET_SCRIPT_DIR/ifcfg-ppp0 ; 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 | cut -c 1-3`"
if [ "x$VAR" = "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
}

# === Function used to compare floats ===
# convert a float to a sting in order to be compared to another
# string using "test -le" for example. It avoid use of bc.
flt_to_str() {
	echo $1 | sed -r -e "s/^(.*)\.(.)(.*)$/\1\2/" -e t -e "s/./\00/"
# 	if echo $1 | grep -q -e "\."; then
# 		echo $1 | sed -r -e "s/^(.*)\.(.)(.*)$/\1\2/"
# 	else # Ne contient que des chiffres
# 		echo ${1}0
# 	fi
}

# === 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
}

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

#***************************************************************************
# $Log: setvars,v $
# Revision 1.27  2004/08/29 21:20:19  Tux
# - add function flt_to_str (used to compare floats)
#
# Revision 1.26  2004/08/27 09:31:47  mcoolive
# - remove substring expansion (setvars is executed with source,
#   so it can be executed  by another shel than bash)
#
# Revision 1.25  2004/08/21 21:43:21  mcoolive
# - Fix to read ISP_PWD between simple quote (and not only double quote)
#
# Revision 1.24  2004/08/18 23:50:59  mcoolive
# - move the `source' of french messages so that it is made only once.
#
# Revision 1.23  2004/08/11 22:44:39  mcoolive
# - simplify the calculation of ISP_LOGIN ans ISP_PWD
#  and prevent an error in case where ISP_LOGIN is between double quotes
#
# Revision 1.22  2004/08/04 19:54:29  Tux
# - SEND_DSP => SEND_DSP_STR, $MODULE => $MODULE_STR
#
# Revision 1.21  2004/08/01 20:13:38  Tux
# - removed some unused hotplug related vars
#
# Revision 1.20  2004/07/16 21:14:05  Tux
# - added $PPP_OPTIONS_ADSL, $PPP_OPTIONS_DIR, $PPP_OPTIONS_MIRE, $SYSCONF_FILE
# - fixed bug with start_on_boot detection and add Mdk10.1 support
# - remove unused lines : i=1; PARAMS=""
#
# Revision 1.19  2004/07/02 19:11:14  Tux
# - slackware 10 support
# - fixed bug with custom interface name on 2.4 kernels
#
# Revision 1.18  2004/06/23 20:40:20  Tux
# - removed unused "isp_to_no" function
#
# Revision 1.17  2004/06/21 18:51:48  Tux
# - disable french chars (accents) on SuSE
#
# Revision 1.16  2004/06/20 16:05:18  baud123
# addition of new ISP
#
# 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?
#
#***************************************************************************/
