#!/bin/bash
#
######################################################################
# 
# TITLE:    HP ProLiant Essentials Rapid Deployment Pack Linux Edition
# Japanese Language Kit
# VERSION:  1.30 Rev A
# LANGUAGE:  Japanese (JP)
#
# DESCRIPTION:  This SoftPaq contains the Japanese Language Kit
# Installer/Uninstaller and Readme file.
# Enhancements/Fixes:
# - Added DEFAULT.JA.CFG/DEFAULT.JA.XML 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 08/25/05
######################################################################

menuselections=("Red Hat Enterprise Linux AS 3 Update 5 for x86"\
	"Red Hat Enterprise Linux AS 3 Update 5 for AMD64/EM64T"\
	"Red Hat Enterprise Linux ES 3 Update 5 for x86"\
	"Red Hat Enterprise Linux ES 3 Update 5 for AMD64/EM64T"\
	"Red Hat Enterprise Linux AS 4 Update 1 for x86"\
	"Red Hat Enterprise Linux AS 4 Update 1 for AMD64/EM64T"\
	"Red Hat Enterprise Linux ES 4 Update 1 for x86"\
	"Red Hat Enterprise Linux ES 4 Update 1 for AMD64/EM64T"\
	"SUSE LINUX Enterprise Server 9 SP1 for x86"\
	"SUSE LINUX Enterprise Server 9 SP1 for AMD64/EM64T"\
	"Novell Open Enterprise Server for x86"\
	"All Distributions" "Exit")
installfunctions=("SetPathRhas3u5"\
	"SetPathRhas3u5x64"\
	"SetPathRhes3u5"\
	"SetPathRhes3u5x64"\
	"SetPathRhas4u1"\
	"SetPathRhas4u1x64"\
	"SetPathRhes4u1"\
	"SetPathRhes4u1x64"\
	"SetPathSles9sp1"\
	"SetPathSles9sp1x64"\
	"SetPathOes")

###############################################################################
function SetPathRhas3u5 { ospath=rhas3u5; osname="Red Hat Enterprise Linux AS 3 Update 5 for x86";}
function SetPathRhas3u5x64 { ospath=rhas3u5.64; osname="Red Hat Enterprise Linux AS 3 Update 5 for AMD64/EM64T";}
function SetPathRhes3u5 { ospath=rhes3u5; osname="Red Hat Enterprise Linux ES 3 Update 5 for x86";}
function SetPathRhes3u5x64 { ospath=rhes3u5.64; osname="Red Hat Enterprise Linux ES 3 Update 5 for AMD64/EM64T";}
function SetPathRhas4u1 { ospath=rhas4u1; osname="Red Hat Enterprise Linux AS 4 Update 1 for x86";}
function SetPathRhas4u1x64 { ospath=rhas4u1.64; osname="Red Hat Enterprise Linux AS 4 Update 1 for AMD64/EM64T";}
function SetPathRhes4u1 { ospath=rhes4u1; osname="Red Hat Enterprise Linux ES 4 Update 1 for x86";}
function SetPathRhes4u1x64 { ospath=rhes4u1.64; osname="Red Hat Enterprise Linux ES 4 Update 1 for AMD64/EM64T";}
function SetPathSles9sp1 { ospath=sles9sp1; osname="SUSE LINUX Enterprise Server 9 SP1 for x86";}
function SetPathSles9sp1x64 { ospath=sles9sp1.64; osname="SUSE LINUX Enterprise Server 9 SP1 for AMD64/EM64T";}
function SetPathOes { ospath=oes; osname="Novell Open Enterprise Server for x86";}

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 ErrorConfigurationFiles
{
   distdir=$1
   chgcolor orange
   echo
   echo "ERROR: Could not find the configuration files in $distdir directory."
   echo
   chgcolor white
}

function ErrorDirectory
{
   distdir=$1
   chgcolor orange
   echo
   echo "ERROR: Could not find $distdir directory."
   echo
   chgcolor white
}

