#!/bin/sh

# This is installation script for "Unix Printer Driver".

show_help() {
	echo "  Usage:"
	echo ""
	echo "  ./install -h : Print this help and exit"
	echo "  ./install -i : Install driver package   (root privileges required)"
	echo "  ./install -c : Check installed files"
	echo "  ./install -d : Uninstall driver package (root privileges required)"
	echo "  ./install -u : the same as -d"
	echo ""
}

# Note: "UPD_LOG_FILE" environment variable activates logging if present
#
# outputs args into log file specified by UPD_LOG_FILE variable
log_line() {
	if [ -n "$UPD_LOG_FILE" ] ; then echo "$@" >>"$UPD_LOG_FILE" ; fi
}

show_cut_line() {
	echo "- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -"
}

# Converts possibly relative directory path into absolute form
absolute_dir_path() {
    (cd "$1" && pwd)
}

# Prints contents of "BUILD_TAG" file from directory $1.
show_build_tag() {
	DIRECTORY="$1"

	if [ -f "$DIRECTORY/BUILD_TAG" ] ; then
		BUILD_TAG=`cat "$DIRECTORY/BUILD_TAG"`
	else
		BUILD_TAG="unavailable"
	fi
	echo "    BUILD_TAG='$BUILD_TAG'"
	echo ""
}

# Prints contents of "BUILD_TAG" file from directory $1 if the file exists.
show_build_tag_if_exist() {
	DIRECTORY="$1"

	if [ -f "$DIRECTORY/BUILD_TAG" ] ; then
		BUILD_TAG=`cat "$DIRECTORY/BUILD_TAG"`
		echo "    BUILD_TAG='$BUILD_TAG'"
		echo ""
	fi
}

exit_if_not_root() {
	if [ "`id | sed -e 's/uid=\([0-9]*\)\(.*\)/\1/'`" != "0" ]; then
		echo "ERROR: You must have root privileges for this operation."
		exit 4
	fi
}

# Distribution layout:
#
# ./install -- this script
#
# BUILD_TAG -- package build info (such as "source code version", "target
# platform", "build timestamp")
#
# ./binz/ -- user visible binaries
# ./cms/ -- CMS data
# ./share/printnx/interface/pnxjmodel -- backend for AIX
# ./share/printnx/ -- helper files
# ./*.tar -- helper distribution files (ghostscript, cups-filter)

# Target layout:
#
# /etc/printnx/ -- storage for run time configuration files
#
# /etc/printnx/BUILD_TAG -- package build info
# /etc/printnx/BUILD_INFO" -- package build info
# /usr/bin/ -- user visible binaries
# /usr/lib/lpd/pnxjmodel -- backend for AIX
# /usr/share/printnx/ -- printnx helper files
# /opt/smfp-common/ -- common helper files (ghostscript, cups-filter)
# /opt/smfp-common/cms/ -- CMS data

# creates directory $1 (including all necessary parent directories)
create_dir() {
	mkdir -p "$1"
}

# copies source file $1 into destination directory $2
copy_file() {
	SRC="$1"
	DST="$2"
	log_line "copying file ${SRC} into ${DST}"
	if [ -n "$UPD_LOG_FILE" ] ; then
		cp "${SRC}" "${DST}" 2>>"$UPD_LOG_FILE"
	else 
		cp "${SRC}" "${DST}"
	fi
}

# copies source directory $1 into destination directory $2
copy_dir() {
	SRC="$1"
	DST="$2"
	log_line "copying directory ${SRC} into ${DST}"
	if [ -n "$UPD_LOG_FILE" ] ; then
		cp -r "${SRC}" "${DST}" 2>>"$UPD_LOG_FILE"
	else 
		cp -r "${SRC}" "${DST}"
	fi
}

# creates symbolic link for $1 at $2
create_symlink() {
	SRC="$1"
	DST="$2"
	log_line "creating symbolic link for ${SRC} at ${DST}"
	if [ -n "$UPD_LOG_FILE" ] ; then
		ln -sf "${SRC}" "${DST}" 2>>"$UPD_LOG_FILE"
	else 
		ln -sf "${SRC}" "${DST}"
	fi
}

