#!/bin/sh
########################################################################
#             (c) Copyright 2004 Lexmark International, Inc.           #
#                        All rights reserved                           #
########################################################################
#                                                                      #
# This command shows the license agreement.                            #
#                                                                      #
########################################################################

DEBUG=0
#-- Set UMASK

#-- run_command_line variable for commandline G option
run_command_line="true"
skip_prompts="no"
force=""


PRODUCT_LINK="/usr/genprint"
SCRIPTS_DIR="${PRODUCT_LINK}/bin/.scripts"
OUTPUTPATH="${PRODUCT_LINK}/docs"


#-- load the shared script
. ${SCRIPTS_DIR}/shared

#-- initialize variables:
initialize user

PLUGIN_SRC=${ROOT_SPOOL_DIR}/.${PRODUCT_NAME}/plugins
TMP_DIR="${PLUGIN_SRC}/tmp/$$"
PLUGIN_LOG_DIR="${PLUGIN_SRC}/logs"
PLUGIN_LOG="${PLUGIN_LOG_DIR}/plugin_manager.log"
CDATE="`${DATE_CMD}`"

#-- usage text
usage_long()
{
${CAT_CMD} << EOT

plugin_manager
    Installs, lists, and removes plugins or software updates.

USAGE:
    plugin_manager -l
    plugin_manager -i filename [-s]
    plugin_manager -r plugin_name [-s]
    plugin_manager -h

OPTIONS:
    -i filename
       Specify a plugin filename to install.
      
    -h
       Display this help and exit.

    -l
       Lists all installed plugins.

    -r plugin_name
       Specify a plugin name to remove.

    -s 
       Skip installation and removal prompts for automation.

    -v 
       Display version information and exit.

EOT
}

usage_short() {
${CAT_CMD} << EOT

USAGE:
    plugin_manager -l
    plugin_manager -i filename [-s]
    plugin_manager -r plugin_name [-s]
    plugin_manager -h

Try 'plugin_manager -h' for more information.

EOT

}

#-- LOG PLUGIN
log_message()
{
	message="${1}"
	if [ ! -d "${PLUGIN_LOG_DIR}" ];then
		${MKDIR_CMD} -p ${PLUGIN_LOG_DIR}
	fi
	${PRINT_CMD} "${CDATE} ---> ${message}" >> ${PLUGIN_LOG}
}

#-- remove tmp dir
removeTMPDIR()
{
	if [ -d "${TMP_DIR}" ]; then
		cd ${PLUGIN_SRC}
		${RM_CMD} -rf ${TMP_DIR}
		if [ "${?}" != "0" ];then
			log_message "Removing TMP_DIR [ ${TMP_DIR} ] FAILED."
		fi
	fi
}

#-- install plugin
check_for_root()
{
   is_privileged_user
   if [ $? -ne 0 ];then
      ${PRINT_CMD} "You must be ROOT to install or remove plugins."
      exit 99
   fi
}

show_short_details()
{
		${PRINT_CMD} "-------------------------------------"
		${PRINT_CMD} "         Plugin Manager"
		${PRINT_CMD} "-------------------------------------"
		${AWK_CMD} -F"=" '{
					if      ( $1 == "NAME" ) name=$2
					else if ( $1 == "DESC" ) desc=$2
					else if ( $1 == "VERSION" ) version=$2
				}
				END {
					printf("Plugin Name\t: %s\nDescription\t: %s\nVersion\t\t: %s\n",name,desc,version)
				}' ${1}
		${PRINT_CMD} "-------------------------------------"
}

