#!/bin/bash
#
# system-switch-asterisk: Allows for switching between Asterisk versions 1.2 and 1.6.
#

OSD_INSTALLED=0
AST12_INSTALLED=0
AST16_INSTALLED=0
AST_RPMS=""
AST_NEW_VER=""

stty intr ^F

echo
echo "  system-switch-asterisk"
echo "  ----------------------"
echo

# Check if 1.2 is installed
echo -n "    Checking for Asterisk 1.2..."
rpm -q asterisk12 > /dev/null 2>&1
if [ "$?" = "0" ]; then
	echo "INSTALLED."
	AST12_INSTALLED=1
else
	echo "no."
fi

# Check if 1.6 is installed
echo -n "    Checking for Asterisk 1.6..."
rpm -q asterisk16 > /dev/null 2>&1
if [ "$?" = "0" ]; then
	echo "INSTALLED."
	AST16_INSTALLED=1
else
	echo "no."
fi

# Exit if neither 1.2 or 1.6 was found.
if [ "$AST12_INSTALLED" = "0" -a "$AST16_INSTALLED" = "0" ]; then
	echo -e "\n  ERROR: Asterisk is not installed.\n"
	stty intr ^C
	exit 1
fi
echo

# Test Internet connection
echo -n "    Testing Internet connection..."
ping -c 5 www.osdial.com > /dev/null 2>&1
if [ ! "$?" = "0" ]; then
	echo "FAILED."
	echo
	echo "  ERROR: In order to switch Asterisk versions, you must"
	echo "         have a working Internet connection."
	echo
	stty intr ^C
	exit 1
fi
echo "SUCCESS."

# Confirm Switch
echo
echo "  WARNING: Switching Asterisk versions will restart Asterisk,"
echo "           which will terminate all current calls."
echo
echo "  WARNING: There are subtle differences in dialplan logic and"
echo "           configuration files between Asterisk versions.  Be"
echo "           prepared to address any issues that might arise."
echo
[ "$AST12_INSTALLED" = "1" ] && AST_NEW_VER="1.6" || AST_NEW_VER="1.2"
if [ ! "$1" = "--force" ]; then
	read -p "To confirm switch of Asterisk to version $AST_NEW_VER, type 'yes': "
	if [ ! "$REPLY" = "yes" ]; then
		echo -e "\n  CANCELLED.\n"
		stty intr ^C
		exit 1
	fi
	echo
fi

# Gather installed RPMs.
echo -n "    Gathering Asterisk RPM list."
AST_RPMS="`rpm -qa | grep -E '(^asterisk|osdial-asterisk|dahdi|zaptel|wanpipe|libpri|voicetime)' | sort`"
echo "done."
echo -n "      "
echo $AST_RPMS | perl -p -e 's| |\n      |g'
echo "$AST_RPMS" | grep osdial-asterisk-version > /dev/null 2>&1
if [ "$?" = "0" ]; then
	echo "    OSDial is installed."
	OSD_INSTALLED=1
else
	echo "$AST_RPMS" | grep osdial-asterisk
	if [ "$?" = "0" ]; then
		echo -e "\n  ERROR: You must first update OSDial.\n"
		stty intr ^C
		exit 1
	fi
fi
echo

# Shut everything down.
echo -n "    Stopping all Asterisk related services."
service asterisk stop > /dev/null 2>&1
sleep 5
echo -n "."
service wanrouter stop > /dev/null 2>&1
sleep 1
rmmod wanpipe_voicetime > /dev/null 2>&1
echo -n "."
sleep 1
if [ "$AST12_INSTALLED" = "0" ]; then
	service dahdi stop > /dev/null 2>&1
	sleep 1
	rmmod dahdi > /dev/null 2>&1
else
	service zaptel stop > /dev/null 2>&1
	sleep 1
	rmmod zaptel > /dev/null 2>&1
fi
echo "done."

# Backup Asterisk configs.
BDATE="`date +%Y%m%d%H%M`"
echo -n "    Backing-up Asterisk Configuration in /etc/asterisk to /etc/asterisk-${BDATE}..."
cp -r /etc/asterisk /etc/asterisk-${BDATE}
echo "done."
if [ "$AST16_INSTALLED" = "1" -a -f "/etc/dahdi/system.conf" ]; then
	echo -n "    Backing-up DAHDI Configuration in /etc/dahdi to /etc/dahdi-${BDATE}..."
	cp -r /etc/dahdi /etc/dahdi-${BDATE}
	echo "done."
elif [ "$AST12_INSTALLED" = "1" -a -f "/etc/zaptel.conf" ]; then
	echo -n "    Backing-up Zaptel Configuration at /etc/zaptel.conf to /etc/zaptel.conf-${BDATE}..."
	cp /etc/zaptel.conf /etc/zaptel.conf-${BDATE}
	echo "done."
