#! /bin/sh

# ----------------------------------------------------------
# Diva callback ppp (c) Copyright by Eicon Networks 1993 - 2003
# Please read legal notice at begin of reference manual:
# /usr/lib/eicon/divas/httpd/reference_manual.html
# ----------------------------------------------------------

diva_dir="/usr/lib/eicon/divas"
base="$diva_dir/tools/ppp_callback"
ppp_base="$base/ppp"
mgetty_base="$base/mgetty"
ppp_dst="/etc/ppp"
mgetty_dst="/etc/mgetty+sendfax"
dialog="$diva_dir/dialog"
tmp="$(pwd)/ppp.tmp"
last_error="Unexpected error"

ip_pool_1="192.168.1."
ip_pool_2="192.168.2."
ip_base="$ip_pool_1"
ip_limit=254

title="Diva callback PPP"
backtitle="Diva PPP Assistant Copyright (c) by Eicon Networks 1993 - 2003"

# --------------------------------------------------
#  Fatal exit. Print error message and exit
# --------------------------------------------------
fatal_exit()
{

	echo "INSTALLATION PROCEDURE FINISHED" > $tmp
	echo "" >> $tmp
	echo " ---------------------------------------------------------" >> $tmp
	echo -e "  ERROR: $1" >> $tmp
	if [ $(($#)) -gt 1 ]
	then
	echo -e "  MISSING: $2" >> $tmp
	fi
	echo "" >> $tmp

	! $dialog --title "$title" --backtitle "$backtitle" --clear --textbox $tmp 18 70
	rm -f $tmp
	setterm -default
	setterm -clear
	rm -rf ${base}/pppd ${base}/pppd.2 ${base}/ppp-2.4.1

	exit 1
}

exit_by_signal()
{
	fatal_exit "$last_error"
}

check_dir()
{
	if [ ! -d "$1" ]
	then
		fatal_exit "Missing directory" "$1"
	fi
}

check_file()
{
	if [ ! -s "$1" ]
	then
		fatal_exit "Missing file" "$1"
	fi
}

stop_mgetty=0

if [ $(($#)) -ge 1 ]
then
	case "$1" in
		"-stop") stop_mgetty=1
						;;
		*) fatal_exit "Unknown command line option" "$1"
			;;
	esac
fi

if [ $((stop_mgetty)) -ne $((0)) ]
then
	trap "exit_by_signal" 1 2 15 ERR

	last_error="sed -e \"/ttyds/d\" /etc/inittab > $tmp"
	sed -e "/ttyds/d" /etc/inittab > $tmp
	if [ ! -s "$tmp" ]
	then
		false
	fi

	last_error="cp -f $tmp /etc/inittab"
	$last_error

	last_error="kill -HUP 1"
	$last_error

	! killall pppd
	
	last_error="Final message"

	echo "PPP SHUTDOWN PROCEDURE FINISHED" > $tmp
	echo "" >> $tmp
	echo " ---------------------------------------------------------" >> $tmp
	echo "PPP server idle" >> $tmp
	! $dialog --title "$title" --backtitle "$backtitle" --clear --textbox $tmp 18 70

	setterm -default
	setterm -clear

	rm -f $tmp

	exit 0
fi



echo "PPP callback installation procedure" > $tmp
echo "" >> $tmp
echo "This application will install and start PPP server." >> $tmp
echo "For this reason this application will modify context" >> $tmp
echo "of /etc/ppp, /etc/mgetty+sendfax directories and" >> $tmp
echo "/etc/inittab file." >> $tmp
echo "" >> $tmp
echo "Please use 'Install -stop' to stop running PPP server" >> $tmp
echo "" >> $tmp
echo "Callback PPP is built using modified PPP. You will" >> $tmp
echo "find sources of the modified PPP in the" >> $tmp
echo "$base/ppp-2.4.1.tgz file." >> $tmp
$dialog --title "$title" --backtitle "$backtitle" --clear --textbox $tmp 18 70

last_error="Unexpected error"
trap "exit_by_signal" 1 2 15 ERR

msg="Continue with installation procedure ?"
last_error="Terminated by user"
$dialog --clear --title "$title" --backtitle "$backtitle" --yesno "$msg" 5 70 2>/dev/null




check_dir "$diva_dir"
check_dir "$base"
check_dir "$ppp_dst"
check_dir "$mgetty_dst"
check_dir "$ppp_base"
check_dir "$mgetty_base"

if [ -x "/usr/sbin/mgetty" ]
then
	mgetty="/usr/sbin/mgetty"
else
  if [ -x "/sbin/mgetty" ]
	then
		mgetty="/sbin/mgetty"
	else
		fatal_exit	"mgetty not installed" "mgetty"
	fi
fi

if [ ! -c "/dev/ttyds01" ]
then
	fatal_exit	"Diva TTY interface not found"
fi

last_error="Can not detect Diva TTY interface"
lsmod | grep Divatty > /dev/null 2>&1

last_error="Please update Diva TTY interface driver"
${diva_dir}/divactrl mantool -c 1001 -r -Eclusive -WDog -Silent > /dev/null 2>&1


# ---------------------------------------------------------
#  Try to build PPPD
# ---------------------------------------------------------
if [ -s "${base}/pppd" ]
then
  	$dialog --backtitle "$backtitle" \
					 --infobox "Installed locally built PPPD" 3 70
		set -e
		cp -f ${base}/pppd /usr/sbin/pppd.callback
		set +e
		chmod 755 /usr/sbin/pppd.callback
else
  $dialog --backtitle "$backtitle" \
					 --infobox "Build PPPD ..." 3 70
	last_error="Build PPPD"
	tar -zxf ${base}/ppp-2.4.1.tgz -C ${base} > /dev/null 2>&1
	cd ${base}/ppp-2.4.1/pppd > /dev/null 2>&1 && make clean > /dev/null 2>&1 && make pppd > /dev/null 2>&1 &&
			cp -f ${base}/ppp-2.4.1/pppd/pppd ${base}/pppd > /dev/null 2>&1
	cd ${base}
	if [ -s "${base}/ppp-2.4.1/pppd/pppd" ]
	then
		set -e
		last_error="cp -f pppd /usr/sbin/pppd.callback"
		cp -f ${base}/ppp-2.4.1/pppd/pppd ${base}/pppd > /dev/null 2>&1
		cp -f ${base}/pppd /usr/sbin/pppd.callback
		set +e
		chmod 755 /usr/sbin/pppd.callback
  	$dialog --backtitle "$backtitle" \
					 --infobox "Installed locally built PPPD" 3 70
	fi
	rm -rf ${base}/ppp-2.4.1 > /dev/null 2>&1
fi

# ---------------------------------------------------------
#  Use precompiled PPPD if failed to build locally
# ---------------------------------------------------------
if [ ! -s "${base}/pppd" ]
then
	kernel_major=$(uname -r 2>/dev/null | sed -e "s/^.\.//" -e "s/\..*$//" - 2>/dev/null)
	last_error="Extract pppd"
  $dialog --backtitle "$backtitle" \
					 --infobox "Extract precompiled PPPD ..." 3 70
	set -e
	tar -zxf ${base}/pppd.tgz -C ${base} > /dev/null 2>&1
	last_error="cp -f pppd /usr/sbin/pppd.callback"
	if [ $((kernel_major)) -lt 5 ]
	then
		cp -f ${base}/pppd /usr/sbin/pppd.callback
	else
		cp -f ${base}/pppd.2 /usr/sbin/pppd.callback
	fi
	set +e
	chmod 755 /usr/sbin/pppd.callback
	rm -f ${base}/pppd ${base}/pppd.2
  	$dialog --backtitle "$backtitle" \
					 --infobox "Installed precompiled PPPD" 3 70
fi

set -e

last_error="Update PPP configuration"
(cd $ppp_base; ls -1d * > $tmp)

for item in $(cat $tmp)
do
	if [ -f "${ppp_dst}/${item}" ]
	then
    if [ ! -f "${ppp_dst}/${item}.saved.by.diva" ]
		then
			last_error="cp -f ${ppp_dst}/${item} ${ppp_dst}/${item}.saved.by.diva"
			$last_error
		fi
	fi
	last_error="cp -f ${ppp_base}/${item} ${ppp_dst}/${item}"
	$last_error
done

last_error="Update MGETTY configuration"
(cd $mgetty_base; ls -1d * > $tmp)

for item in $(cat $tmp)
do
	if [ -f "${mgetty_dst}/${item}" ]
	then
    if [ ! -f "${mgetty_dst}/${item}.saved.by.diva" ]
		then
			last_error="cp -f ${mgetty_dst}/${item} ${mgetty_dst}/${item}.saved.by.diva"
			$last_error
		fi
	fi
	last_error="cp -f ${mgetty_base}/${item} ${mgetty_dst}/${item}"
	$last_error
done

last_error="PPP configuration"
(cd /dev; ls -1d ttyds* | sed -e "/ttydsctrl/d" - > $tmp)

ip_nr=1
ppp_channels=0

for item in $(cat $tmp)
do
	options_file=${ppp_dst}/options.${item}
	echo '#' > $options_file
	echo '# Options that differ for every TTY interface (i.e. IP address)' >> $options_file
	echo '#' >> $options_file
	echo '# LOCAL IP:REMOTE_IP' >> $options_file
	echo "${ip_base}${ip_nr}:${ip_base}$(($ip_nr+1))" >> $options_file
	echo "" >> $options_file
	ppp_channels=$(($ppp_channels+1))

  ip_nr=$(($ip_nr+2))
  if [ $(($ip_nr+1)) -gt $((ip_limit)) ]
	then
		ip_nr=1
		ip_base="$ip_pool_2"
	fi
done

last_error="Please re-compile callback pppd"
! /usr/sbin/pppd.callback dryrun > $tmp 2>&1
if [ $(($?)) -eq $((0)) ]
then
  fatal_exit "Please recompile callback PPP" "$(cat $tmp)"
fi

last_error="sed -e \"/ttyds/d\" /etc/inittab > $tmp"
sed -e "/ttyds/d" /etc/inittab > $tmp
if [ ! -s "$tmp" ]
then
	false
fi

last_error="cp -f $tmp /etc/inittab"
$last_error

last_error="Update /etc/inittab"
(cd /dev; ls -1d ttyds* | sed -e "/ttydsctrl/d" - > $tmp)

nr=1

for item in $(cat $tmp)
do
	if [ $((nr)) -le $((9)) ]
	then
		nr_str="0$((nr))"
	else
		nr_str="$((nr))"
	fi
	
	last_error="echo S${nr_str}:2345:respawn:${mgetty} ${item} >> /etc/inittab"
	echo "S${nr_str}:2345:respawn:${mgetty} ${item}" >> /etc/inittab

	nr=$(($nr+1))
done

sync

last_error="Start PPP server"
kill -HUP 1

last_error="Final message"

echo "INSTALLATION PROCEDURE FINISHED" > $tmp
echo "" >> $tmp
echo " ---------------------------------------------------------" >> $tmp
echo "PPP server running" >> $tmp
echo " Channels        : $((ppp_channels))" >> $tmp
echo " IP address range: ${ip_pool_1}1 - ${ip_base}$(($ip_nr-1))" >> $tmp
echo "" >> $tmp
echo "Please use 'Install -stop' to stop PPP server" >> $tmp
echo "" >> $tmp
echo "CHAP ACCOUNTS (${ppp_dst}/chap-secrets):" >> $tmp
cat  ${ppp_dst}/chap-secrets >> $tmp
echo "" >> $tmp
echo "PAP ACCOUNTS (disabled, ${ppp_dst}/pap-secrets):" >> $tmp
cat  ${ppp_dst}/pap-secrets >> $tmp

! $dialog --title "$title" --backtitle "$backtitle" --clear --textbox $tmp 18 70

setterm -default
setterm -clear

rm -f $tmp

exit 0

