#!/bin/sh
#
# Version 3.01  2008-05-29 (Stable)
# Version 3.02  2008-06-09 (Add to SVN)
# Version 3.03  2008-06-10 (Umount before mount, add tcpdump to default install)
# Version 3.04  2008-10-01 (Add default IP address and ETH interface)
# Version 3.05  2008-10-10 (Add ChBRR configuration)
# Version 3.06  2008-10-11 (United: prepare, format and install packages)

PACKDATE=`date +%G%m%d%H%M`
ETH="eth0"

help() {
    echo -e "\nPlease select menu:\n"
    #echo "e  - Edit script"
    echo "h  - Get help"
    echo "q  - Quit"
    echo ""
    echo "i1 - Infocom: GPRS/FLASH configure"
    echo "i2 - Infocom: Prepare and format USB flash"
    echo "i3 - Infocom: Install packages to USB flash"
    echo "i4 - Infocom: Update SSH key"
    echo "i5 - Infocom: Backup router configs"
    echo "i6 - Infocom: Prepare, format and install packages to USB flash"
    echo ""
    echo "c1 - ChBRR: GPRS/ATM configure"
    echo ""
    echo "p1 - Pinger configure"
    echo ""    
    echo "s1 - SSH connect as flyrouter to work-pc via klondike"
    echo "s2 - SSH connect as flyrouter to work-pc via p13"
    echo ""    
    echo "u1 - Update #1"
    echo ""    
}

ask_var() {
    var="$1"
    prompt="$2"
    default="$3"
    echo -n "$prompt [$default]: "
    read val
    if [ "$val" = '' ] ; then
	val="$default"
    fi
    export "$var"="$val"
}




clear
echo -e "\nWelcome to simple multi-menu script..."
help



while true ; do
    echo -n -e "\n[`date +%H:%M`] -->"
    read COMMAND
    ANSWER=`echo $COMMAND`
case $ANSWER in

	    
#-------
    s1)
	echo ""
	rm -rf ~/.ssh/known_hosts 
	ssh -p 7322 flyrouter@192.168.6.77
	;;
#-------
    s2)
	echo ""
	rm -rf ~/.ssh/known_hosts
	ssh -p 7322 flyrouter@192.168.14.31
	;;
#-------
    h)
	help
	;;
#-------
    q)
        echo ""
	echo -e "\nGood bye, exiting..."
	exit 0
	;;



#-------
    i1)
	echo ""
	rm -rf ~/.ssh/known_hosts
	cd /var/www/build
	ask_var 'FLY_REMOTE_HOST' 'Remote hostname or ip:' '192.168.1.1'
	ask_var 'FLY_RESTORE_FILE' 'Restore file name:' './config/flyrouter_special/ifc_gprs_v5.tgz'
	ask_var 'FLY_USB_FLASH' 'USB Flash device on Linux:' '/dev/scsi/host0/bus0/target0/lun0'
	echo ""
	if [ ! -r "$FLY_RESTORE_FILE" ]; then
	    echo "Restore file $FLY_RESTORE_FILE not found... Exit"
	else
	    echo "Restore file $FLY_RESTORE_FILE found..."
	    echo "Probe ping remote host, please wait..."
	    #ip a add 192.168.1.2/24 dev $ETH
	    #sleep 1
	    if ! arping -I $ETH -q -c 1 $FLY_REMOTE_HOST > /dev/null ; then 
		echo "Host $FLY_REMOTE_HOST not alive... Exit"
	    else
		echo "Remote host $FLY_REMOTE_HOST alive..."
		echo "Connecting via SSH, please wait..."
		cat $FLY_RESTORE_FILE | ssh root@$FLY_REMOTE_HOST "cd / ; tar xvzf - ; \
		    rm -f /etc/init.d/S80dnrd /etc/init.d/S95ipkg-update ; \
		    killall -9 snmpd ; umount /mnt/usb ; sleep 2 ; mkdir -p /mnt/usb ; \
		    mount -o ro $FLY_USB_FLASH/part1 /mnt/usb ; sleep 2 ; \
		    cp -r /mnt/usb/etc/* /etc/ ; sleep 2 ; \
		    umount /mnt/usb ; sleep 2 ; \
		    flash save ; sleep 2 ; \
		    reboot"
	    fi
	    if [ $? != 0 ]; then
		    echo ""
		    echo "Configuration from file $FLY_RESTORE_FILE NOT upload... Exit"
		else
		    echo ""
		    echo "Configuration from file $FLY_RESTORE_FILE upload successfull"
	    fi
	fi
	;;
