#!/bin/sh

########################################################################
#             (c) Copyright 2007 Lexmark International, Inc.           #
#                        All rights reserved                           #
########################################################################
#                                                                      #
#  This command sets or returns available pdf viewer                   #
#                                                                      #
########################################################################

#-- set debug to false by default. To turn debug "on" set to "1"
DEBUG=0
if [ ! -z "${SANE_DEBUG_LEXMARK_NSCAN}" ] ;then
   DEBUG=1
fi


#-- initialize variables
APP_NAME=lexinkjet
PRODUCT_LINK="/usr/lexinkjet"

#-------------------------
#-- BEGIN: System Commands
#-------------------------
MORE_CMD="/bin/more"
PRINT_CMD="/bin/echo"
GREP_CMD="/bin/grep"
EGREP_CMD="/bin/egrep"
CAT_CMD="/bin/cat"
MKDIR_CMD="/bin/mkdir"
TOUCH_CMD="/bin/touch"
CHMOD_CMD="/bin/chmod"
RM_CMD="/bin/rm"

if [ -f "/bin/awk"  ];then  AWK_CMD="/bin/awk" ; else  AWK_CMD="/usr/bin/awk"  ; fi
if [ -f "/bin/sed"  ];then  SED_CMD="/bin/sed"  ; else  SED_CMD="/usr/bin/sed"  ; fi

SYS_COMMANDS="${CAT_CMD} ${PRINT_CMD} ${PRINT_CMD} ${GREP_CMD} ${EGREP_CMD} ${MORE_CMD} ${AWK_CMD} ${SED_CMD} ${CHMOD_CMD} ${RM_CMD}"
FLAG="0"
   for i in ${SYS_COMMANDS}
   do
     if [ ! -x "${i}" ]; then
        if [ "${FLAG}" = "0" ]; then
				${PRINT_CMD}
				${PRINT_CMD} "The following system command(s) were not found:"
				${PRINT_CMD}
				FLAG="1"
        fi
        ${PRINT_CMD} "   File missing  --  $i"
     fi
   done

	if [ "${FLAG}" = "1" ]; then
      ${PRINT_CMD}
      ${PRINT_CMD} "HOW TO FIX"
      ${PRINT_CMD} "----------"
      ${PRINT_CMD} "This problem can be easily corrected by manually editing the"
      ${PRINT_CMD} "script [ ${PRODUCT_LINK}/bin/${APP_NAME} ] and modify."
      ${PRINT_CMD} "the System Commands section."
      exit 1;
   fi

KNOWN_VIEWERS="xpdf
               evince
			   acroread
	           kpdf
               okular"                

VIEWER="0"