checkfile()
{
	file=${1}
	if [ ! -f "${file}" ];then
		if [ "${run_command_line}" = "true" ]; then
			${PRINT_CMD} "Plugin Installation Failed - Plugin file does not exist."
		else
			log_message "File [ ${file} ] does not exist."
			${PRINT_CMD} ${PLUGIN_NAME}
		fi
		exit 8
	fi

	${MKDIR_CMD} -p ${TMP_DIR}
	if [ ${?} -ne 0 ];then
		if [ "${run_command_line}" = "true" ]; then
			${PRINT_CMD} "Plugin Installation Failed - Failed to create tmp directory."
		else
			log_message "Creating TMP_DIR [ ${TMP_DIR} ] FAILED."
		fi
		exit 8
	fi
	OUT=`${CAT_CMD} "${file}" | ${UNCOMPRESS_CMD} 2>&1 > ${TMP_DIR}/pluginfile.tar`
	if [ "${?}" != "0" ]; then
		${PRINT_CMD} "Plugin Installation Failed - Corrupt plugin file."
		${PRINT_CMD} "${OUT}" | ${EGREP_CMD} "not in compressed|not in gzip" >/dev/null 2>&1
		if [ $? -eq 0 ];then
			${PRINT_CMD} "The plugin file is not compressed."
		else
			${PRINT_CMD} "${OUT}"
		fi
		removeTMPDIR
		log_message "Uncompress file ${file} failed."
		log_message "${OUT}"
		exit 7
	fi
	cd ${TMP_DIR}
   OUT=`${TAR_CMD} xf pluginfile.tar 2>&1`
	if [ "${?}" != "0" ]; then
		${PRINT_CMD} "Plugin Installation Failed - Corrupt plugin file."
		${PRINT_CMD} "${OUT}"
		removeTMPDIR
		log_message "Un-tar file pluginfile.tar failed."
		log_message "${OUT}"
		exit 7
	fi
	
	PLUGIN_NAME=`${FIND_CMD} * -type d | ${SED_CMD} 1q 2>&1`
	if [ -f "${PLUGIN_NAME}/properties" -a  -f "${PLUGIN_NAME}/plugin" ];then
		OUT=`${PLUGIN_NAME}/plugin -p ${PRODUCT_NAME} -z -v 2>&1`
		rc=${?}
		if [ $rc -ne 0 ];then
			${PRINTF_CMD} "${OUT}\n"
			removeTMPDIR
			log_message "Plugin Verify Failed. ${OUT}"
			exit $rc
		fi
	else
		log_message "Invalid Plugin file.  The properties or plugin file is missing."
		${PRINT_CMD} "Plugin Installation Failed - Corrupt plugin file."
		removeTMPDIR
		exit 7
	fi
}

manage_plugin_install()
{
	OS_TYPE1="`${UNAME_CMD}`"
	OS_TYPE2="`${UNAME_CMD} -a | ${AWK_CMD} '{print $6}'`"
	if [ "${OS_TYPE1}" = "SunOS" -a "${OS_TYPE2}" = "i386" ];then
		PLUGS="AAT_DRV|AAU_DRV|AAV_DRV|AAW_DRV|AAX_DRV|AAY_DRV|AAZ_DRV|ABB_DRV|ABC_DRV|ABD_DRV|ABH_DRV"
		PLUGS="${PLUGS}|AAT_ASIAN_DRV|AAU_ASIAN_DRV|AAV_ASIAN_DRV|AAW_ASIAN_DRV"
		${PRINT_CMD} "${PLUGIN_NAME}" | ${EGREP_CMD} "${PLUGS}" >/dev/null 2>&1
		if [ $? -eq 0 ];then
			${PRINT_CMD} "Plugin Installation Failed - Plugin is incompatible with this Operating System."
			removeTMPDIR
			exit 1
		fi
	fi
}

display_eula()
{
more -dfpcsu ${TMP_DIR}/${PLUGIN_NAME}/docs/license.txt

while [ true ]
do
        ${PRINTF_CMD} "You must agree to the terms of the End-User License Agreement."
        ${PRINTF_CMD} "To Accept the license agreement, type 'agree' and press <enter>."
        ${PRINTF_CMD} "::"
        read answer2
        if [ "${answer2}" = "agree" ]; then
                break
        else
                ${PRINTF_CMD} "You must agree to the license agreement before you can continue."
                exit 1
        fi
done
}




check_eula()
{
	is_installed="no"
	check_for_root
 	checkfile "${PLUGIN_FILE}"
	manage_plugin_install
	${PRINT_CMD} ${PLUGIN_NAME} | ${GREP_CMD} -v "ASIAN" | ${EGREP_CMD} "AAT|AAU|AAV|AAW" >/dev/null 2>&1
	if [ "${?}" = "0" ];then 
		if [ -d "${PLUGIN_SRC}/${PLUGIN_NAME}" ]; then
			is_installed="yes"
		fi
	else
		OUT=`${TMP_DIR}/${PLUGIN_NAME}/plugin -p "${PRODUCT_NAME}" -c "${PLUGIN_SRC}/${PLUGIN_NAME}" -z 2>&1`
		RC="${?}"
		if   [ "${RC}" = "1" ];then 
			  is_installed="yes"
		elif [ "${RC}" = "2" ];then 
			  is_installed="upgrade"
		fi
	fi
	if [ "${is_installed}" = "yes" ]; then
		if [ "${run_command_line}" = "true" ]; then
			show_short_details ${PLUGIN_SRC}/${PLUGIN_NAME}/properties
			${PRINT_CMD} "Plugin is already installed."
		else
			${PRINT_CMD} ${PLUGIN_NAME}
		fi
		removeTMPDIR
		exit 1
	fi

	if [ -f "${TMP_DIR}/${PLUGIN_NAME}/docs/license/en/license.txt" ]; then
		if [ ! -d "/tmp" ]; then
			${MKDIR_CMD} /tmp	
		fi 
		${CP_CMD} -R ${TMP_DIR}/${PLUGIN_NAME}/docs/ /tmp
		${PRINT_CMD} ${PLUGIN_NAME}
		removeTMPDIR
		exit 9
	fi
	${PRINT_CMD} ${PLUGIN_NAME}
	removeTMPDIR
	exit 10

}


