#!/bin/sh

WHOAMI=/usr/ucb/whoami
PKGRM=/usr/sbin/pkgrm
PKGINFO=/usr/bin/pkginfo
ECHO=/usr/bin/echo
LS=/usr/bin/ls
WC=/usr/bin/wc
RM=/usr/bin/rm
SED=/usr/bin/sed
CAT=/usr/bin/cat
GREP=/usr/bin/grep
LOG_FILE="/tmp/htm-uninstall-cd1.log"
DATE=/usr/bin/date
TM_BIN=/opt/HiCommand/TuningManager/bin		# 40H0123 C
EGREP=/usr/bin/egrep						# 40H0635 A
HTMCD1_PKGINFO=/var/sadm/pkg/HTMCD1/pkginfo # 50H0091 A
CUT=/usr/bin/cut                            # 50H0091 A

# 40H0401 A START 
#NAME : htmInstallLog
##DETAIL : write htm log into logfile
##PARAM2 :message
##PARAM3 :line feed
htmInstallLog() {
	if [ ! -z "$1" ]; then
		printf "`date '+%y/%m/%d %H:%M:%S'` $1\n" >> $LOG_FILE
	fi
}
# 40H0401 A END 

#NAME : checkZero
#DETAIL : パラメータが空文字かどうか判定する
#引数1  : チェック対象文字列
#RETURN CODE :  1 - 空文字,  0 - 空文字以外
checkZero() {												# 40H0635 A start
  if [ `echo "$1" | $EGREP -c "^.+$"` -eq 0 ]; then
    return 1
  fi
  return 0
}															# 40H0635 A end

# 50H0091 A START
#NAME : putVersionToLog
#DETAIL : バージョン情報をインストールログに出力する。                          55H0008 C
#       : 初期メッセージに出力するバージョン情報文字列を作成を作成する。        55H0008 C
#バージョン情報文字列：v.r. 0 (v.r.0-g)                                         55H0008 C
#  v   : バージョン番号。                                                       55H0008 C
#  r   : リリース番号。                                                         55H0008 C
#  0   : 0固定。                                                                55H0008 C
#  g   : 限定コード（01〜99）。                                                 55H0008 C
#OUTPUT:INIT_MSG_VERSION_STR:初期メッセージに表示する詳細バージョン情報文字列   55H0008 C
#RETURN CODE  なし
putVersionToLog() {

    #前バージョン(R)を取得
    $PKGINFO -q HTMCD1
    if [ $? -eq 0 ]; then    # アップグレード・修復インストール
        LOG_PRE_VERSION=`${CAT} ${HTMCD1_PKGINFO} | ${GREP} "^VERSION=" | ${SED} 's/^.*=//g'| ${CUT} -d. -f1`
        LOG_PRE_REVISION=`${CAT} ${HTMCD1_PKGINFO} | ${GREP} "^VERSION=" | ${SED} 's/^.*=//g'| ${CUT} -d. -f2`
        LOG_PRE_SLANT=`${CAT} ${HTMCD1_PKGINFO} | ${GREP} "^VERSION=" | ${SED} 's/^.*=//g'| ${CUT} -d'-' -f2`
        PRE_VERSION_STR=`printf "%s.%s.0-%s" ${LOG_PRE_VERSION} ${LOG_PRE_REVISION} ${LOG_PRE_SLANT}`
        htmInstallLog "INSTALLED VERSION : ${PRE_VERSION_STR}"

        # 55H0008 A START
        # 初期メッセージに出力するバージョン情報文字列を作成
        INIT_MSG_VERSION_STR=`printf "%s.%s.0(%s.%s.0-%s)" ${LOG_PRE_VERSION} ${LOG_PRE_REVISION} ${LOG_PRE_VERSION} ${LOG_PRE_REVISION} ${LOG_PRE_SLANT}`
        # 55H0008 A END
    fi
}
# 50H0091 A END

# is CD-1 actually installed
if [ ! -d /var/sadm/pkg/HTMCD1 ]; then
	$ECHO "HiCommand Tuning Manager - Server Part 1 is already uninstalled"    # 50H0113 C
	exit
