#!/bin/bash
#
######################################################################
# 
# TITLE:    HP ProLiant Essentials Rapid Deployment Pack Linux Edition
# Japanese Language Kit
# VERSION:  1.1 Rev A
# LANGUAGE:  Japanese (JP)
#
# DESCRIPTION:  This SoftPaq contains the Japanese Language Kit
# Installer/Uninstaller
# Enhancements/Fixes:
# - added KS.JA.CFG files for each operating systems directory
# in the NFS Server.
#
# HOW TO USE:
# Download the SoftPaq to a directory on your target computer. The SoftPaq
# is a .tgz file with a file name based on the SoftPaq Number. 
#
# Run this command to extract the files from the .tgz file: 
#	tar -zxvf *.tgz 
# Refer to the Softpaq README file for installation instructions and
# firmware information.
#
# Y.M 05/18/04
######################################################################

menuselections=("Red Hat Enterprise Linux AS 2.1"\
	"Red Hat Enterprise Linux ES 2.1"\
	"Red Hat Enterprise Linux AS 3"\
	"Red Hat Enterprise Linux ES 3"\
	"Red Hat Linux 7.3"\
	"Red Hat Linux 8.0"\
	"United Linux 1.0"\
	"All Distributions" "Exit")
installfunctions=("SetPathRhas21"\
	"SetPathRhes21"\
	"SetPathRhas3"\
	"SetPathRhes3"\
	"SetPathRh73"\
	"SetPathRh80"\
	"SetPathUl10")

###############################################################################
function SetPathRhas21 { ospath=rhas21; osname="Red Hat Enterprise Linux AS 2.1";}
function SetPathRhes21 { ospath=rhes21; osname="Red Hat Enterprise Linux ES 2.1";}
function SetPathRhas3 { ospath=rhas3; osname="Red Hat Enterprise Linux AS 3";}
function SetPathRhes3 { ospath=rhes3; osname="Red Hat Enterprise Linux ES 3";}
function SetPathRh73 { ospath=rh73; osname="Red Hat Linux 7.3";}
function SetPathRh80 { ospath=rh80; osname="Red Hat Linux 8.0";}
function SetPathUl10 { ospath=ul10; osname="United Linux 1.0";}

function chgcolor
{
  # Change color
  case "$1" in
       blue )
        c1=40
        c2=34
       ;;
       green )
        c1=40
        c2=32
       ;;
       red )
        c1=40
        # c2=31 moved to white for readability
        c2=37
       ;;
       aqua )
        c1=40
        c2=36
       ;;
       orange )
        c1=40
        c2=33
       ;;
       white )
        c1=40
        c2=37
       ;;
       white-blue )
        c1=44
        c2=37
       ;;
       aqua-blue )
        c1=44
        c2=36
       ;;
       black-red )
        # moved to white for readability
        # c1=41
        # c2=30
        c1=40
        c2=37
       ;;
       reset )
        tput sgr0
       ;;
       * )
        # reset back to white on black
        tput sgr0
       ;;
  esac
  echo -en "\E[$c1;${c2}m"

}
##########################
function UpdateConfigurationFiles
{
   distdir=$1
   filespec=$2
   cd $distdir   

   if [ ! -d $distdir ]; then
      chgcolor orange
      echo
      echo "ERROR: Could not find $distdir directory."
      echo
      chgcolor white
      exit 60
   fi
echo
echo ":$distdir"
   for file in $filespec; do
      chmod +w $file

     if [ -e ${file%\.cfg}\.ja\.cfg ]; then
        echo "   The Japanese Configuration file (${file%\.cfg}.ja.cfg) already exists."
        echo -n "   Would you like to overwrite the ${file%\.cfg}.ja.cfg (y/n)? "

## Overwrite
        while [ $prompt=[yn] ]; do
                read prompt
             case $prompt in
             y)
                 chgcolor orange
                 echo "     Removing ${file%\.cfg}.ja.cfg..."
                 sed -e "s/lang en_US/lang ja_JP.eucJP/g" -e "s/langsupport en_US/langsupport \-\-default ja_JP.eucJP ja_JP.eucJP/g" -e "s/keyboard \"us\"/keyboard \"jp106\"/g" -e "s/timezone \-\-utc America\/Chicago/timezone \-\-utc Asia\/Tokyo/g" $file > $file.sav