# expects distribution source directory in PKG_DIR variable
# expects Operating System name in "OS" variable
# expects list of binaries in BINARIES variable
# expects list of filters in FILTERS variable
# expects list of links in LINKS variable
do_install() {
	exit_if_not_root

	show_build_tag_if_exist "$PKG_DIR"

	echo "installing ..."

	create_dir /etc/printnx

	copy_file "$PKG_DIR/BUILD_TAG" /etc/printnx/
	copy_file "$PKG_DIR/BUILD_INFO" /etc/printnx/

	for f in $BINARIES ; do
		bf="`basename $f`"
		copy_file "$PKG_DIR/binz/$bf" /usr/bin/
		chown root:bin "/usr/bin/$bf" 2>/dev/null
		chmod 555 "/usr/bin/$bf" 2>/dev/null
	done

	create_dir /opt/smfp-common/bin

	for f in $FILTERS ; do
		bf="`basename $f`"
		copy_file "$PKG_DIR/binz/$bf" /opt/smfp-common/bin/
		chown root:bin "/opt/smfp-common/bin/$bf" 2>/dev/null
		chmod 555 "/opt/smfp-common/bin/$bf" 2>/dev/null
	done

	if [ "$OS" = "AIX" ] ; then
		copy_file "$PKG_DIR/share/printnx/interface/pnxjmodel" /usr/lib/lpd/
		chown root /usr/lib/lpd/pnxjmodel 2>/dev/null
		chmod 555 /usr/lib/lpd/pnxjmodel 2>/dev/null
		chmod +s /usr/lib/lpd/pnxjmodel 2>/dev/null
	fi

	copy_dir "$PKG_DIR/cms" /opt/smfp-common/
	copy_dir "$PKG_DIR/share/printnx" /usr/share/

	( create_dir /opt/smfp-common && cd /opt/smfp-common && tar -xf "$PKG_DIR/gs.tar" )
	( create_dir /opt/smfp-common && cd /opt/smfp-common && tar -xf "$PKG_DIR/cups-filter.tar" )

	# create links
	# format: "LINK_NAME=LINK_TARGET"
	for LINK in $LINKS ; do
		echo ${LINK} | while IFS="=" read LINK_NAME LINK_TARGET ; do
			create_symlink "${LINK_TARGET}" "${LINK_NAME}"
		done
	done
}

# expects Operating System name in "OS" variable
# expects list of binaries in BINARIES variable
# expects list of filters in FILTERS variable
# expects list of links in LINKS variable
do_check() {
	echo "  installed files:"
	echo ""

	show_build_tag_if_exist /etc/printnx

	FILES=""

	FILES="${FILES} /etc/printnx/"
	FILES="${FILES} /etc/printnx/BUILD_TAG"
	FILES="${FILES} /etc/printnx/BUILD_INFO"
	FILES="${FILES} /etc/printnx/printnx.conf"

	for f in $BINARIES ; do
		FILES="${FILES} /usr/bin/$f"
	done

	for f in $FILTERS ; do
		FILES="${FILES} /opt/smfp-common/bin/$f"
	done

	if [ -d /opt/smfp-common/cms ] ; then
		FILES="${FILES} `find /opt/smfp-common/cms -type f`"
	fi

	if [ "$OS" = "AIX" ] ; then
		FILES="${FILES} /usr/lib/lpd/pnxjmodel"
	fi

	if [ -d /usr/share/printnx ] ; then
		FILES="${FILES} /usr/share/printnx"
		FILES="${FILES} `find /usr/share/printnx -type f`"
	fi

	FILES="${FILES} /opt/smfp-common/*"

	# links
	# format: "LINK_NAME=LINK_TARGET"
	for LINK in $LINKS ; do
		LINK_NAME=`echo ${LINK} | while IFS="=" read LINK_NAME LINK_TARGET ; do echo "${LINK_NAME}" ; done`
		FILES="${FILES} ${LINK_NAME}"
	done

	ls -ld ${FILES} 2>/dev/null
	if [ -n "$UPD_LOG_FILE" ] ; then
		ls -ld ${FILES} 2>/dev/null 1>>"$UPD_LOG_FILE"
	fi
}

# removes file $1
remove_file() {
	NAME="$1"
	log_line "removing file ${NAME}"
	if [ -n "$UPD_LOG_FILE" ] ; then
		rm -f "${NAME}" 2>>"$UPD_LOG_FILE"
	else 
		rm -f "${NAME}"
	fi
}

# removes directory $1
remove_dir() {
	NAME="$1"
	log_line "removing directory ${NAME}"
	if [ -n "$UPD_LOG_FILE" ] ; then
		rm -rf "${NAME}" 2>>"$UPD_LOG_FILE"
	else 
		rm -rf "${NAME}"
	fi
}

