#!/bin/sh
# ============================================================================
# WANPIPE (tm) Installation Script.
# ----------------------------------------------------------------------------
# Copyright (c) 1995-1996, Sangoma Technologies Inc.  All rights reserved.
# ============================================================================
# Rev.#	Date		Who	Details
# -----	---------------	-------	----------------------------------------------
#  2.30	Oct.16,1996	GK	Uses meta-configuration file wanpipe.rc.
#  2.20	Jul.15,1996	GK	
#  2.10	Jun.07,1996	GK	
#  2.00	May 01,1996	GK	
#  1.00	Apr.17,1996	GK	Initial version.
# ============================================================================
# set -x

PATH=/bin:/sbin:/usr/bin:/usr/sbin
WANPIPE_RC=/etc/wanpipe.rc
SDLACONF=/etc/sdla.conf
SUBSYSTEM=wanpipe
SDLADEVNAME=sdla
SDLADEVMAJOR=17
INITLEVEL=09

####### FUNCTION DEFINITIONS #################################################

# ----------------------------------------------------------------------------
# Clear the screen if it is supported.
# ----------------------------------------------------------------------------
clearscr() {
	# check if the terminal environment is set up
	[ "$TERM" ] && clear 2> /dev/null
}

# ----------------------------------------------------------------------------
# Display error message.
# ----------------------------------------------------------------------------
error() {
	echo -e "\nError: $*!"
	return 0
}

# ----------------------------------------------------------------------------
# Pause.
# ----------------------------------------------------------------------------
pause() {
	[ $# -ne 0 ] && echo -e $*
	echo -e "Press [Enter] to continue...\c"
	read tmp
	return 0
}

# ----------------------------------------------------------------------------
# Get Yes/No
# ----------------------------------------------------------------------------
getyn() {
	while echo -e "$* (y/n) \c" >&2
	do read yn rest
		case $yn in
			[yY])	return 0
				;;
			[nN])	return 1
				;;
			*)	echo -e "\nPlease answer y or n" >&2
				;;
		esac
	done
}

# ----------------------------------------------------------------------------
# Make SDLA device nodes.
# ----------------------------------------------------------------------------
makenodes() {
	banner
	echo "Creating SDLA device nodes ..."

	rm -f /dev/${SDLADEVNAME}*
	mknod -m 644 /dev/${SDLADEVNAME}0 c $SDLADEVMAJOR 0
	mknod -m 644 /dev/${SDLADEVNAME}1 c $SDLADEVMAJOR 1
	mknod -m 644 /dev/${SDLADEVNAME}2 c $SDLADEVMAJOR 2
	mknod -m 644 /dev/${SDLADEVNAME}3 c $SDLADEVMAJOR 3
	mknod -m 644 /dev/${SDLADEVNAME}4 c $SDLADEVMAJOR 4
	mknod -m 644 /dev/${SDLADEVNAME}5 c $SDLADEVMAJOR 5
	mknod -m 644 /dev/${SDLADEVNAME}6 c $SDLADEVMAJOR 6
	mknod -m 644 /dev/${SDLADEVNAME}7 c $SDLADEVMAJOR 7
	return 0
}

# ----------------------------------------------------------------------------
# Install initialization scripts.
# ----------------------------------------------------------------------------
installinit() {

	banner

	# Examine system bootstrap files.
	if [ -d /etc/init.d ]
	then
		INITDIR=/etc/init.d
		RCDIR=/etc
	elif [ -d /etc/rc.d/init.d ]
	then
		INITDIR=/etc/rc.d/init.d
		RCDIR=/etc/rc.d
	else
		cat << ENDOFTEXT

	WARNING: Your system does not use System V-like initialization
	method.  To enable automatic WANPIPE start-up on system boot you
	should add a WANPIPE initialization script ${INITSCRIPT}
	to one of your system boot-time initialization script, such as
	/etc/rc, /etc/rc.d/rc.M or /etc/rc.d/rc.local, depending on your
	particular Linux distribution.  See WANPIPE User Manual for more
	details.

ENDOFTEXT
		return 1
	fi

	# Get usrer's confirmation.
	cat << ENDOFTEXT

	Links to WANPIPE initialization script are about to be added
	to the following system directories:

		${INITDIR}
		${RCDIR}/rc2.d
		${RCDIR}/rc3.d
		${RCDIR}/rc5.d

ENDOFTEXT
	getyn "\nWould you like to install initialization script?" || return 0

	# Check script existance.
	[ -f "${INITSCRIPT}" ] || {
		error "WANPIPE initialization script ${INITSCRIPT} not found"
		return 1
	}

	# Install script.
	[ -d "${INITDIR}" ] && {
		[ -e ${INITDIR}/${SUBSYSTEM} ] && rm ${INITDIR}/${SUBSYSTEM}
		ln -s ${INITSCRIPT} ${INITDIR}/${SUBSYSTEM}
	}
	[ -d "${RCDIR}/rc2.d" ] && {
		[ -e ${RCDIR}/rc2.d/S${INITLEVEL}${SUBSYSTEM} ] && {
			rm ${RCDIR}/rc2.d/S${INITLEVEL}${SUBSYSTEM}
		}
		ln -s ${INITSCRIPT} ${RCDIR}/rc2.d/S${INITLEVEL}${SUBSYSTEM}
	}
	[ -d "${RCDIR}/rc3.d" ] && {
		[ -e ${RCDIR}/rc3.d/S${INITLEVEL}${SUBSYSTEM} ] && {
			rm ${RCDIR}/rc3.d/S${INITLEVEL}${SUBSYSTEM}
		}
		ln -s ${INITSCRIPT} ${RCDIR}/rc3.d/S${INITLEVEL}${SUBSYSTEM}
	}
	[ -d "${RCDIR}/rc5.d" ] && {
		[ -e ${RCDIR}/rc5.d/S${INITLEVEL}${SUBSYSTEM} ] && {
			rm ${RCDIR}/rc5.d/S${INITLEVEL}${SUBSYSTEM}
		}
		ln -s ${INITSCRIPT} ${RCDIR}/rc5.d/S${INITLEVEL}${SUBSYSTEM}
	}
	return 0
}

