
#!/bin/bash
# Script to download and install updated INX scripts
# and bits and pieces on a running INX system.

# We'll need the killer function...
# And colours, naturally :)
# set -e

# See if this is INX - else we risk clobbering someone's install...
# For some reason I have to use /bin/echo here, or run sudo tests with "sudo bash " - weird
if ! grep INX /etc/issue.net ; then
    /bin/echo
    cat /etc/issue.net
    /bin/echo -e "\n  Oops, you don't seem to be running this from INX... "
    /bin/echo -e "  This script would clobber your configuration: - exiting!\n"
    exit 1
fi
		    
if [ $(whoami) = root ] ; then
    /bin/echo -e "\n  You don't need to run this as root - the script uses sudo.\n"
    /bin/echo -e "  So, don't run it with sudo, or as root... \n"
    exit
fi    


PATH=$PATH:/usr/local/lib/inx
. inx_essentials
. f-theme-and-guru
maybe_switch


cleantmp () { rm -rf $TMPUPDATE ; }

# Grab the inx ppa key if not already obtained...
if ! [ -f $HOME/.ppa_key_ok ] ; then 
    bold ; yellow
    echo -e "\n  Grabbing the gpg key for the INX apt PPA repository...\n\n"
    echo -e "  If you already have it, this is harmless :) Press any key to continue..."
    read -s -n 1
    unbold
    sudo apt-key adv --recv-keys --keyserver keyserver.ubuntu.com  89449B3E
    touch $HOME/.ppa_key_ok
    sleep 2
fi

unbold ; clear
echo
fig-col
figlet -k "  INX   Updater"
head-col ; echo
echo "  This menu gives you update options for INX."
info-col ; echo