# expects Operating System name in "OS" variable
# expects list of binaries in BINARIES variable
# expects list of links in LINKS variable
do_remove() {
	exit_if_not_root

	if [ -f /etc/printnx/printnx.conf ]; then
		echo "removing printers added by the driver package"
		printer_list=`cat /etc/printnx/printnx.conf | grep "printer \"" | cut -d"\"" -f2`
		for printer in $printer_list ; do
			echo Do you want to remove the printer "$printer" [y/n] default=y :
			read answer
			if [ "$answer" = "n" ] || [ "$answer" = "N" ]; then
				echo ""
			else
				if [ "$OS" = "AIX" ] ; then
					quedev=`lsallqdev -q$printer`
					rmquedev -q$printer -d$quedev 2>/dev/null
					rmque -q$printer 2>/dev/null
				elif [ "$OS" = "SCO_SV" ] ; then
					/usr/lib/lpadmin --CUPS -x$printer
				else
					/usr/sbin/lpadmin -x$printer
				fi
			fi
		done
	fi

	echo "removing ..."

	for f in $BINARIES ; do
		remove_file /usr/bin/$f
	done

	if [ "$OS" = "AIX" ] ; then
		remove_file /usr/lib/lpd/pnxjmodel
	fi

	# remove links
	# format: "LINK_NAME=LINK_TARGET"
	for LINK in $LINKS ; do
		echo ${LINK} | while IFS="=" read LINK_NAME LINK_TARGET ; do
			remove_file "${LINK_NAME}"
		done
	done

	remove_dir /etc/printnx
	remove_dir /usr/share/printnx

	remove_dir /opt/smfp-common
}


# script execution starts here

# remove old log file if exists
if [ -n "$UPD_LOG_FILE" -a -f "$UPD_LOG_FILE" ] ; then
	rm -f "$UPD_LOG_FILE"
fi

# make sure "locale" is good one
LC_ALL=C
export LC_ALL

# helper functions expect distribution source directory in PKG_DIR variable
PKG_DIR="`dirname $0`"
# path must be absolute
PKG_DIR=`absolute_dir_path "${PKG_DIR}"`

# helper functions expect Operating System name in "OS" variable
OS="`uname -s`"

# helper functions expect list of binaries in BINARIES variable
BINARIES=""
BINARIES="${BINARIES} installprinter"
BINARIES="${BINARIES} printui"
BINARIES="${BINARIES} uninstallprinter"
if [ "$OS" = "AIX" ] ; then
	BINARIES="${BINARIES} pnxrawprt"
elif [ "$OS" = "HP-UX" ] ; then
	BINARIES="${BINARIES} pnxrawprt"
fi

# helper functions expect list of filters in FILTERS variable
FILTERS="${FILTERS} booklet"
FILTERS="${FILTERS} pscms"
FILTERS="${FILTERS} pstopclxl"
FILTERS="${FILTERS} pstospl"
FILTERS="${FILTERS} pstosplc"
FILTERS="${FILTERS} pstospjlps"

# helper functions expect list of links in LINKS variable
# format: "LINK_NAME=LINK_TARGET"
if [ "$OS" = "SCO_SV" ] ; then
	# make our filters visible to CUPS
	LINKS="${LINKS} /usr/lib/cups/filter/pstospjlps=/opt/smfp-common/bin/pstospjlps"
	LINKS="${LINKS} /usr/lib/cups/filter/rastertosamsungpclxl=/opt/smfp-common/bin/pstopclxl"
	LINKS="${LINKS} /usr/lib/cups/filter/rastertosamsungspl=/opt/smfp-common/bin/pstospl"
	LINKS="${LINKS} /usr/lib/cups/filter/rastertosamsungsplc=/opt/smfp-common/bin/pstosplc"
fi

show_cut_line
echo ""
echo "  Unix Printer Driver"
echo ""

case "$1" in
	"-d"|"-u")
		# "(de|un)-install"
		do_remove
		;;
	"-c")
		# "check install"
		do_check
		;;
	"-i")
		# "install"
		do_install
		show_cut_line
		do_check
		;;
	"-h"|"")
		# "help"
		show_build_tag_if_exist "$PKG_DIR"
		show_help
		;;
	*)
		show_build_tag_if_exist "$PKG_DIR"
		echo ""
		echo "  Warning: unknown option '$1'"
		#echo ""
		show_help
		;;
esac
show_cut_line

# script execution ends here