# ----------------------------------------------------------------------------
# Recompile modules.
# ----------------------------------------------------------------------------
recompile() {
	banner
	KVERSION=`uname -r`
	cat << ENDOFTEXT

	Your system is running Linux kernel version $KVERSION.
	WANPIPE is distributed with precompiled modules, however you
	must recompile them before you start WANPIPE to make sure
	your kernel and WANPIPE are compiled with the same configuration
	options.

	You can recompile WANPIPE modules now.  If you choose to skip
	this step, you will have to run WANPIPE configuration script
	($BUILDSCRIPT) at some point later.

ENDOFTEXT

	# Get user's confirmation.
	getyn "\nWould you like to recompile the modules now?" || return 0

	$BUILDSCRIPT
	return
}

# ----------------------------------------------------------------------------
# Configure driver.
# ----------------------------------------------------------------------------
configure() {
  banner
  cat <<ENDOFTEXT

	WANPIPE installation is now complete.  To be able to use
	WANPIPE start-up initialization scripts you must configure
	hardware, data links and network interfaces.

	You have an option to start WANPIPE configuration script
	now.  If you choose to skip this step, you will have to run
	WANPIPE configuration script ($CONFSCRIPT)
	or manually edit WANPIPE configuration file ($SDLACONF)
	at some point later.  (Refer to WANPIPE User's Manual for
	more information.)

ENDOFTEXT

  getyn "\nWould you like to configure WANPIPE now?" && $CONFSCRIPT
}

# ----------------------------------------------------------------------------
# Display banner.
# ----------------------------------------------------------------------------
banner() {
	clearscr
	cat << ENDOFTEXT
	----------------------------------------------------------
	         WANPIPE (tm) v$WANPIPE_VERSION Installation Script.
	    Copyright (c) 1995-1996, Sangoma Technologies Inc.
	----------------------------------------------------------

ENDOFTEXT
	return 0
}

####### MAIN #################################################################
# set -x

# Check user permissions.
[ "${LOGNAME}" = "root" ] || {
	error "You must be logged in as a superuser to run this script"
	exit 1
}

# Read meta-configuration file.
[ -f $WANPIPE_RC ] || {
	error "Configuration file $WANPIPE_RC not found"
	exit 1
}
. $WANPIPE_RC

# Set default configuration.
WANPIPE_VERSION=${WANPIPE_VERSION:=0.0.0}
WANPIPE_HOME=${WANPIPE_HOME:=/usr/lib/wanpipe}
WANPIPE_LOG=${WANPIPE_LOG:=/var/log/wanpipe}
WANPIPE_LOCK=${WANPIPE_LOCK:=/var/lock/wanpipe}
BUILDSCRIPT=${WANPIPE_HOME}/build
INITSCRIPT=${WANPIPE_HOME}/wanpipe
CONFSCRIPT=${WANPIPE_HOME}/configure
WANPIPE_SRC=${WANPIPE_HOME}/src
README=${WANPIPE_HOME}/README

# Ignore interrupt signals.
trap '' 2

# Display release notes.
banner
[ -f "${README}" ] && {
	getyn "\nWould you like to read release notes?" && {
		cat ${README} | more
		pause
	}
}

# Get a confirmation from user.
banner
cat << ENDOFTEXT
	You are about to install Sangoma WANPIPE (tm) product on your
	system.  This script will examine your system and install and/or
	create product files in your system directories.
ENDOFTEXT
getyn "\nWould you like to proceed with the installation?" || exit 0

# Remove obsolete files.
[ -f /var/adm/wanpipe.log ] && rm -f /var/adm/wanpipe.log 2> /dev/null

# Create device nodes.
makenodes || { pause; banner }

# Install System V -style initialization scripts.
installinit || { pause; banner }

# Recompile WANPIPE modules.
recompile || pause

# Configure SDLA hardware.
configure