#echo "${file%[.cfg]}"
                 mv $file.sav ${file%\.cfg}.ja.cfg

                 chgcolor green
                 echo -n "     Copying  ${file%\.cfg}.ja.cfg..."
                 chgcolor white
                 echo " Done"
                 break;;
              n)
                 break;;
              *)
                 echo -n "   Would you like to overwrite the ${file%\.cfg}.ja.cfg (y/n)? ";;
              esac
        done
##
      else
            sed -e "s/lang en_US/lang ja_JP.eucJP/g" -e "s/langsupport en_US/langsupport \-\-default ja_JP.eucJP ja_JP.eucJP/g" -e "s/keyboard \"us\"/keyboard \"jp106\"/g" -e "s/timezone \-\-utc America\/Chicago/timezone \-\-utc Asia\/Tokyo/g" $file > $file.sav
                 mv $file.sav ${file%\.cfg}.ja.cfg

            chgcolor green
            echo -n "     Copying  ${file%\.cfg}.ja.cfg..."
            chgcolor white
            echo " Done"
      fi
   done

   cd - 
   return 0
}
##########################
function UpdateULConfigurationFiles
{
   distdir=$1/control
   filespec=$2
   cd $distdir
   if [ ! -d $distdir ]; then
      chgcolor orange
      echo
      echo "ERROR: Could not find $distdir directory."
      echo
      chgcolor white
      exit 60
   fi

echo
echo ":$distdir"

   for file in $filespec; do
      chmod +w $file
     
     if [ -e ${file%\.xml}\.ja\.xml ]; then
        echo "   The Japanese Configuration file (${file%\.xml}.ja.xml) already exists."
        echo -n "   Would you like to overwrite the ${file%\.xml}.ja.xml (y/n)? "
## Overwrite
        while [ $prompt=[yn] ]; do
              echo -n " : "
                read prompt
             case $prompt in
             y)
                 chgcolor orange
                 echo "     Removing ${file%\.xml}.ja.xml..."
                 sed -e "s/<timezone>US\/Central/<timezone>Asia\/Tokyo/g" -e "s/<keymap>english-us/<keymap>japanese/g" -e "s/<language>en\_US/<language>ja\_JP\.eucJP/g" -e "s/chmod 777 \/tmp\/hp\/ul10\.sh/chmod 777 \/tmp\/hp\/ul10ja\.sh/g" -e "s/\/tmp\/hp\/ul10\.sh/\/tmp\/hp\/ul10ja\.sh/g" $file > $file.sav
                 mv $file.sav ${file%\.xml}.ja.xml
                 chgcolor green
                 echo -n "     Copying  ${file%\.xml}.ja.xml..."
                 chgcolor white
                 echo " Done"
                 break;;
              n)
                 break;;
              *)
                 echo -n "   Would you like to overwrite the ${file%\.xml}.ja.xml (y/n)? ";;
              esac
        done
##
     else
            sed -e "s/<timezone>US\/Central/<timezone>Asia\/Tokyo/g" -e "s/<keymap>english-us/<keymap>japanese/g" -e "s/<language>en\_US/<language>ja\_JP\.eucJP/g" $file > $file.sav
            mv $file.sav ${file%\.xml}.ja.xml
            chgcolor green
            echo -n "     Copying  ${file%\.xml}.ja.xml..."
            chgcolor white
            echo " Done"

      fi
   done

   cd -
   return 0
}
##########################
function GetNFSServerIP
{
   declare -a addresses
   let index=0

   # HACK: SuSE scripted installs can't use the hostname
#  if [ $(echo $menuselectionstring | cut -d' ' -f1) != "SuSE" -a $(echo $menuselectionstring | cut -d' ' -f1) != "All" ]; then

      # get hostname
      hostname=$(hostname -f 2>/dev/null)
      if [ -n "$hostname" -a "$hostname" != "localhost.localdomain" -a "$hostname" != "localhost" -a ! ${hostname%*.*} ]; then
         addresses[$index]=$hostname
         let index=$index+1
      fi

#   fi

   # get ip addresses
   for i in $(ifconfig | grep "inet addr" | cut -d: -f2 | cut -d' ' -f1); do
      if [ "$i" != "127.0.0.1" ]; then
         addresses[$index]=$i
         let index=$index+1
      fi
   done

   # if only one entry, use it if it is an ip address
   if [ "$index" = "1" ]; then
      if [ ${index%[0-9]*.[0-9]*.[0-9]*.[0-9*} ]; then
         nfsserver=${addresses[0]}
         return 0
      else
         chgcolor orange
         echo
         echo "ERROR: The network configuration of this machine is not correct."
         echo
         chgcolor white
         exit 60
      fi
   fi

   # select address to use
   echo
   echo "Select the hostname or IP address that you wish to use to identify the"
   echo "NFS server on the network. The target servers must have a route to the"
   echo "NFS server via the selected hostname or IP address."
   PS3='Address? '
   select addressselectionstring in "${addresses[@]}"; do
      let addressselection=$REPLY-1
      nfsserver=${addresses[$addressselection]}
      break
   done

   return 0
}

function InfoMessage
{
echo "... Done!"
chgcolor white-blue
distname=$1
 cat << TheTextData

-----------------------------------------------------------------------------
 IMPORTANT! You must update the OS scripted install events 
 on your Deployment Server to specify this NFS server (${nfsserver}). 

 Edit the "Run Script - Install OS" task and make the following changes.
   From:  set nfsserver=0.0.0.0
   To:    set nfsserver=${nfsserver}

 *) In case of ksfile=ks.cfg
   From:  set ksfile=ks.cfg
   To:    set ksfile=ks.ja.cfg
 *) In case of ksfile=xxxxx.ks.cfg
   From:  set ksfile=xxxxx.ks.cfg
   To:    set ksfile=xxxxx.ks.ja.cfg
 *) In case of ksfile=xxxxx.xml
   From:  set ksfile=xxxxx.xml
   To:    set ksfile=xxxxx.ja.xml