function UpdateConfigurationFiles
{
   distdir=$1
   filespec=$2

echo
echo ":$distdir"
cd $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..."
                 ###
                 CopyAndReplaceRH $file $ospath
                 ###
                 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
                 ###
                 CopyAndReplaceRH $file $ospath
                 ###

                 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 CopyAndReplaceRH
{
  tmpfile=$1
  tmpos=$2
 if [ $tmpos == "rhas4u1" ] || [ $tmpos == "rhas4u1.64" ] || [ $tmpos == "rhes4u1" ] || [ $tmpos == "rhes4u1.64" ]; then
  sed -e "s/lang en_US/lang ja_JP/g" -e "s/langsupport .*$/langsupport ja_JP/g" -e "s/keyboard \"us\"/keyboard \"jp106\"/g" -e "s/timezone .*$/timezone \-\-utc Asia\/Tokyo/g" $tmpfile > $tmpfile.sav
 else
  sed -e "s/lang en_US/lang ja_JP.eucJP/g" -e "s/langsupport .*$/langsupport \-\-default ja_JP.eucJP ja_JP.eucJP/g" -e "s/keyboard \"us\"/keyboard \"jp106\"/g" -e "s/timezone .*$/timezone \-\-utc Asia\/Tokyo/g" $tmpfile > $tmpfile.sav
 fi
}

function CopyAndReplaceSLES
{
  tmpfile=$1
  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" $tmpfile > $tmpfile.sav
}

function UpdateSLESConfigurationFiles
{
   distdir=$1
   filespec=$2

echo
echo ":$distdir"
cd $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..."
                 CopyAndReplaceSLES $file
                 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
            CopyAndReplaceSLES $file
            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 $(/sbin/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
clear
distname=$1
 cat << TheTextData1

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

 Edit the "Create Boot Environment" task and make the following changes.

 *) In case of unattendfile=[OS Name]/default.cfg
   From:  set unattendfile=[OS Name]/default.cfg
   To:    set unattendfile=[OS Name]/default.ja.cfg

 *) In case of unattendfile=[OS Name]/bl10e.cfg
   From:  set unattendfile=[OS Name]/bl10e.cfg
   To:    set unattendfile=[OS Name]/bl10e.ja.cfg

 *) In case of unattendfile=[OS Name]/default.xml
   From:  set unattendfile=[OS Name]/default.xml
   To:    set unattendfile=[OS Name]/default.ja.xml

 *) In case of unattendfile=[OS Name]/bl10e.xml
   From:  set unattendfile=[OS Name]/bl10e.xml
   To:    set unattendfile=[OS Name]/bl10e.ja.xml

*****************************************************************************
 Readme file contains more information regarding the installation 
 and configuration.
 It is strongly recommended that you read the entire document.
*****************************************************************************

TheTextData1
   echo
   echo -n "(Press Enter to next)"
   read answer
   chgcolor white
   echo
}
###############################################################################
#  MAIN EXECUTION STARTS HERE
###############################################################################

nfsserver=0.0.0.0
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.30 Rev A              "
  # Switch to Aqua Blue
  echo '____________________________________________________________________'
  echo
  chgcolor white
#
# Determine the source root
#

kssrc=/usr/rdp/osconfig
scriptname=Install

# 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

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-9] | 10 )
        ${installfunctions[$menuselection]}

        if [ $ospath == "sles9sp1" ] || [ $ospath == "sles9sp1.64" ] || [ $ospath == "oes" ]; then
        ## for SLES
         if [ -e $kssrc/$ospath ]; then
          GetFList=$(ls $kssrc/$ospath | grep \.xml | grep -v \.ja\.xml)
           ##
           if [ -n "$GetFList" ]; then
            UpdateSLESConfigurationFiles $kssrc/$ospath "$GetFList"
           else
            ErrorConfigurationFiles "$kssrc/$ospath"
           fi
           ##
         else
          ErrorDirectory "$kssrc/$ospath"
         fi
        else
         if [ -e $kssrc/$ospath ]; then
          GetFList=$(ls $kssrc/$ospath | grep \.cfg | grep -v \.ja\.cfg)
           ##
           if [ -n "$GetFList" ]; then
            UpdateConfigurationFiles $kssrc/$ospath "$GetFList"
           else
            ErrorConfigurationFiles "$kssrc/$ospath"
           fi
           ##
         else
          ErrorDirectory "$kssrc/$ospath"
         fi
        fi

         echo
         echo -n "(Press Enter to continue)"
         echo
         echo "_____________________________________________"
         read answer
         InfoMessage
         break;;
      $maxmenuselections )
         for installfunction in "${installfunctions[@]}"; do
            $installfunction
             if [ $ospath == "sles9sp1" ] || [ $ospath == "sles9sp1.64" ] || [ $ospath == "oes" ]; then
               if [ -e $kssrc/$ospath ]; then
                GetFList=$(ls $kssrc/$ospath | grep \.xml | grep -v \.ja\.xml)
                ##
                if [ -n "$GetFList" ]; then
                 UpdateSLESConfigurationFiles $kssrc/$ospath "$GetFList"
                else
                 ErrorConfigurationFiles "$kssrc/$ospath"
                fi
                ##
               else
                ErrorDirectory "$kssrc/$ospath"
               fi
             else
               if [ -e $kssrc/$ospath ]; then
                GetFList=$(ls $kssrc/$ospath | grep \.cfg | grep -v \.ja\.cfg)
                 ##
                 if [ -n "$GetFList" ]; then
                  UpdateConfigurationFiles $kssrc/$ospath "$GetFList"
                 else
                  ErrorConfigurationFiles "$kssrc/$ospath"
                 fi
                 ##
                else
                ErrorDirectory "$kssrc/$ospath"
               fi
             fi

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