# $Id: setvars,v 1.45 2005/01/16 22:06:35 Tux Exp $

# Copyright (C) 2003-2005 Olivier Borowski
# 
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

# Goal :
#	initialize global variables (used by all scripts)
# Params :
#	none

# === 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_LANG_DIR=@EU_LANG_DIR@

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

# === 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@
NET_SCRIPT_DIR=@NET_SCRIPT_DIR@
SYSCONF_FILE=@SYSCONF_FILE@

# === Commands ===
STARTADSL=startadsl
STOPADSL=stopadsl
EAGLECTRL=eaglectrl
EAGLESTAT=eaglestat
FCTSTARTADSL=fctStartAdsl
FCTSTOPADSL=fctStopAdsl

# === Strings used to determine modem state ===
MODULE_STR="eagle-usb"
SEND_DSP_STR="Please send DSP (eaglectrl -d)"
OPER_STR="Modem is operational"
BOOTING_STR="Modem is booting"
PREFIRMWARE_STR="Pre-firmware device"
#???_STR="eagle-usb driver not loaded"


# === readConfFile name default_value -> value ===
readConfFile(){
	if [ -e $EU_DIR/eagle-usb.conf ] && grep -q "^$1=" $EU_DIR/eagle-usb.conf ; then
		grep "^$1=" $EU_DIR/eagle-usb.conf | cut -d '=' -f2
	else
		echo "$2"
	fi
}

# === Encapsulation => pppoa/pppoe/none ===
encapsToPppox() {
	if [ "$1" = "00000001" ] || [ "$1" = "00000002" ] ; then
		pppoe -h 1>/dev/null 2>/dev/null
		if [ $? -ne 0 ] ; then
			echo -e "\nError: pppoe cannot be launched. 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)
		#[ "x$EU_LANG" = "xfr" -a "x$DISTRIB" = "xSuse" ] && EU_LANG=f2
		# TODO : remove the following test!!
		#if [ "x$EU_LANG" = "xpl" ] ; then
		#	EU_LANG=p2
		#fi
	elif [ "x$EU_LANG" = "xkeep" ] ; then
		EU_LANG="`readConfFile 'LANG' '@EU_LANG@'`"
	fi
	# now read the real language-file
	if [ -f $EU_LANG_DIR/$EU_LANG ] ; then
		. $EU_LANG_DIR/$EU_LANG
	fi
}
# "fr" is usually the most up-to-date language
# "en" is the best understood
. $EU_LANG_DIR/f2
. $EU_LANG_DIR/en
EU_LANG="`readConfFile 'LANG' '@EU_LANG@'`"
setStrings

# === Load config from eagle-usb.conf ===
# Linetype
LINETYPE="`readConfFile 'Linetype' '00000000'`"

# VPI
VPI="`readConfFile 'VPI' '00000000'`"

# VCI
VCI="`readConfFile 'VCI' '00000000'`"

# 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="`readConfFile 'ISP' ''`"

# 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="`readConfFile 'STATIC_IP' 'none'`"

# Asynchronous start
ASYNCHRONOUS_START="`readConfFile 'ASYNCHRONOUS_START' '1'`"

# auto-update resolv.conf?
UPDATE_DNS=0
if [ "$PPPOX" = "none" ] ; then
	# UPDATE_DNS is currently unused when not using pppd :
	#  - using dhclient, DNS are updated automatically
	#  - with a static ip, DNS have to be specified by hand in /etc/revolv.conf
	UPDATE_DNS=1