-----------------------------------------------------------------------------
TheTextData
   chgcolor white
   echo
   echo -n "(Press Enter to exit)"
   read answer
}
###############################################################################
#  MAIN EXECUTION STARTS HERE
###############################################################################

nfsserver=0.0.0.0
ss=ss.000
ospath=
osname=

chgcolor white
clear
# Switch to Aqua Blue
  chgcolor white-blue
  echo '                                                                    '
  echo "   HP ProLiant Essentials Rapid Deployment Pack Linux Edition       "
  # Switch to Green
  chgcolor aqua-blue
  echo -n "   Japanese Language Kit"
  echo "          VERSION:  1.10 Rev A              "
  # Switch to Aqua Blue
  echo '____________________________________________________________________'
  echo
  chgcolor white
#
# Determine the source root
#

kssrc=/usr/cpqrdp
scriptname=Install
# strip out script name from script execution command

# If not root, then exit
#
if [ "$UID" != "0" ]; then
   clear
   chgcolor orange
   echo
   echo "ERROR: You must be root to run this script."
   echo
   chgcolor white
   exit 60
fi
if [ ! -d $kssrc ]; then
    chgcolor orange
    echo
    echo "ERROR: Could not find the Rapid Deployment Pack files."
    echo "Please install ProLiant Integration Module for NFS Server"
    echo
    chgcolor white
    exit 60
fi

ss="ss.710"

GetNFSServerIP

echo "Select the distribution(s) that you wish to install Japanese Language "
echo "Kit files via NFS."
echo "To install more than one distribution, run $scriptname multiple times."
PS3='Distribution? '
let maxmenuselections=${#installfunctions[@]}
select menuselectionstring in "${menuselections[@]}"; do
   let menuselection=$REPLY-1
   case $menuselection in
      [0-$(($maxmenuselections - 1))] )
         ${installfunctions[$menuselection]}

        if [ $ospath == "ul10" ]; then
        ## for UL
         GetFList=$(ls $kssrc/$ss/$ospath/control | grep \.xml | grep -v \.ja\.xml)
         UpdateULConfigurationFiles $kssrc/$ss/$ospath "$GetFList"
#        UpdateULConfigurationFiles $kssrc/$ss/$ospath '*.xml'
        else
         UpdateConfigurationFiles $kssrc/$ss/$ospath '*ks.cfg'
        fi

         echo
         echo -n "(Press Enter to continue)"
         echo
         echo "_____________________________________________"
         read answer
         InfoMessage
         break;;
      $maxmenuselections )
         for installfunction in "${installfunctions[@]}"; do
            $installfunction
             if [ $ospath == "ul10" ]; then
             ## for UL
              GetFList=$(ls $kssrc/$ss/$ospath/control | grep \.xml | grep -v \.ja\.xml)
              UpdateULConfigurationFiles $kssrc/$ss/$ospath "$GetFList"
#             UpdateULConfigurationFiles $kssrc/$ss/$ospath '*.xml'
             else
              UpdateConfigurationFiles $kssrc/$ss/$ospath '*ks.cfg'
             fi

           echo
           echo -n "(Press Enter to continue)"
           echo
           echo "_____________________________________________"
           read answer
         done
         InfoMessage
         break;;
   esac
   break
done
# end of script
