#!/bin/sh
# ============================================================================
# WANPIPE (tm) Build 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.21	Oct.10,1996	GK	
#  2.20	Jul.15,1996	GK	
#  2.10	Jun.07,1996	GK	
#  2.00	May 01,1996	GK	
# ============================================================================
# set -x

PATH=/bin:/sbin:/usr/bin:/usr/sbin
WANPIPE_RC=/etc/wanpipe.rc

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

# ----------------------------------------------------------------------------
# Recompile modules.
# ----------------------------------------------------------------------------
recompile() {

	# Check source directory existance.
	[ -d "$WANPIPE_SRC" ] || {
		error "Source directory $WANPIPE_SRC not found"
		return 1
	}

	case "$KVERSION" in
		1.2.*)	OS_VER=102	;;
		1.3.*)	OS_VER=103	;;
		2.*)	OS_VER=200	;;
		*)	error "Unknown kernel version"; return 1 ;;
	esac
	export OS_VER

	# Re-compile.
	cd $WANPIPE_SRC
	touch *.c
	make
}

# ----------------------------------------------------------------------------
# Display banner.
# ----------------------------------------------------------------------------
banner() {
  clearscr
  cat << ENDOFTEXT
	----------------------------------------------------------
	             WANPIPE (tm) v$WANPIPE_VERSION Build 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}
WANPIPE_BOOT=${WANPIPE_BOOT:=NO}
WANPIPE_SRC=$WANPIPE_HOME/src/modules

# Check product existance.
[ -d ${WANPIPE_HOME} ] || {
	error "Product directory ${WANPIPE_HOME} is not found"
	exit 1
}

# Gather system info.
KVERSION=`uname -r`

# Get a confirmation from user.
trap '' 2
banner
cat << ENDOFTEXT
	WARNING!

	You are about to rebuild Sangoma WANPIPE (tm) product.
	This will take few minutes.
ENDOFTEXT
getyn "\nWould you like to continue?" || exit 0

recompile || pause