fi

# Remove packages
echo -n "    Removing Asterisk related RPMs..."
rpm -e --nodeps $AST_RPMS > /dev/null 2>&1
if [ ! "$?" = "0" ]; then
	echo "FAILED."
	echo -e "\n  ERROR: There was a fatal error while removing Asterisk.\n"
	stty intr ^C
	exit 1
fi
echo "done."

# Install Packages
INSTCMD=""
echo -n "    Installing RPMs for Asterisk $AST_NEW_VER (may take a while)..."
if [ "$OSD_INSTALLED" = "1" ]; then
	if [ "$AST12_INSTALLED" = "1" ]; then
		INSTCMD="yum -y install osdial-asterisk-version16"
	else
		INSTCMD="yum -y install osdial-asterisk-version12"
	fi
else
	if [ "$AST12_INSTALLED" = "1" ]; then
		INSTCMD="yum -y install asterisk16"
	else
		INSTCMD="yum -y install asterisk12"
	fi
fi
$INSTCMD > /dev/null 2>&1
if [ ! "$?" = "0" ]; then
	echo "FAILED."
	echo -e "\n  ERROR: There was a fatal error while installing Asterisk."
	echo -e "          Please try the following command manually."
	echo -e "     $INSTCMD\n" 
	stty intr ^C
	exit 1
fi
echo "done."

# Generate Zaptel/DAHDI config files
echo -n "    Generating Zaptel/DAHDI Configuration..."
if [ "$AST12_INSTALLED" = "1" ]; then
	/usr/sbin/dahdi_genconf > /dev/null 2>&1
	if [ ! -f "/etc/asterisk/chan_dahdi.conf" ]; then
		if [ -f "/etc/asterisk-${BDATE}/zapta.conf" ]; then
			cp /etc/asterisk-${BDATE}/zapata.conf /etc/asterisk/chan_dahdi.conf
		else
			cp /etc/asterisk/sample-configs/chan_dahdi.conf /etc/asterisk/chan_dahdi.conf
			if [ -f "/etc/asterisk/zapata-auto.conf" ]; then
				echo "#include zapata-auto.conf" >> /etc/asterisk/chan_dahdi.conf
			elif [ -f "/etc/asterisk/dahdi-channels.conf" ]; then
				echo "#include dahdi-channels.conf" >> /etc/asterisk/chan_dahdi.conf
			fi
		fi
	fi
else
	/usr/sbin/zapconf > /dev/null 2>&1
	if [ ! -f "/etc/asterisk/zapata.conf" ]; then
		if [ -f "/etc/asterisk-${BDATE}/chan_dahdi.conf" ]; then
			cp /etc/asterisk-${BDATE}/chan_dahdi.conf /etc/asterisk/zapata.conf
		else
			cp /etc/asterisk/sample-configs/zapata.conf /etc/asterisk
			if [ -f "/etc/asterisk/zapata-auto.conf" ]; then
				echo "#include zapata-auto.conf" >> /etc/asterisk/zapata.conf
			elif [ -f "/etc/asterisk/zapata-channels.conf" ]; then
				echo "#include zapata-channels.conf" >> /etc/asterisk/zapata.conf
			fi
		fi
	fi
fi
chown asterisk:asterisk /etc/asterisk/*.conf
echo "done."

# Start everything back up.
ZTCMD=""
echo -n "    Starting all Asterisk related services."
# start Zap/DAHDI
rmmod wanpipe_voicetime > /dev/null 2>&1
if [ "$AST12_INSTALLED" = "1" ]; then
	rmmod zaptel > /dev/null 2>&1
	ZTCMD="service dahdi start"
else
	rmmod dahdi > /dev/null 2>&1
	ZTCMD="service zaptel start"
fi
$ZTCMD > /dev/null 2>&1
if [ ! "$?" = "0" ]; then
	echo
	echo "  ERROR: Failed to start Zaptel/DAHDI, using:"
	echo -e "     $ZTCMD\n" 
	stty intr ^C
	exit 1
fi
echo -n "."
# start wanrouter
service wanrouter start > /dev/null 2>&1
if [ ! "$?" = "0" ]; then
	echo
	echo "  ERROR: Failed to start Wanrouter, using:"
	echo -e "     service wanrouter start\n" 
	stty intr ^C
	exit 1
fi
echo -n "."
# start asterisk
service asterisk start > /dev/null 2>&1
if [ ! "$?" = "0" ]; then
	echo
	echo "  ERROR: Failed to start Asterisk, using:"
	echo -e "     service asterisk start\n" 
	stty intr ^C
	exit 1
fi
echo "done."

echo -e "\n  SUCCESS!  The switch to Asterisk $AST_NEW_VER is complete.\n"
stty intr ^C