#-------
    i2)
	echo ""
	ask_var 'FLY_USB_FLASH' 'USB Flash device on Linux:' '/dev/scsi/host0/bus0/target0/lun0'
	ask_var 'FLY_WARNING_FLASH' 'Warning, all data on USB Flash will be erased (yes/no):' 'no'
	echo ""
	if [ "$FLY_WARNING_FLASH" = "yes" ]; then
	    if [ -c /dev/scsi/host* ];then
    		echo "You do not have USB Flash... Exit"
	    else
	        echo "Umount $FLY_USB_FLASH/part1..."
		umount /mnt/usb > /dev/null 2> /dev/null
		echo "Create Linux partition on $FLY_USB_FLASH/disc..."
		echo "Please press: t, 83, w"
		fdisk $FLY_USB_FLASH/disc 
		echo ""
		if [ $? != 0 ]; then
    		    echo "Linux partition can not be created on $FLY_USB_FLASH/disc... Exit"
		else
		    echo "Linux partition successfully created on $FLY_USB_FLASH/disc..."
		    echo "Create filesystem on $FLY_USB_FLASH/part1"
		    mke2fs $FLY_USB_FLASH/part1
		    if [ $? != 0 ]; then
			echo "Filesystem on $FLY_USB_FLASH/part1 can not be created... Exit"
		    else
			echo "Filesystem on $FLY_USB_FLASH/part1 successfully created..."
		    fi
		fi
	    fi
	else
	    echo "Ok, exit..."
	fi
	;;
#-------
    i3)
	echo ""
	ask_var 'FLY_USB_FLASH' 'USB Flash device on Linux:' '/dev/scsi/host0/bus0/target0/lun0'
	ask_var 'FLY_INSTALL_PACKAGES' 'Install next packages:' 'picocom deco openvpn snmpd-static tcpdump'
	echo ""
	echo "Start mount $FLY_USB_FLASH/part1 to /mnt/usb..."
	umount /mnt/usb > /dev/null 2> /dev/null
	mount $FLY_USB_FLASH/part1 /mnt/usb
	if [ $? != 0 ]; then
		echo "Mount $FLY_USB_FLASH/part1 to /mnt/usb FAILED... Exit"
	else
		echo "Mount $FLY_USB_FLASH/part1 to /mnt/usb done..."
		echo "Install selected packages to USB Flash..."
		ipkg update ; ipkg install -d usb $FLY_INSTALL_PACKAGES
		if [ $? != 0 ]; then
			echo "Install selected packages to USB Flash FAILED..."
		else
			echo "Install selected packages ($FLY_INSTALL_PACKAGES) to USB Flash done..."
		fi
	fi
	
	sleep 10
	reboot
	;;
#-------
    i4)
	echo ""
	rm -rf ~/.ssh/known_hosts
	ask_var 'FLY_REMOTE_HOST' 'Remote hostname or ip:' '192.168.1.1'
	ask_var 'FLY_UPLOAD_KEY' 'SSH public key for upload:' '~/.ssh/midge.pub'
	echo ""
	if [ ! -r "$FLY_UPLOAD_KEY" ]; then
	    echo "SSH public key $FLY_UPLOAD_KEY not found... Exit"
	else
	    echo "SSH public key $FLY_UPLOAD_KEY found..."
	    echo "Probe ping remote host, please wait..."
	    if ! ping -q -c 3 $FLY_REMOTE_HOST > /dev/null ; then 
		echo "Host $FLY_REMOTE_HOST not alive... Exit"
	    else
		echo "Remote host $FLY_REMOTE_HOST alive..."
		echo "Connecting via SSH, please wait..."
		cat $FLY_UPLOAD_KEY | ssh root@$FLY_REMOTE_HOST 'cd /etc/dropbear ; cat >> authorized_keys ; flash save'
	    fi
	    if [ $? != 0 ]; then
		    echo ""
		    echo "SSH public key $FLY_UPLOAD_KEY NOT upload... Exit"
		else
		    echo ""
		    echo "SSH public key $FLY_UPLOAD_KEY upload successfull"
	    fi
	fi
	;;