install_plugin()
{
	is_installed="no"
	check_for_root
 	checkfile "${PLUGIN_FILE}"
	manage_plugin_install
	${PRINT_CMD} ${PLUGIN_NAME} | ${GREP_CMD} -v "ASIAN" | ${EGREP_CMD} "AAT|AAU|AAV|AAW" >/dev/null 2>&1
	if [ "${?}" = "0" ];then 
		if [ -d "${PLUGIN_SRC}/${PLUGIN_NAME}" ]; then
			is_installed="yes"
		fi
	else
		OUT=`${TMP_DIR}/${PLUGIN_NAME}/plugin -p "${PRODUCT_NAME}" -c "${PLUGIN_SRC}/${PLUGIN_NAME}" -z 2>&1`
		RC="${?}"
		if   [ "${RC}" = "1" ];then 
			  is_installed="yes"
		elif [ "${RC}" = "2" ];then 
			  is_installed="upgrade"
		fi
	fi
	if [ "${is_installed}" = "yes" ]; then
		if [ "${run_command_line}" = "true" ]; then
			show_short_details ${PLUGIN_SRC}/${PLUGIN_NAME}/properties
			${PRINT_CMD} "Plugin is already installed."
		else
			${PRINT_CMD} ${PLUGIN_NAME}
		fi
		removeTMPDIR
		exit 1
	fi

	if [ "${run_command_line}" = "true" ]; then
		if [ -f "${TMP_DIR}/${PLUGIN_NAME}/docs/license.txt" ]; then
			display_eula
		fi
	fi

	if [ "${run_command_line}" = "true" -a "${skip_prompts}" = "no" ]; then
		show_short_details ${TMP_DIR}/${PLUGIN_NAME}/properties
		continue="yes"
		while [ "$continue" = "yes" ]; do
			${PRINTF_CMD} "Install plugin ${PLUGIN_NAME} (y/n)? "
			read flag
			case ${flag} in
				yes|y|YES)
							continue="no"
							;;
				n|no|NO)
							removeTMPDIR
							exit 1
							;;
			esac
		done
	fi

	if [ "${is_installed}" = "upgrade" ]; then
		OUT=`${PLUGIN_SRC}/${PLUGIN_NAME}/plugin -p ${PRODUCT_NAME} ${USERARG} -z -r -F 2>&1`
		${RM_CMD} -rf ${PLUGIN_SRC}/${PLUGIN_NAME}
	fi

	${MOVE_CMD} ${TMP_DIR}/${PLUGIN_NAME} ${PLUGIN_SRC}/${PLUGIN_NAME}
	removeTMPDIR
	OUT=`${PLUGIN_SRC}/${PLUGIN_NAME}/plugin -p ${PRODUCT_NAME} ${USERARG} -z -i 2>&1`
	return $?
}

#-- remove plugin
remove_plugin()
{
	check_for_root
	if [ ! -d "${PLUGIN_SRC}/${PLUGIN_NAME}" ]; then
		${PRINT_CMD} "Plugin [ ${PLUGIN_NAME} ] is not installed."
		exit 1
	fi

	if [ "${run_command_line}" = "true" -a "${skip_prompts}" = "no" ]; then
		show_short_details ${PLUGIN_SRC}/${PLUGIN_NAME}/properties
		continue="yes"
		while [ "$continue" = "yes" ]; do
			${PRINTF_CMD} "Remove ${PLUGIN_NAME} (y/n)? "
			read flag

			if [ "${flag}" = "yes" -o "${flag}" = "y" -o "${flag}" = "YES" ];then
				continue="no"
			elif [ "${flag}" = "no" -o "${flag}" = "n" -o "${flag}" = "NO" ];then
				exit 1	
			fi
		done
	fi
	var=`${PLUGIN_SRC}/${PLUGIN_NAME}/plugin -p ${PRODUCT_NAME} -z -r ${force} 2>&1`
	rc="${?}"

	if [ "${rc}" = "0" ];then
		${RM_CMD} -rf ${PLUGIN_SRC}/${PLUGIN_NAME}
		${PRINT_CMD} "Plugin has been removed successfully."
	elif [ "${rc}" = "4" ];then
		if [ "${run_command_line}" = "false" ]; then
			${PRINTF_CMD} "${var}\n" | ${GREP_CMD} "QUEUES:" | ${SED_CMD} -e 's/QUEUES: *//'
		else
			${PRINTF_CMD} "${var}\n"
		fi
	else
		${PRINTF_CMD} "${var}\n"
	fi
	exit ${rc}
}

