#!/bin/bash
#
######################################################################
# 
# TITLE:    HP ProLiant Essentials Rapid Deployment Pack Linux Edition
# Japanese Language Kit (Uninstaller)
# VERSION:  1.0 Rev A
# LANGUAGE:  Japanese (JP)
#
# DESCRIPTION:  This SoftPaq contains the Japanese Language Kit
# Installer/Uninstaller, Management agents JLK and Readme file.
# 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
######################################################################
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
       ;;
       orange-blue )
        c1=44
        c2=31
       ;;
       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 ListFiles
{
 distdir=$1
 filespec=$2
 if [ -d $distdir ]; then
# cd $distdir 2>\/dev\/null
 cd $distdir
 echo ": $distdir"
 for i in $filespec; do
 if [ -e $i ]; then
   echo "      $i"
   flg=1
  fi
 done
#if [ $flg == "0" ]; then echo "Couldn't find the installed Japanese Language Kit files in $distdir";fi 
fi
 cd - 
}

function RemoveFiles
{
  distdir=$1
  filespec=$2
 if [ -d $distdir ]; then
  cd $distdir
  echo ": $distdir"
  for i in $filespec; do
   if [ -e $i ]; then
    chgcolor orange
    echo -n "      $i --> Removing..."
    rm -f $i
    chgcolor white
    echo " Done"
   fi
  done
 fi
 cd - 
}

function InfoMessage
{

distname=$1
echo "... Done!"
chgcolor green
 cat << TheTextData
-----------------------------------------------------------------------------
IMPORTANT! You must update the distribution scripted 
install events on your Deployment Server.

Edit the "Run Script - Install OS" task and make the following change.

  *)In case of "ksfile=ks.ja.cfg"
  From:  set ksfile=ks.ja.cfg
  To:    set ksfile=ks.cfg

  *)In case of "ksfile=xxxxx.ks.ja.cfg"
  From:  set ksfile=xxxxx.ks.ja.cfg
  To:    set ksfile=xxxxx.ks.cfg

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

###############################################################################
#  MAIN EXECUTION STARTS HERE
###############################################################################
chgcolor white
clear
# Switch to Aqua Blue
chgcolor white-blue
echo '                                                                    '
echo "    HP ProLiant Essentials Rapid Deployment Pack Linux Edition      "
chgcolor aqua-blue
echo -n "    Japanese Language Kit ( "
chgcolor orange-blue
echo -n "Uninstaller"
chgcolor aqua-blue
echo " )  VERSION:  1.0 Rev A      "
echo '____________________________________________________________________'
echo
chgcolor white

echo "Searching..."

filepath=/usr/cpqrdp/ss.640
osdir=$(ls $filepath)

jlkasm=hpasm-jlk-6.40.0-16.i386.rpm
jlkstor=cmastor-jlk-6.40.0-8.i386.rpm
jlknic=cmanic-jlk-6.40.0-3.i386.rpm

ksmldl=ks.ja.cfg
ksbl10e=bl10e.ks.ja.cfg
ksbl20p=bl20p.ks.ja.cfg
ksbl40p=bl40p.ks.ja.cfg

ulksmldl=mldl.ja.xml
ulksbl10e=bl10e.ja.xml
ulksbl20p=bl20p.ja.xml
ulksbl40p=bl40p.ja.xml

jlkname="$jlkasm $jlkstor $jlknic"
ksname="$ksmldl $ksbl10e $ksbl20p $ksbl40p"
ulksname="$ulksmldl $ulksbl10e $ulksbl20p $ulksbl40p"
ul10name="ul10ja.sh"

for osdirname in $osdir; do
  ulkspath=$filepath/$osdirname/control
  kspath=$filepath/$osdirname
  jlkpath=$filepath/$osdirname/csp
  flg=0

   if [ $osdirname == "ul10" ]; then
      ListFiles $ulkspath "$ulksname"
      ListFiles $jlkpath "$ul10name"
   else
      ListFiles $kspath "$ksname"
   fi
      ListFiles $jlkpath "$jlkname"

  if [ $flg == 1 ]; then
   echo
   echo -n "Would you like to remove the file(s)? (y/n) : "
     while [ $prompt=[yn] ]; do
       read prompt
       case $prompt in
       y*)
        if [ $osdirname == "ul10" ]; then
         RemoveFiles $ulkspath "$ulksname"
         RemoveFiles $jlkpath "$ul10name"
        else
         RemoveFiles $kspath "$ksname"
        fi
         RemoveFiles $jlkpath "$jlkname"

        echo -n "(Press Enter to continue)"
        read answer
        echo
        echo "--------------------------------------------------"
        echo "Searching..."
        break;;
       n*)
        break;;
       *)
        echo -n "Would you like to remove the file(s)? (y/n) : ";;
       esac
     done
  fi
done
if [ $flg == 1 ]; then
 InfoMessage
else
 chgcolor orange
 echo "Couldn't find the installed Japanese Language Kit files."
 echo
 chgcolor white
fi
# end of script