#-------
    i5)
	echo ""
	rm -rf ~/.ssh/known_hosts
	ask_var 'FLY_REMOTE_HOST' 'Remote hostname or ip:' '192.168.1.1'
	ask_var 'FLY_BACKUP_DIR' 'Backup dir for save remote configs:' './'
	echo ""
	if [ ! -d "$FLY_BACKUP_DIR" ]; then
	    echo "Backup dir $FLY_BACKUP_DIR not found... Exit"
	else
	    echo "Backup dir $FLY_BACKUP_DIR found..."
	    echo "Probe ping remote host, please wait..."
	    if ! ping -q -c 3 $FLY_REMOTE_HOST > /dev/null ; then 
		echo "echo "Host $FLY_REMOTE_HOST not alive... Exit""
	    else
		echo "Remote host $FLY_REMOTE_HOST alive..."
		echo "Connecting via SSH, please wait..."
		ssh -p 22 root@$FLY_REMOTE_HOST 'zcat /dev/mtdblock/3 | gzip' > $FLY_BACKUP_DIR/$FLY_REMOTE_HOST-$PACKDATE.tgz
	    fi
	    if [ $? != 0 ]; then
		    echo ""
		    echo "Backup file $FLY_BACKUP_DIR/$FLY_REMOTE_HOST-$PACKDATE.tgz NOT created"
		else
		    echo ""
		    echo "Backup file $FLY_BACKUP_DIR/$FLY_REMOTE_HOST-$PACKDATE.tgz successfully created"
	    fi
	fi
	;;


#-------
    i6)
	echo ""
	ask_var 'FLY_USB_FLASH' 'USB Flash device on Linux:' '/dev/scsi/host0/bus0/target0/lun0'
	ask_var 'FLY_INSTALL_PACKAGES' 'Install next packages:' 'picocom deco openvpn snmpd-static tcpdump'
	ask_var 'FLY_WARNING_FLASH' 'Warning, all data on USB Flash will be erased (yes/no):' 'no'
	echo ""
	if [ "$FLY_WARNING_FLASH" = "yes" ]; then
	    if [ -c /dev/scsi/host* ];then
    		echo "You do not have USB Flash... Exit"
	    else
	        echo "Umount $FLY_USB_FLASH/part1..."
		umount /mnt/usb > /dev/null 2> /dev/null
		echo "Create Linux partition on $FLY_USB_FLASH/disc..."
		echo "Please press: t, 83, w"
		fdisk $FLY_USB_FLASH/disc 
		echo ""
		if [ $? != 0 ]; then
    		    echo "Linux partition can not be created on $FLY_USB_FLASH/disc... Exit"
		else
		    echo "Linux partition successfully created on $FLY_USB_FLASH/disc..."
		    echo "Create filesystem on $FLY_USB_FLASH/part1"
		    mke2fs $FLY_USB_FLASH/part1
		    if [ $? != 0 ]; then
			echo "Filesystem on $FLY_USB_FLASH/part1 can not be created... Exit"
		    else
			echo "Filesystem on $FLY_USB_FLASH/part1 successfully created..."
			sleep 3
			echo "Start mount $FLY_USB_FLASH/part1 to /mnt/usb..."
			umount /mnt/usb > /dev/null 2> /dev/null
			mount $FLY_USB_FLASH/part1 /mnt/usb
			if [ $? != 0 ]; then
			    echo "Mount $FLY_USB_FLASH/part1 to /mnt/usb FAILED... Exit"
			else
			    echo "Mount $FLY_USB_FLASH/part1 to /mnt/usb done..."
			    echo "Install selected packages to USB Flash..."
			    ipkg update ; ipkg install -d usb $FLY_INSTALL_PACKAGES
			    if [ $? != 0 ]; then
				echo "Install selected packages to USB Flash FAILED..."
			    else
				echo "Install selected packages ($FLY_INSTALL_PACKAGES) to USB Flash done..."
			    fi
			fi
			echo "Wait 10 sec. and reboot..."
			sleep 10
			reboot
		    fi
		fi
	    fi
	else
	    echo "Ok, exit..."
	fi
	;;