#-- list plugin
list_plugins()
{
	if [ ! -d "${PLUGIN_SRC}" ];then
		return
	fi
	if [ -z "${1}" ]; then
		var=`${LS_CMD} -1 ${PLUGIN_SRC} |${EGREP_CMD} -v "logs|tmp"`
	else
		var=`${LS_CMD} -1 ${PLUGIN_SRC} |${EGREP_CMD} ${1} | ${EGREP_CMD} -v "logs|tmp"`
	fi

	for plugin in `${PRINT_CMD} $var`	
	do
		if [ -f ${PLUGIN_SRC}/${plugin}/properties ];then
		${AWK_CMD} -F"=" 'BEGIN { 
								  OUTFILEPATH="'${OUTPUTPATH}'" 
						        }
						{ 
                        if      ( $1 == "NAME" ) name=$2
                        else if ( $1 == "DESC" ) desc=$2
                        else if ( $1 == "FULL_DESC" ) full_desc=$2
                        else if ( $1 == "OS_SUPPORT" ) os_support=$2
                        else if ( $1 == "OEM" ) oem=$2
                        else if ( $1 == "APP_VER" ) app_ver=$2
                        else if ( $1 == "DATE" ) date=$2
                        else if ( $1 == "VERSION" ) ver=$2
                        else if ( $1 == "INSTALLED_DATE" ) installed_date=$2
                        else if ( $1 == "INSTALLED_BY" ) installed_by=$2
                  }
                  END {
								if ( installed_date == "" ) { installed_date="none"; }
								if ( installed_by== "" )    { installed_by="none"; }
								if ( full_desc == "" )      { full_desc="none"; }
								if ( "true" ~ /'"$run_command_line"'/ ) {
                      		printf("Plugin Name\t: %s\nDescription\t: %s\n",name,desc)
									printf("Version\t\t: %s\nInstalled Date\t: %s\nInstalled By\t: %s\n\n",ver,installed_date,installed_by) 
								}
								else {
                      		printf("%s:%s:%s:%s:%s:%s\n",name,desc,ver,installed_date,installed_by,full_desc)
							print "PLUGIN " ver > OUTFILEPATH"/"name".txt"
								}
                      }' ${PLUGIN_SRC}/${plugin}/properties
 	fi	
	done
	return $?
}

while getopts Gsli:c:r:p:hu:Fv arg
do
  case "${arg}" in
	G) run_command_line="false"
     	;;

  	i) PLUGIN_FILE=${OPTARG}
		runtype="install"
     	;;
  	r) PLUGIN_NAME=${OPTARG}
		runtype="remove"
     	;;
  	l) PLUGIN_NAME=${OPTARG}
		runtype="list"
     	;;
  	p) PLUGIN_NAME=${OPTARG}
		runtype="single"
     	;;
   s) skip_prompts="yes"
      ;;
	u) USERARG="-u ${OPTARG}" 
     	;;
	F) force="-F"
     	;;
	c) PLUGIN_FILE=${OPTARG}
		runtype="check_eula"
		run_command_line="false"
     	;;
	h) usage_long 
      exit 99
		;;
	v) display_version
		exit
     ;;
  esac 
done

if [ ! -z "${PLUGIN_FILE}" -a ! -z "${PLUGIN_NAME}" ];then
	usage_short
	exit 99
fi

if   [ "${runtype}" = "install" ];then  
		install_plugin "${PLUGIN_FILE}"
		rc=${?}
		if [ "${rc}" = "0" ]; then
			if [ "${run_command_line}" = "false" ]; then
				${PRODUCT_LINK}/bin/plugin_manager -G -p ${PLUGIN_NAME}	
			else
				${PRINT_CMD} "Plugin was installed successfully."
			fi
			exit 0
		else
			if [ -d "${PLUGIN_SRC}/${PLUGIN_NAME}" ]; then
				${RM_CMD} -rf ${PLUGIN_SRC}/${PLUGIN_NAME}
			fi
			${PRINTF_CMD} "${OUT}\n"
      	exit ${rc}
		fi
elif [ "${runtype}" = "remove" ];then  
		remove_plugin
elif [ "${runtype}" = "list" ];then  
		list_plugins
elif [ "${runtype}" = "single" ];then  
		list_plugins ${PLUGIN_NAME}
elif [ "${runtype}" = "check_eula" ];then  
		check_eula "${PLUGIN_FILE}"
else
	usage_short
	exit 99
fi
