 
 # $Id$
 
+#### TO DO:
+#
+# - auto installation of NSM possible from inside this client,
+# - auto log monitoring and doing an action once a pattern is found,
+
 set logdir "/home/case/store/work/_archives_worklogs/"
-
-set os "Linux"
 
 set send_slow {10 .01}
 set timeout 60
 set app nsm
+set os "Linux"
 
 if { $argc == 0 } {
   puts "

IP that will be used during the installation is: $ourip

Ctrl+a h - this message,
Ctrl+a c - correct /usr/netscreen/DevSvr/var/devSvr.cfg by removing unneeded white characters (make a copy) and restart DevSvr,
Ctrl+a d - download and do a clean install of a given NSM version,
Ctrl+a i - can be entered during the nsm installation will answer all the questions (clean install Gui+Dev if installing from scratch or just refresh otherwise),
Ctrl+a l - types \"$pass\\r\",
Ctrl+a p - prepare a command that will uninstall all NSM packages and remove all the NSM data from the server, the command is printed without \\r at the end, 
Ctrl+a t - truncate the schema,
Ctrl+a u - correct the customer db (super password, IPs)

"
      send "\r"
    }

    \001l { 
      send "$pass\r"
    }

    \001c {
      set backuptime [ timestamp -format "%Y%m%d_%H%M%S"]
      send "perl -pi\".$backuptime\" -e 's/\s\s+/ /g' /usr/netscreen/DevSvr/var/devSvr.cfg && /etc/init.d/devSvr restart\r" 
    }

    \001d {
      send_user "\nPlease enter the NSM version in LGB format: "
      stty cooked echo 
      expect_user -re "(.*)\n"
      stty raw -echo
      set nextnsmver $expect_out(1,string)
      system "date;uptime"
      send_user "|localy run: nsm_auto_install.pl $nextnsmver $ourip|\n"
    }

    \001p { 
      if { $os == "Linux" } {
        send -s "rpm -qa | grep netscreen | xargs -r rpm -e ; rm -rf /var/netscreen/*/* /usr/netscreen/*" 
      } elseif { $os == "SunOS" } {
        send -s "pkgrm -n `pkginfo -c application | grep -i netscreen | grep -v NSCNpostgres | awk '{print \$2}' | xargs` &&  rm -rf /var/netscreen/ /usr/netscreen/"
      } 
    }

    \001i { 
        send  "\r"
        expect timeout {
          send_user " "
        } "Do you want to do NSM installation with base license? (y/n) *>" {
          send "y\r" 
          exp_continue
        } "Enter selection (1-2)*>" {
          send "2\r"
          exp_continue
        } "Will server(s) need to be reconfigured during the refresh? (y/n) *>" {
          send "n\r"
          exp_continue 
        } "Enter selection (1-3)*>" {
          send "3\r" 
          exp_continue
        } "Enter base directory location for management servers " {
          send "\r"
          exp_continue 
        } "Enable FIPS Support? (y/n)*>" {
          send "n\r"
          exp_continue
        } "Will this machine participate in an HA cluster? (y/n) *>" {
          send "n\r"
          exp_continue 
        } "Enter database log directory location *>" {
          send "\r"
          exp_continue
        } "Enter the management IP address of this server *>" {
          send "$ourip\r"
          exp_continue
        } "Enter the https port for NBI service *>" {
          send "\r"
          exp_continue
        } "Enter the https port for web server *>" {
          send "\r"
          exp_continue
        } "Enter password (password will not display as you type)>" {
          send "$pass\r"
          exp_continue
        } "Will a Statistical Report Server be used with this GUI Server? (y/n) *>" {
          send "n\r"
          exp_continue
        } "UNIX password: " {
          send "$pass\r"
          exp_continue
        } "Will server processes need to be restarted automatically in case of a failure? (y/n) *>" {
          send "n\r"
          exp_continue
        } "Will this machine require local database backups? (y/n) *>" {
          send "n\r"
          exp_continue
        } "Enter Postgres DevSvr Db port *> " {
          send "\r"
          exp_continue
        } "Enter Postgres DevSvr Db super user *> " {
          send "\r"
          exp_continue
        } "Start server(s) when finished? (y/n) *> " {
          send "y\r"
          exp_continue
        } "Are the above actions correct? (y/n)> " {
          send "y\r"
          exp_continue
        } "Specify location of PostgreSQL * bin *> " {
          send "\r"
          exp_continue
        }
    }

    \001t {
      send "/etc/init.d/haSvr stop\r"
      expect "*# " { send "/etc/init.d/guiSvr stop\r"}
      expect "*# " { send "/etc/init.d/devSvr stop\r"}
      #expect "*# " { send "mv -f /usr/netscreen/GuiSvr/var/dmi-schema-stage /usr/netscreen/GuiSvr/var/dmi-schema-stage.old\r"}
      expect "*# " { send "rm -rf /usr/netscreen/GuiSvr/var/dmi-schema-stage\r"}
      expect "*# " { send "cp --reply=yes -fpr /usr/netscreen/GuiSvr/lib/initVar/dmi-schema-stage /usr/netscreen/GuiSvr/var/dmi-schema-stage\r"}
      expect "*# " { send "rm -f /usr/netscreen/GuiSvr/var/xdb/init/*\r"}
      expect "*# " { send "rm -rf /tmp/Schemas*\r"}
      expect "*# " { send "rm -rf /usr/netscreen/GuiSvr/var/Schemas-GDH/*\r"}
      expect "*# " { send "sh /usr/netscreen/GuiSvr/utils/.truncateSchemaTables.sh /usr/netscreen/GuiSvr/var/xdb\r"}
      expect "*# " { send "cp --reply=yes -fpr /usr/netscreen/GuiSvr/lib/initVar/xdb/init/* /usr/netscreen/GuiSvr/var/xdb/init/\r"}
      expect "*# " { send "/etc/init.d/haSvr start\r"}
      expect "*# " { send "/etc/init.d/guiSvr start\r"}
      expect "*# " { send "/etc/init.d/devSvr start\r"}
    }

    \001u { 
      send "/etc/init.d/haSvr stop\r"
      expect "*# " { send "/etc/init.d/guiSvr stop\r"}
      expect "*# " { send "/usr/netscreen/GuiSvr/utils/setperms.sh GuiSvr > /dev/null\r"}
      expect "*# " { send "/bin/chmod +s /usr/netscreen/GuiSvr/utils/.installIdTool\r"}
      expect "*# " { send "/usr/netscreen/GuiSvr/utils/.xdbUpdate.sh /usr/netscreen/GuiSvr/var/xdb admin 1 0 /__/password \"glee/aW9bOYEewkD/6Ri8sHh2mU=\" > /dev/null\r"}
      sleep 1
      expect "*# " { send "/usr/netscreen/GuiSvr/utils/.xdbUpdate.sh /usr/netscreen/GuiSvr/var/xdb server 0 0 /__/ip \"$ourip\" > /dev/null\r"}
      sleep 1
      expect "*# " { send "/usr/netscreen/GuiSvr/utils/.xdbUpdate.sh /usr/netscreen/GuiSvr/var/xdb server 0 1 /__/ip \"$ourip\" > /dev/null\r"}
      sleep 1
      expect "*# " { send "/usr/netscreen/GuiSvr/utils/.xdbUpdate.sh /usr/netscreen/GuiSvr/var/xdb shadow_server 0 1 /__/clientOneTimePassword \"dk2003ns\" > /dev/null\r"} 
      sleep 1
      set backuptime [ timestamp -format "%Y%m%d_%H%M%S"]
      expect "*# " { send "perl -npi\".old_$backuptime\" -e 's/ +/ /g;s/(clientOneTimePassword\\s+)\\S*/\$1 dk2003ns/i;s/(default.printLevel\\s+)\\S*/\$1 debug/i;s/(ourRsaPrivateKey|theirRsaPublicKey)/#\$1/i;s/(guiSvrDirectiveHandler.max.heap|devSvrDirectiveHandler.max.heap\\s+)\\d+/\$1 1536000000/i' /var/netscreen/*Svr/*Svr.cfg > /dev/null\r"}
      expect "*# " { send "/etc/init.d/haSvr restart\r"}
      expect "*# " { send "/etc/init.d/guiSvr restart\r"}
      expect "*# " { send "/etc/init.d/devSvr restart\r"}
    }
  }
}


spawn ssh $user@$host

set stime     [ timestamp -format "%Y/%m/%d %H:%M:%S"]
set filetime [ timestamp -format "%Y%m%d_%H%M%S"]

puts "\033]0;$host $filetime\007"