echo "  1: Check for INX-specific updates (bug fixes, new apps and script updates)"
echo "  2: Only check for package upgrades from the repositories (apt-get)"
echo "  3: Restore: My INX update broke! Please return to the happy days before I did this!"
echo
echo "  m: Return to the INX menu."
echo "  x: Give me a command prompt."
head-col
echo
echo
read -s -n 1 UPGRADE
    case $UPGRADE in
        1)
	bold ; yellow
	echo -e "\n  This makes a backup of your INX configuration,"
	echo -e "  then grabs the most recent INX scripts and configurations.\n"

	green
	echo -e "  Continue? Your existing settings will be backed up. (Type n to abort)"
	read -s -n 1 BACKUPS
	if [ "$BACKUPS" = "n" -o "$BACKUPS" = "N" ] ; then
	    yellow
	    echo -e "\n  Well, OK ... Exiting..."
	    sleep 4
	    exec $0 
	fi
	
        # Do the downloads etc. into /tmp/tmp.*
        TMPUPDATE=$(mktemp -d)
	
        # Go home for backups
        echo -e "\n  Making backups...\n"
        mkdir -p $HOME/backup-pre-update
        cd
        bold ; green
        echo -e "\n  Backing up existing INX settings in $HOME/backup-pre-update...\n"
        sleep 5 ; unbold
        
	# $HOME
	
	cp -r .links2 .elinks .calcurse .irssi .inx .moc .mplayer plait .plait .vifm backup-pre-update/
	cp .auto-menus .bash_logout .bashrc .crawlrc .mailcap .muttheaders .muttrc-base .nanorc .profile .vimrc backup-pre-update
			
        cd $HOME/backup-pre-update	
        # /etc/skel

        tar czvf etc.skel.tar.gz /etc/skel

        # /usr/local

        tar czvf scripts.tar.gz /usr/local
	
	bold ; green
        echo -e "\n  Continuing with updates..."
        cd $TMPUPDATE

        echo -e "\n  Getting new files from the INX servers...\n"
        sleep 5 ; unbold

        for each in skel.tar.gz scripts.tar.gz selections newapps
         do
           wget -c http://inx.maincontent.net/updates/$each
         done
        bold ; green
        echo -e "\n  Updating package data...\n"
	unbold
        sleep 2
        sudo apt-get update
	bold ; green
        echo -e "\n  Grabbing new apps, if available...\n"
	sleep 3
	unbold
        sudo apt-get install $(cat $TMPUPDATE/newapps)
        

        bold ; green
        echo -e "\n Unpacking the INX-specific menu and configuration upgrades...\n"
	sleep 4 ; unbold

	# Back up /etc/skel
	
	sudo cp -r /etc/skel/ /etc/skel.pre-update
        # Clobber contents of /etc/skel since tar doesn't, and we don't want the old stuff ;)
	sudo rm -rf /etc/skel/*
	
        cd /etc/skel/
        sudo tar xvzf $TMPUPDATE/skel.tar.gz
	sudo rm -f skel.tar.gz
	cd /
        sudo tar xvzf $TMPUPDATE/scripts.tar.gz
	bold ; green
        echo -e "\n Copying new /etc/skel settings to $HOME ...\n" 
        sleep 4 ; unbold
        mkdir $TMPUPDATE/skel
	cd /$TMPUPDATE/skel
	tar xvzf $TMPUPDATE/skel.tar.gz
	cd
	rsync -av $TMPUPDATE/skel/ $HOME/
        rm $HOME/skel.tar.gz
	
	# Warn VM and HDD users about clobbering...
	if ! [ -d /rofs ] ; then
	    bold ; yellow
	    echo -e "\n  You appear to be running a hard drive install, or a virtual machine."
	    echo -e "  Some of your personal configuration might have been altered by this update."
	    echo -e "  All hidden $HOME configuration files (\"dot\" files) have been backed up"
	    echo -e "  to $HOME/backup-pre-update.\n"
	    echo -e "  If you find that, for example, your configuration for irssi, moc, or elinks"
	    echo -e "  has changed in a way that you dislike, copy the relevant file or directory back to $HOME.\n"
	    echo -e "  Examples: cp -r $HOME/backup-pre-update/.irssi $HOME"	    
	    echo -e "            cp  $HOME/backup-pre-update/.screenrc $HOME\n"
	    echo -e " You will be asked about the more obvious candidates ;-)"
            
	    clobbernow ()
	    {
	    bold ; green
	    echo -e "\n  Keep your older .$CLOBBER configuration? (y/n)\n"
	    read -s -n 1 NOCLOBBER
	    unbold
	    if [ "$NOCLOBBER" = "y" -o "$NOCLOBBER" = "Y" ] ; then
	        unbold ; echo
	        cp -r $HOME/backup-pre-update/.$CLOBBER $HOME
		bold ; green
		echo -e "\n  Restored your old .$CLOBBER settings.\n"
		sleep 3
	    else
	        bold ; green
	        echo -e "\n  Kept the newer version of the .$CLOBBER settings.\n"
		sleep 3
            fi    
	    
	    }
	    
	    green
	    echo -e "\n  Press any key to continue...\n"
	    read -s -n 1
	    
	    echo -e "\n  Here you can answer whether to keep old configurations."
	    echo -e "  There are several questions, so if you know you haven't"
	    echo -e "  played with your configuration, you might want to skip this ;-)"
	    yellow
	    echo -e "\n  Choose which older configurations to keep? (y for yes, default is no)\n"
	    read -s -n 1 GIVEITBACK
	    green

	    if [ "$GIVEITBACK" = "y" -o "$GIVEITBACK" = "Y" ] ; then
	    echo -e "\n  There may be some error messages about sockets etc. - they should be harmless, ;-)\n"
	        for CLOBBER in irssi elinks moc profile bashrc mailcap crawlrc vimrc nanorc plait
	          do
	            clobbernow $CLOBBER
	          done
            fi	      
        fi
	
        # Log out
        bold ; green
        echo -e "\n  Log out now to update all settings and menus? (y/n)\n"
        read -s -n 1 LOGOUT
        if [ "$LOGOUT" = "y" -o "$LOGOUT" = "Y" ] ; then
	    bold ; yellow
	    echo -e "\n  This will log out all your running tty virtual terminal sessions."
	    echo -e "  In a live CD session, all ttys will log back in automatically.\n"
	    echo -e "  Proceed? (default is 'No' - type 'y' for yes).\n"
	    read -s -n 1
	    if ! [ "$REPLY" = "y" -o "$REPLY" = "Y" ] ; then
	        cleantmp
	        exec $0
	    fi	
	    echo -e "\n  Logging out all your running virtual terminals - please wait."
            echo -e "  To revert changes, run the 'restore' option from the INX update menu.\n"
	    sleep 5
	    cleantmp
	    unbold
            killer
	else
	    cleantmp
	    exec $0
        fi    

        ;;
	2)
	unbold ; clear
	bold ; yellow

	echo -e "\v  Running sudo apt-get update and sudo apt-get upgrade, please wait...\n"
	sleep 3
	unbold
        sudo apt-get update
	bold ; yellow
	echo -e "\n  The next step upgrades all installed packages.\n"
	if [ -d /rofs ] ; then # HDD or virtual installs shouldn't have /rofs...
            echo -e "  You really don't want to do this in a live session ;)\n"
            echo -e "  Looks like this is a live session...\n"
	fi
	echo -e "  Go ahead and do the upgrades? (y for yes)"
	read -s -n 1 UPGRADES
	if [ "$UPGRADES" = "y" -o "$UPGRADES" = "Y" ] ; then
	unbold
	sudo apt-get upgrade 
	    if [ $? -ne 0 ] ; then    
	        bold ; yellow
	        echo -e "\n  Oops, apt had a problem! Exiting..."
	        sleep 3
            else
	        bold ; green
	        echo -e "\n  Your system packages are up to date."
	        sleep 5
            fi
	fi
	cleantmp
        exec $0
	;;
	3)
	unbold ; clear
	bold ; green 
        echo -e "\v This option simply puts the backup from before your last update in place."
	echo -e " Generally speaking you should not need it, but it is here as an option.\n"
	echo -e " It will not uninstall new packages, just revert INX menus and configuration."
	echo -e " Go ahead and put your old configuration back? (y for yes)"
	read -s -n 1 REPLACE
	if [ "$REPLACE" = "y" -o "$REPLACE" = "Y" ] ; then
            if ! [ -d $HOME/backup-pre-update ] ; then
        	    bold ; yellow
	            echo -e "\n  There doesn't seem to be a backup-pre-update directory..."
	            echo -e "  Not performing restore step...\n"
		    sleep 5
		    cleantmp
		    exec $0
	    fi    
	
	    cd /
	    # clear out /etc/skel (easier than doing dot files separately. Call me lazy...)
	    sudo rm -rf /etc/skel
	    # replace
	    unbold
	    sudo tar xvzf $HOME/backup-pre-update/etc.skel.tar.gz
	    sudo tar xvzf $HOME/backup-pre-update/scripts.tar.gz

	    cp -ar $HOME/backup-pre-update/{*,.*[A-Za-z0-9]} $HOME
    
	    rm -r $HOME/etc.skel.tar.gz $HOME/scripts.tar.gz $HOME/backup-pre-update-*
	    mv $HOME/backup-pre-update $HOME/backup-pre-update-$(date +%y%m%d-%H.%M)
	    bold ; green
	    echo -e "\n Your backups have been restored.\n"
	    sleep 3
	    cd
        fi
	cleantmp
        exec $0
	;;
	m)
	cleantmp
	exec menu
	;;
	x)
	cleantmp
	clear
	f-prompt
	;;
	*)
	head-col ; echo
	echo "  I didn't understand that request. Please try again."
	sleep 3
	exec $0
	;;
    esac
    
	
	