TEMPLATE="
if [ ! -x \"\${VIEWER}\" ]; then
        exit 1
fi

if [ -z \"\${1}\" ]; then
        echo \${VIEWER}
else
        \${VIEWER} \${*}
fi

exit \${?}
"

#-- usage_short text
usage_short ()
{
${CAT_CMD} << EOT

USAGE:
    lspdfviewer -h 		: help 
    lspdfviewer -l 		: list all available viewer
    lspdfviewer -d		: return the default viewer_path 
    lspdfviewer -c "viewer_path" : check if viewer_path is valid
    lspdfviewer -s "viewer_path" : set default viewer to viewer_path

EOT
}

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

lspdfviewer
    Sets the default viewer that the application will use.

USAGE:
    lspdfviewer -h 		: help 
    lspdfviewer -l 		: list all available viewer
    lspdfviewer -d		: return the default viewer_path 
    lspdfviewer -c "viewer_path" : check if viewer_path is valid
    lspdfviewer -s "viewer_path" : set default viewer to viewer_path

OPTIONS:  
    -x  Run sviewer in debug mode.

EOT

}

list_viewers()
{
	STR=""
	for PATH in ${KNOWN_VIEWERS}
	do
		PROG_PATH=`/usr/bin/whereis ${PATH}|${AWK_CMD} '{print $2}'`
		if [ -z ${PROG_PATH} ];then
			continue	
		fi
		
		STR="${STR}:${PROG_PATH}"		
	done
	echo ${STR} | ${SED_CMD} -e 's/^://g' | ${SED_CMD} -e 's/:/\n/g'
	#echo ${STR} | ${SED_CMD} -e 's/:/\n/g'

	if [ "${STR}" ];then
		return 0
	else
		return 1
	fi
}

get_default()
{
	#-- Return Values: 
	#-- 	0 - default viewer set. path stored in ${VIEWER} variable
	#-- 	1 - 'viewer' script not found
	#-- 	2 - 'viewer' script executable
	#--	3 - 'viewer' returned an error. rebuild script from scratch

	if [ ! -f "${VIEWER_SH}" ];then
		return 1;
	elif [ ! -x "${VIEWER_SH}" ];then
		return 2;
	fi	

	VIEWER=`${VIEWER_SH}`;
	if [ $? -eq 0 ];then
		${PRINT_CMD} ${VIEWER}
		return 0;
	elif [ $? -eq 1 ]; then
		return 3;
	fi
}

remove_default()
{
	#-- Return Values: 
	#-- 	0 - default viewer removed. 

	if [ -f "${VIEWER_SH}" ];then
		${RM_CMD} ${VIEWER_SH}
	fi
}

check_viewer()
{
	#-- Return Values: 
	#-- 	0 - viewer good to go 
	#-- 	1 - viewer not found
	#-- 	2 - viewer not executable
	#-- 	3 - viewer not specified
	
	RET="0";

	if [ ! "${VIEWER}" ];then
		return 3;
	elif [ ! -f "${VIEWER}" ];then
		return 1;
	elif [ ! -x "${VIEWER}" ];then
		return 2;
	else
		return 0;
	fi
}

set_viewer()
{
	#-- Return Values: 
	#-- 	0 - viewer good to go 
	#-- 	1 - viewer not found
	#-- 

	if [ ! -f "${VIEWER}" ];then
		return 1;
	elif [ ! -x "${VIEWER}" ];then
		return 2;
	fi

	if [ ! -e "${USER_DIR}" ];then
		${MKDIR_CMD} ${USER_DIR}
	fi	

	if [ -f "${VIEWER_SH}" ];then
		${RM_CMD} ${VIEWER_SH}
	fi

	${PRINT_CMD} "#!/bin/sh" > ${VIEWER_SH}
	${PRINT_CMD} "VIEWER=\"${VIEWER}\"" >> ${VIEWER_SH}
	${PRINT_CMD} "${TEMPLATE}" >> ${VIEWER_SH}

        ${CHMOD_CMD} 755 ${VIEWER_SH}

	return 0;
}

pdebug()
{
	if [ "${DEBUG}" = "1" ];then
		printf "${1}\n" >&2
	fi
}

#-- parse arguments begin

while getopts xhldrc:s: arg
do
	case "${arg}" in
		x)
			DEBUG="1"
			;;
		h)
			RUNMODE="help"
			;;
		l)	
			RUNMODE="list"
			;;
		d)	
			RUNMODE="default"
			;;
		r)	
			RUNMODE="remove"
			;;
		c)	
			RUNMODE="check"
			VIEWER="${OPTARG}"
			if [ ! ${VIEWER} ];then
				${VIEWER} = ""
			fi
			;;
		s)	
			RUNMODE="set"
			VIEWER="${OPTARG}"
			;;
	esac
done

#PDHOME=`${PRODUCT_LINK}/bin/lshome`
#if [ $? -ne 0 ];then
	PDHOME=${HOME}
#fi

USER_DIR="${PDHOME}/.${APP_NAME}"
VIEWER_SH="${PDHOME}/.${APP_NAME}/viewer"

if [ "${RUNMODE}" = "help" ]; then
	usage_long | ${MORE_CMD}
        exit 1
elif [ "${RUNMODE}" = "list" ]; then
	list_viewers
	exit $?
elif [ "${RUNMODE}" = "default" ]; then
	get_default
	if [ $? -gt 1 ]; then
		${RM_CMD} ${VIEWER_SH}
		exit 2
	fi
	exit 0
elif [ "${RUNMODE}" = "remove" ]; then
	remove_default
	exit 0
elif [ "${RUNMODE}" = "check" ]; then
	check_viewer ${VIEWER}
	exit $?
elif [ "${RUNMODE}" = "set" ]; then
	set_viewer ${VIEWER}
	exit $?
fi

