instdir_cups_data() {
	local d
	if which cups-config >/dev/null 2>&1; then
		d=$(cups-config --datadir)
		log_message "INSTDIR_CUPS_DATA(actual): <$d>"
	else
		d=/usr/share/cups
		log_message "INSTDIR_CUPS_DATA(default): <$d>"
	fi
	echo "$d"
}

restart_cups() {
	if service cups status 2>&1 >/dev/null ; then
		CUPS_SCRIPT=service\ cups 
	elif test -e /etc/init.d/cups ; then
		CUPS_SCRIPT=/etc/init.d/cups 
	elif test -e /etc/init.d/cupsys ; then
		CUPS_SCRIPT=/etc/init.d/cupsys 
	else
		return 1
	fi

	if $CUPS_SCRIPT restart 2>&1 >/dev/null ; then
		return 0
	fi
	return 1
}

contains_item() {
# $1 - item to check
# $2... - items list
	local i="$1"
	while [ $# -gt 1 ]; do
		shift
		if [ "$i" = "$1" ]; then
			return 0
		fi
	done
	return 1
}

get_ppd_text() {
# $1 - PPD file
	grep '*NickName:' "$1" | awk -F\" '{print $2}'
}

get_ppd_id() {
# $1 - PPD file
	get_ppd_text "$1" | tr -d " "
}

unregister_cups_printers() {
	log_message "unregister_cups_printers"
	# FIXME: check if CUPS running and run it if not

	PPD_DIST=`ls $INSTDIR_PPD/*.ppd 2>/dev/null`
	PPD_CUPS=`ls /etc/cups/ppd/*.ppd 2>/dev/null`
	log_message "PPD_DIST: '$PPD_DIST'"
	log_message "PPD_CUPS: '$PPD_CUPS'"

	if [ "$PPD_DIST" ]; then
		for f in $PPD_DIST; do
			PPD_ID_LIST=$PPD_ID_LIST\ $(get_ppd_id "$f")
		done
		log_message "PPD_ID_LIST: '$PPD_ID_LIST'"

		PRINTERS_TO_UNINSTALL=
		for f in $PPD_CUPS ; do
			PPD_ID=$(get_ppd_id "$f")
			if contains_item "$PPD_ID" $PPD_ID_LIST; then
				PRINTERS_TO_UNINSTALL=$PRINTERS_TO_UNINSTALL\ $(basename "$f" .ppd)
			fi
		done
		log_message "Printers to be removed: '$PRINTERS_TO_UNINSTALL'"

		for printer_name in $PRINTERS_TO_UNINSTALL ; do
			print_message $MSG_REMOVING_PRINTER $printer_name ...
			lpadmin -x $printer_name 2> /dev/null || true
			lpoptions -x $printer_name > /dev/null 2>&1 || true
		done
	fi
}

install_base_files() {
	## packet specific files
	## install ppd
	mkdir_p "$INSTALL_DIR_DATA"
	mkdir_p "$INSTDIR_PPD"
	copy_directories "$DIST_PPD_PATH" "$INSTDIR_PPD"
		
	lns_p "$INSTDIR_PPD" "$INSTDIR_CUPS_PPD" 
	lns_p "$INSTDIR_PPD" "$INSTDIR_LSB_PPD" 
}

# need to create /opt/$VENDOR/mfp/uninstall/guiuninstall
compatibility_with_old_versions() {
	mkdir_p "/opt/$VENDOR_LC/mfp"
	mkdir_p "/opt/$VENDOR_LC/mfp/uninstall"
	touch_p "/opt/$VENDOR_LC/mfp/uninstall/guiuninstal"
}

do_install() {
	log_message "INSTALL PRINTER PACKET $VENDOR_LC"
	install_base_files 2>&1 | log_message
	
	restart_cups >"$LOG_FILE" 2>&1 
	RESULT="$?"
	case "$RESULT" in
	0) print_message $MSG_CUPS_RESTART_OK ;;
	1) print_message $MSG_CUPS_RESTART_FAILED ;;
	*) break ;;
	esac
	
	compatibility_with_old_versions 2>&1 | log_message
}

do_uninstall() {
	log_message "UNINSTALL PRINTER PACKET $VENDOR_LC"
	restart_cups  >"$LOG_FILE" 2>&1 
	true
}

dependencies() {
	echo "printer"
	exit 0;
}

# 1. the parameter "printer" determines the name of the directory packet will be installed
# example /opt/samsung/printer
# be careful if you change "printer" to something else
# packet will be installed in /opt/samsung/something_else
# 2. --vendor-specific parameter determines that this packet will be installed 
# in /opt/$VENDOR/printer directory otherwise it will be installed in /opt/smfp-common/printer
package_name() {
	echo "printer" 
}

# if package is vendor independent then it have to return "no"
# if not then "yes"
package_suffix(){
	echo "-script"
}


local_init() {
	INSTALL_DIR_DATA=$INSTALL_DIR/share
	INSTDIR_PPD=$INSTALL_DIR_DATA/ppd
	INSTDIR_CMS=$INSTDIR_PPD/cms

	SUBDIR_VENDOR_PPD=uld-$VENDOR_LC
	INSTDIR_LSB_PPD=/usr/share/ppd/$SUBDIR_VENDOR_PPD

	INSTDIR_CUPS_DATA=$(instdir_cups_data)
	INSTDIR_CUPS_PPD=$INSTDIR_CUPS_DATA/model/$SUBDIR_VENDOR_PPD

	# TODO: put here real path
	# now try to find ppd in uld/noarch
	DIST_PPD_PATH="$DIST_DIR/noarch/share/ppd"

	MSG_CUPS_RESTART_FAILED=$(gettext "CUPS restart FAILED")
	MSG_CUPS_RESTART_OK=$(gettext "CUPS restart OK")
	MSG_REMOVING_PRINTER=$(gettext "Removing printer")

	#if [ "$UNINSTALLMODE" ] ; then	
	#	unregister_cups_printers
	#fi
}

get_components() {
	if ! test -f /etc/cups/printers.conf && ! test -e /usr/sbin/cupsd ; then
		echo "cups"
	fi
	
	if test -z "`gs --version 2> /dev/null`" ; then
		echo "ghostscript"
	fi
}
