#!/bin/sh

# An *un*installer script, just in case...

PROCNAME="3dm2"
INIT_SH="/etc/init.d/tdm2"
AMCC_DIR="/opt/AMCC"
TW_CLI_LN="/usr/local/bin/tw_cli"
TW_CLI_MAN="/usr/local/share/man/man8/tw_cli.8.nroff"
CONF_DIR="/etc/3dm2"
BIN_DIR="/usr/sbin"
SYSV_INIT_GLOB="/etc/rc?.d/[SK]??`basename "${INIT_SH}"`"
M5INST_DIRNAME="3dm_m5inst"

echo ""
echo " !WARNING!"
echo ""
echo "I am about to *DELETE* all files associated with the 3DM and tw_cli utilities!"
echo "If you want to save the configuration file (${CONF_DIR}/${PROCNAME}.conf) or anything else, "
echo "abort and make backup copies now!!"
echo ""

echo -n "Press ENTER to continue (Ctrl-C to abort):"
# wait for input
read

# first, turn it off!
echo ""
echo "Stopping the 3DM service..."
if [ -x "${INIT_SH}" ]; then
	"${INIT_SH}" stop
	RETVAL=$?
	if [ $RETVAL -ne 0 ]; then
		echo "(error stopping the daemon??)"
	fi
else
	echo "(no init script??)"
	RETVAL=1
fi
if [ $RETVAL -ne 0 ]; then
	if [ -n "${PROCNAME}" ]; then
		sleep 1
		echo "using 'killall -9' sledgehammer..."
		killall -9 ${PROCNAME} 2>/dev/null
		sleep 1
	else
		echo "proceeding anyway..."
	fi
fi


echo ""
echo "Cleaning up the SysV init links ..."
rm -vf ${SYSV_INIT_GLOB} 2>/dev/null

echo ""
echo "Cleaning up man pages ..."
find /usr/local/share/man /usr/share/man -name "tw_cli*" -exec rm -vf {} \; 2>/dev/null

echo ""
echo "Deleting core files and directories ..."
for fd in ${INIT_SH} ${TW_CLI_LN} "${BIN_DIR}/${PROCNAME}" ${AMCC_DIR} ${CONF_DIR} ; do
	echo -n "	X  "
	echo -n "\"${fd}\""
	if [ ! -e "${fd}" ]; then
		echo " (not found)"
		continue
	fi
	if [ -d "${fd}" ]; then
		rm -rf "${fd}" 2>/dev/null
		RETVAL=$?
	elif [ -f "${fd}" ]; then
		rm -f "${fd}" 2>/dev/null
		RETVAL=$?
	fi
	if [ $RETVAL -ne 0 ]; then
		echo " (error)"
	else
		echo ""
	fi
done

echo ""
echo "done."