#-------
    c1)
	echo ""
	rm -rf ~/.ssh/known_hosts
	ask_var 'FLY_REMOTE_HOST' 'Remote hostname or ip:' '192.168.1.1'
	ask_var 'FLY_RESTORE_FILE' 'Restore file name:' './chbrr_cdma_v1.tgz'
	echo ""
	if [ ! -r "$FLY_RESTORE_FILE" ]; then
	    echo "Restore file $FLY_RESTORE_FILE not found... Exit"
	else
	    echo "Restore file $FLY_RESTORE_FILE found..."
	    echo "Probe ping remote host, please wait..."
	    ip a add 192.168.1.2/24 dev $ETH
	    if ! ping -q -c 3 $FLY_REMOTE_HOST > /dev/null ; then 
		echo "Host $FLY_REMOTE_HOST not alive... Exit"
	    else
		echo "Remote host $FLY_REMOTE_HOST alive..."
		echo "Connecting via SSH, please wait..."
		cat $FLY_RESTORE_FILE | ssh root@$FLY_REMOTE_HOST "cd / ; tar xvzf - ; \
		    rm -f /etc/init.d/S80dnrd /etc/init.d/S95ipkg-update ; \
		    flash save ; sleep 2 ; \
		    reboot"
	    fi
	    if [ $? != 0 ]; then
		    echo ""
		    echo "Configuration from file $FLY_RESTORE_FILE NOT upload... Exit"
		else
		    echo ""
		    echo "Configuration from file $FLY_RESTORE_FILE upload successfull"
	    fi
	fi
	;;
#-------
    p1)
	echo ""
	rm -rf ~/.ssh/known_hosts
	ask_var 'FLY_REMOTE_HOST' 'Remote hostname or ip:' '192.168.1.1'
	ask_var 'FLY_RESTORE_FILE' 'Restore file name:' './pinger_v1.tgz'
	echo ""
	    echo "Probe ping remote host, please wait..."
	    if ! ping -q -c 3 $FLY_REMOTE_HOST > /dev/null ; then 
		echo "Host $FLY_REMOTE_HOST not alive... Exit"
	    else
		echo "Remote host $FLY_REMOTE_HOST alive..."
		echo "Connecting via SSH, please wait..."
		cat $FLY_RESTORE_FILE | ssh root@$FLY_REMOTE_HOST "cd / ; tar xvzf - ; \
		    rm -f /etc/init.d/S95ipkg-update /etc/init.d/S49synctime ; \
		    flash save ; sleep 2 ; \
		    reboot"
	    fi
	    if [ $? != 0 ]; then
		    echo ""
		    echo "Configuration from file $FLY_RESTORE_FILE NOT upload... Exit"
		else
		    echo ""
		    echo "Configuration from file $FLY_RESTORE_FILE upload successfull"
	    fi
	;;
#-------
    u1)
	echo ""
	rm -rf ~/.ssh/known_hosts
	ask_var 'FLY_REMOTE_HOST' 'Remote hostname or ip:' '192.168.1.1'
	ask_var 'FLY_RESTORE_FILE' 'Restore file name:' './up1.tgz'
	echo ""
	    echo "Probe ping remote host, please wait..."
	    if ! ping -q -c 3 $FLY_REMOTE_HOST > /dev/null ; then 
		echo "Host $FLY_REMOTE_HOST not alive... Exit"
	    else
		echo "Remote host $FLY_REMOTE_HOST alive..."
		echo "Connecting via SSH, please wait..."
		cat $FLY_RESTORE_FILE | ssh root@$FLY_REMOTE_HOST "cd / ; tar xvzf - ; \
		    flash save ; sleep 2 ; \
		    reboot"
	    fi
	    if [ $? != 0 ]; then
		    echo ""
		    echo "Configuration from file $FLY_RESTORE_FILE NOT upload... Exit"
		else
		    echo ""
		    echo "Configuration from file $FLY_RESTORE_FILE upload successfull"
	    fi
	;;
#-------
    *)
	;;
esac

done