fi
STAMP=`$DATE`
htmInstallLog "\n*************************************"	# 40H0401 C
htmInstallLog "Beginning TM-CD1 uninstall ( $STAMP )"	# 40H0401 C

# バージョン情報をログファイルへ出力する。    # 50H0091
putVersionToLog                               # 50H0091

PACKAGES="HTMCD1"
INSTALL_DIR=`$CAT /var/sadm/pkg/HTMCD1/pkginfo | $GREP HTMINSTALL | $SED 's/^.*=//g'`

if [ `$WHOAMI` != "root" ]; then
    echo "You must be root to uninstall HiCommand Tuning Manager"
    htmInstallLog "You must be root to uninstall HiCommand Tuning Manager"	# 40H0401 C
    exit
fi



# 40H0187 A START
printf "\nUninstalling HiCommand Tuning Manager - Server Part 1 - ${INIT_MSG_VERSION_STR}"    # 50H0113 C 55H0008 C 55H0010 C
printf "\nDo you wish to start the uninstallation? \n"
while [ 1 ];
do
	printf "To continue, enter Y. (Default Y) Y/N > "	# 40H0437 A
    read USER_RESPONSE
	htmInstallLog "Uninstalling HiCommand Tuning Manager - CD1. user input:[$USER_RESPONSE]"  # 40H0635
	    
	checkZero "$USER_RESPONSE" 				# 40H0635 A
	ISZERO=$?                               # 40H0635 A
	if [ $ISZERO -eq 1 ]; then              # 40H0635 C
        break
    fi
	case "${USER_RESPONSE}" in			# 40H0635 start
		[Yy])
            break
			;;
		[Nn])
	    	printf "Exiting..\n"
	    	htmInstallLog "User selected to cancel uninstall\n" >> $LOG_FILE 
	    	exit
			;;
	esac								# 40H0635 end

done
# 40H0187 A END

$PKGINFO HTMCD2 > /dev/null 2>&1
if [ $? -eq 0 ]; then
    echo "*** Uninstall first HiCommand Tuning Manager - Server Part 2"    # 50H0113 C
    htmInstallLog "*** Uninstall first HiCommand Tuning Manager - Server Part 2" >> $LOG_FILE    # 50H0113 C
    exit
fi

for package in $PACKAGES
do
    printf "Uninstalling package: $package\n"
    htmInstallLog "Uninstalling package: $package\n" >> $LOG_FILE
    $PKGRM -n -a "/opt/HiCommand/TuningManager/uninstall/dontask" $package 2>> $LOG_FILE	# 40H0296 C 
    if [ $? -eq 0 ]; then
	htmInstallLog "$package successfully removed\n" >> $LOG_FILE
    else
	printf "Error in removal of $package\n"
    fi
done

if [ -d /opt/HiCommand/TuningManager ]; then

	if [ -d /opt/HiCommand/TuningManager/help ]; then
		rm -rf /opt/HiCommand/TuningManager/help
	fi

	# now check if TM dir is empty

	if [ ` ls /opt/HiCommand/TuningManager | wc -l` -eq 0 ]; then
		cd /opt/HiCommand
		rm -rf /opt/HiCommand/TuningManager
	fi

fi

if [ -d /opt/HiCommand ]; then
	
	if [ ` ls /opt/HiCommand | wc -l` -eq 0 ]; then
		cd /opt
		$RM -rf /opt/HiCommand
	fi
fi

if [ -f /opt/HiCommand/TuningManager/.hckey ]; then
	$RM /opt/HiCommand/TuningManager/.hckey
fi

if [ -f /opt/HiCommand/TuningManager/.hckey2 ]; then
	$RM /opt/HiCommand/TuningManager/.hckey2
fi

if [ -d $INSTALL_DIR ]; then
	
	if [ `$LS $INSTALL_DIR | $WC -l` -eq 0 ]; then
		$RM -rf $INSTALL_DIR
	fi
fi

echo "Done uninstalling HiCommand Tuning Manager - Server Part 1"    # 50H0113 C
htmInstallLog "Done uninstalling HiCommand Tuning Manager - Server Part 1" >> $LOG_FILE    # 50H0113 C
