#!/bin/sh
# ============================================================================
# WANPIPE (tm) De-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.
#  1.00	Jul.15,1996	GK	Initial version.
# ============================================================================
# set -x

PATH=/bin:/sbin:/usr/bin:/usr/sbin
WANPIPE_RC=/etc/wanpipe.rc
SDLACONF=/etc/sdla.conf
FLIPCONF=/etc/flip.conf
SUBSYSTEM=wanpipe
SDLADEVNAME=sdla
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
}

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

ENDOFTEXT
	return 0
}

# ----------------------------------------------------------------------------
# Delete links to initialization scripts.
# ----------------------------------------------------------------------------
del_init() {

	# 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 return 0
	fi

	# Remove script.
	[ -e ${INITDIR}/${SUBSYSTEM} ] && rm ${INITDIR}/${SUBSYSTEM}
	[ -e ${RCDIR}/rc2.d/S${INITLEVEL}${SUBSYSTEM} ] && {
		rm ${RCDIR}/rc2.d/S${INITLEVEL}${SUBSYSTEM}
	}
	[ -e ${RCDIR}/rc3.d/S${INITLEVEL}${SUBSYSTEM} ] && {
		rm ${RCDIR}/rc3.d/S${INITLEVEL}${SUBSYSTEM}
	}
	[ -e ${RCDIR}/rc5.d/S${INITLEVEL}${SUBSYSTEM} ] && {
		rm ${RCDIR}/rc5.d/S${INITLEVEL}${SUBSYSTEM}
	}
}

# ----------------------------------------------------------------------------
# Delete product files and device nodes.
# ----------------------------------------------------------------------------
del_files() {

	# Delete device nodes.
	rm -f /dev/${SDLADEVNAME}*

	# Delete configuration and log files.
	[ -f "$SDLACONF" ] && rm -f $SDLACONF
	[ -f "$FLIPCONF" ] && rm -f $FLIPCONF
	[ -f "$WANPIPE_RC" ] && rm -f $WANPIPE_RC
	[ -f "$WANPIPE_LOG" ] && rm -f $WANPIPE_LOG
}

###### MAIN ##################################################################

# 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}
INITSCRIPT=${WANPIPE_HOME}/wanpipe

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

# Ignore interrupt signals.
trap '' 2

# Get a confirmation from user.
banner
cat << ENDOFTEXT
	You are about to remove Sangoma WANPIPE (tm) from your system.
ENDOFTEXT
getyn "\nWould you like to continue?" || exit 0

# Stop WANPIPE.
$INITSCRIPT stop

# Remove product components.
del_init
del_files

# Remove product directory.
cd /
rm -fr $WANPIPE_HOME
