#!/bin/bash
#

##################################################
##        ipchange: version .00000005           ##
## Purpose:                                     ##
##   To switch ip settings quickly              ##
##                                              ##
## Requires:                                    ##
##   Config files for each location             ##
##   which have been created by yast.           ##
## i.e. /etc/pcmcia/network.opts.<location>     ##
##                                              ##
## Setup:                                       ##
##   For each location,                         ##
##   setup network for a given location is yast ##
##   cp /etc/pcmcia/network.opts \              ##
##      /etc/pcmcia/network.opts.<location>     ##
##                                              ##
## COPYRIGHT NOTICE:                            ##
##   Copyright   2000 All Rights Reserved.      ##
##   Created by: John Streeton Stile            ##
##   email:      john@stilen.com                ##
##                                              ##
## Context:                                     ##
##   This must be run with all mighty root.     ##
##################################################

#######################################################
# check to see if they are root                       #
# if they are not root, they can't edit the files     #
#######################################################
function am_i_root {
  Me=`whoami`
  if [[ $Me != "root" ]]
  then
	echo -e  "\nGotta be su to run this ...."
	sleep 3
	exit 1
  fi
}

function slash_screen {
  BOLD=$(tput smso)
  NOBOLD=$(tput rmso)
  clear
  cat <<- EOT   
        ${BOLD}Made for SUSE Linux${NOBOLD}
   Switch ip settings at work and home
   This requries config files for each location
   stored in /etc/pcmcia/network.opts.<location>
   
   Press any key to continue.
EOT
  read bla
}

function non_interactive {
  if [ -n "$blabla" ]
  then 
	for network in $networks
	do
		if [ "$blabla" = `echo $network | awk -F. '{ print $3}'` ]
		then
			# echo -n "Setting network scheme '$blabla' ... "
			Location="$blabla"
			activate_location
			exit 0
		fi
	done
        #echo "$blabla Isn't my list" > /dev/stderr
  fi
}
################################################
#  Main Menu, options to see/change settings
#  Every function should return to this menu.
################################################
function menu {
BOLD=$(tput smso)
NOBOLD=$(tput rmso)

while true
do
	clear
	cat <<- ENDMENU

                  ${BOLD}SuSE Linux ipchange.bash (v..00000004)${NOBOLD}

                      Location: `echo $Location`
     ${BOLD}Variables${NOBOLD}                            ${BOLD}Settings${NOBOLD}
   1) Activate Location                    4) Remove Location
   2) Modify Location                      5) Rename Location
   3) Add New Location                     6) Display Location


                        X) Exit

	ENDMENU

	echo -e "Enter Choice: \c" 
	read choice
	case "$choice" 
	in  1 ) # Activate Location
		locations
		activate_location
                menu
		exit 0
		;;
 	    2 ) # Modify Location
		locations
	        modify_location
		menu
		exit 0
		;;
	    3 ) # Add Location
		add_location
		menu
		exit 0
		;;
	    4 ) # Remove Location
		locations
		remove_locaion
		menu
		exit 0
	    	;;
	     5 ) # Rename Locaion
		locations
		rename_locaion
		menu  
		exit 0
	    	;;		
	     6 ) # Display Location	    
		locations
		display_location
		menu
		exit 0
		;;	 	    
	 [xX] )	exit 0
		;;

	    * )	echo "$choice"
		exit 1
		;;
       esac
done
}
################################################
#  Locations - ask which location to look at
#  It checks to see if input was valid
#  Then return to the the next line in menu
################################################
function locations {  
  #
  # Updated 2001 06 26 : "select" takes a list, 
  #    numbers each entery on <STDOUT>, then it
  #    prompts with PS3, for the user to make a selection.
  #    If valid, it executes the code before the "done"
    
  # Make the prompt message.
  PS3old="$PS3"
  PS3="Choose Location: "
  
  # Make a list of available networks
  options=`ls /etc/pcmcia/network.opts.* |awk -F'.' '{print $3}'`
  export options
  
  # Number the options, and let them choose
  select Location in $options; do   
  	# Set the variable
	echo "$Location"
	export Location
	# Return the Prompt 
	PS3="$PS3old"
	return
  done
}

################################################
# 1) activate location 	- calls locations, 
#		    	- copies current to last
#		    	- moves Location to current
#		    	- returns to the menu
################################################
function activate_location {
   echo "Activating location $Location"
   cp /etc/pcmcia/network.opts /etc/pcmcia/network.opts.last
   cp /etc/pcmcia/network.opts.$Location /etc/pcmcia/network.opts
   /etc/init.d/pcmcia restart
} 
################################################
# 2) modify location 	- cp Location to Location<date>-<time>
# 			- vi the location
#			- return to the menu
################################################
function modify_location {
   cp /etc/pcmcia/network.opts.$Location /etc/pcmcia/network.opts.$Location$(date +%Y%m%d-\%H:%M)
   vi /etc/pcmcia/network.opts.$Location
}
################################################
#  3) add location	- asks user for location settings
#			- puts the output into template file
#			- return to the menu
################################################
function add_location {
   echo "not working yet."
   sleep 2
}
################################################
#  4) remove location	- ask if they realy want to do it
#			- remove location file
#			- return to the menu
################################################
function remove_locaion {
   echo "This will delete the locaion $Locaion"
   echo -e "Are you sure? \c"
   read bla
   case  "$bla"
   	in [Yy] ) # they want to delete it
		echo "deleting $Locaion"
		sleep 2
		echo "I have deleted /etc/pcmcia/network.opts.$Location"
		#rm -rf /etc/pcmcia/network.opts.$Location
		;;
	*  )	# They are not sure	
   		echo "Invalid choice: $bla"
		echo "Returning to the menu."
		sleep 2
		;;
   esac
} 
################################################
#  5) rename_locaion  	- 
################################################
function rename_locaion {
  echo "not working yet."
   sleep 2
}
################################################
#  6) display location 	- grep for the varialbe in Location
#			- return to the menu
################################################
function display_location {
   grep "^\ \ \ " /etc/pcmcia/network.opts.$Location |grep -v \# |grep -v \;
   echo -e "Press any key to continu \c:"
   read bla
}

##################################################
# Start the program
##################################################
blabla="$1"
am_i_root 
#slash_screen
# Initialize Variables
networks=`ls /etc/pcmcia/network.opts.*`  # * |grep -v ".*.opts$"`
here="$network"
Location=`echo $here | awk -F'.' '{print $3}'`
  if [ -n "$blabla" ]
  then 
	for network in $networks
	do
		if [ "$blabla" = `echo $network | awk -F. '{ print $3}'` ]
		then
			# echo -n "Setting network scheme '$blabla' ... "
			Location="$blabla"
			activate_location
		fi
	done
        #echo "$blabla Isn't my list" > /dev/stderr

  else 
	menu
 fi
# I am the bearded cow like sea beast