else
	if [ ! -f $PPP_OPTIONS_ADSL ] || grep -q "usepeerdns" $PPP_OPTIONS_ADSL ; then
		UPDATE_DNS=1
	fi
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 'options eagle-usb if_name=' $MODCONF | 
		cut -d '=' -f2 | sed 's/\"//g'`"
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() {
	doInUtf8 echo -en $1
	if [ $2 -eq 1 ] ; then
		doInUtf8 echo -en " $YES_DEFAULT_MSG"
	else
		doInUtf8 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 [ $? -eq 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 distrib versions ===
# !!! This is not a comparison between floats !!!
# examples: test_version 1.1 -eq 1.10 => false ; test_version 1.1 -eq 1.01 => true
test_version() {
# $1 is a version number
# $2 is an arithmetic operator ( -eq, -ne, -lt, -le, -gt, or -ge )
# $3 is a version number
	local head1 tail1 head2 tail2

	head1=`echo $1 | cut -d'.' -f1`
	tail1=`echo $1 | sed -nre 's/[^.]*\.//p'`
	head2=`echo $3 | cut -d'.' -f1`
	tail2=`echo $3 | sed -nre 's/[^.]*\.//p'`

	if test $head1 -eq $head2 -a -n "$tail1" -a -n "$tail2"; then
		test_version $tail1 $2 $tail2
	elif test $head1 $2 $head2; then
		true
	else
		false
	fi
}

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

# === UTF-8 ===
function doInUtf8 () {
	"$@" | iconv -f UTF-8 -t `locale charmap`
#	case $TERM in
#		aterm | rxvt )
#			echo "coucou"
#			#$@ | iconv -f
#			;;
#		* )
#			echo -ne '\033%G'
#			"$@"
#			echo -ne '\033%@'
#			;;
# 		xterm )  if [ `locale charmap` = UTF-8 ]; then
# 					"$@"
# 				else echo -ne '\033%G' ; "$@" ; echo -ne '\033%@'
# 				fi ;;
# 		aterm | rxvt ) "$@" ;;  # BUG ? If we use CONDING variable, it's as before
# 		* ) "$@" ;;             # BUG ? Idem
#	esac;
}

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

#***************************************************************************
# $Log: setvars,v $
# Revision 1.45  2005/01/16 22:06:35  Tux
# - add license header
# - remove some hacks with language files
#
# Revision 1.44  2005/01/06 00:28:09  mcoolive
# - fix a little bug ("$@" for args of doInUtf8 with space...)
#
# Revision 1.43  2005/01/04 21:14:05  Tux
# - switch strings to utf-8
#
# Revision 1.42  2005/01/04 20:49:06  Tux
# - added doInUtf8 function
#
# Revision 1.41  2004/12/05 01:27:59  mcoolive
# - replace flt_to_float function by test_version anywhere
#
# Revision 1.40  2004/11/21 15:30:16  Tux
# - replaced == with -eq
# - removed #!/bin/bash
#
# Revision 1.39  2004/11/17 20:58:14  Tux
# - change paths
#
# Revision 1.38  2004/11/11 10:56:04  mcoolive
# - tests are simplified ( && -> -a ; || -> -o )
#
# Revision 1.37  2004/11/07 20:48:04  Tux
# - add $FCTSTARTADSL and $FCTSTOPADSL
# - polish language temporary use p2 (without special characters)
#
# Revision 1.36  2004/11/07 20:39:08  mcoolive
# - fix bug (when user add alias or other options in modprobe.conf)
#
# Revision 1.35  2004/10/17 19:13:18  mcoolive
# - simplifications (factorize code with readConfFile function)
#
# Revision 1.34  2004/10/17 00:13:57  mcoolive
# - remove unused variables
# - "en" is the default language
#
# Revision 1.33  2004/09/29 11:54:41  mcoolive
# - (POSIX) commands are typed "int main..." => replace "$? ==" by "$? -eq"...
# - fix typo
# - cosmetic changes
#
# Revision 1.32  2004/09/28 19:45:20  Tux
# - add $BOOT_METHOD
#
# Revision 1.31  2004/09/26 19:36:30  Tux
# - add comments
#
# Revision 1.30  2004/09/16 20:48:24  Tux
# - removed useless $SYSCONF_DIR variable
#
# Revision 1.29  2004/09/16 20:13:51  Tux
# - "eagleconfig -e" now ask whether resolv.conf should be automatically updated
#
# Revision 1.28  2004/09/15 19:25:03  Tux
# - make sure that parent folders of the lock file exist
#
# 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?
#
#***************************************************************************/
