#!/bin/bash

if [ "$(id -u)" != "0" ]; then
   echo "This script must be run as root/sudo"
   exit 1
fi

createQueue()
{
	ret=1
    UPDPPD="/usr/share/ppd/Lexmark_PPD/Lexmark_UPD_Series.ppd"
	HBPPPD="/usr/share/ppd/Lexmark_PPD/Lexmark_Linux_HBP_Universal_Print_Driver.ppd"
	if [ -f ${UPDPPD} -a -f ${HBPPPD} ]
	then
		PS3='Select Datastreem: '
		datastream=("PS" "HBP")
		select DS in "${datastream[@]}"
		do
			case $DS in
				"PS")
					echo "Creating queue name: ${2} using Lexmark_UPD_Series.ppd"
					echo ""
					lpadmin -p "$2" -E -v "$1"  -P /usr/share/ppd/Lexmark_PPD/Lexmark_UPD_Series.ppd >/dev/null 2>&1 && echo -e "#CUPS-COMMAND\nAutoConfigure" | lp -d "$2" >/dev/null 2>&1
					ret=$?
					break
					;;
				"HBP")
					echo "Creating queue name: ${2} using Lexmark_Linux_HBP_Universal_Print_Driver.ppd"
					echo ""
					lpadmin -p "$2" -E -v "$1"  -P /usr/share/ppd/Lexmark_PPD/Lexmark_Linux_HBP_Universal_Print_Driver.ppd >/dev/null 2>&1 && echo -e "#CUPS-COMMAND\nAutoConfigure" | lp -d "$2" >/dev/null 2>&1
					ret=$?
					break
					;;
			esac
		done

	elif [ -f ${UPDPPD} ]
	then
		echo "Creating queue name: ${2} using Lexmark_UPD_Series.ppd"
		echo ""
		lpadmin -p "$2" -E -v "$1"  -P /usr/share/ppd/Lexmark_PPD/Lexmark_UPD_Series.ppd >/dev/null 2>&1 && echo -e "#CUPS-COMMAND\nAutoConfigure" | lp -d "$2" >/dev/null 2>&1
		ret=$?
	elif [ -f ${HBPPPD} ]
	then
		echo "Creating queue name: ${2} using Lexmark_Linux_HBP_Universal_Print_Driver.ppd"
		echo ""
		lpadmin -p "$2" -E -v "$1"  -P /usr/share/ppd/Lexmark_PPD/Lexmark_Linux_HBP_Universal_Print_Driver.ppd >/dev/null 2>&1 && echo -e "#CUPS-COMMAND\nAutoConfigure" | lp -d "$2" >/dev/null 2>&1
		ret=$?
	else
		ret=2
		echo "There is no Lexmark UPD driver installed !!!"
	fi

	return $ret
}

PS3='Please enter your choice: '
options=("Create" "Modify" "Delete" "Quit")
select opt in "${options[@]}" 
do
    case $opt in
        "Create")
	    echo ""
            PS3='Network or USB: '
	    type=("Network" "USB")
	    echo "Create Print Queue.."
	    echo ""
	    select NoU in "${type[@]}"
	    do
		case $NoU in
			"Network")
				echo ""
	   			echo "Enter queue name:"
	    			read newQ
				echo ""
	   			echo "Enter The Printer IP address:"
	    			read Ip
				echo ""
	    			PS3='Enter connection type: '
	    			options=("socket" "IPP" "LPD")
	    			select conn in "${options[@]}"
	    			do
					case $conn in 
		  			 "socket")
						echo ""
						protocolString="socket://$Ip"
						createQueue "$protocolString" "$newQ"
						res=$?
						if [ $res -eq 0 ]; then
							echo "$newQ is sucessfully created!"
						else
							echo "Creation of $newQ is failed!"
							exit -1;
						fi
						break
						;;
					  "IPP")
						  echo ""
						  protocolString="ipp://$Ip"
						  createQueue "$protocolString" "$newQ"
						  res=$?
						  if [ $res -eq 0 ]; then
						  	echo "$newQ is sucessfully created!"
						  else
						  	echo "Creation of $newQ is failed!"
						  	exit -1;
						  fi
				          break
						  ;;
					   "LPD")
						  echo ""
						  protocolString="lpd://$Ip/$newQ"
						  createQueue "$protocolString" "$newQ"
						  res=$?
						  if [ $res -eq 0 ]; then
						  	echo "$newQ is sucessfully created!"
						  else
						  	echo "Creation of $newQ is failed!"
						  	exit -1;
						  fi
                          break
                          ;;
				esac
				done
				break
				;;
			"USB")
				echo ""
				usburi=`lpinfo -v | grep -i usb | awk -F" " '{print $NF}'`
				echo "USB URI -- $usburi"
				echo ""
				echo "Enter queue name:"
                		read usbQ
				protocolString="$usburi"
				createQueue "$protocolString" "$usbQ"
				res=$?
				if [ $res -eq 0 ]; then
					echo "$usbQ is sucessfully created!"
				else
					echo "Creation of $usbQ is failed!"
					exit -1;
				fi
				break
				;;
	   esac
	   done
	   break
            ;;
        "Modify")
	    echo ""
            echo -e "Modify Existing Print Queue \n"
	    listQueue=`lpstat -a | awk '{print $1}'`
		if [ -z "$listQueue" ]; then
			echo "There is no existing queue in the machine!!!"
			exit -2;
		fi
		arrIndex=1
	    echo ""
        echo -e "Existing queues\n"
		for i in $listQueue 
		do
			arrQueue[${arrIndex}]="$i"
			echo "${arrIndex}) $i"
			arrIndex=`expr $arrIndex + 1`
		done
	    echo ""
	    echo -n "Select an existing queue to modify: "
	    read Qname
		re='^[0-9]+$'
        if ! [[ $Qname =~ $re ]] ; then
        echo "error: Not a number" >&2; exit 1
        fi
        if [ $Qname -ge $arrIndex ]
        then
        echo "No such queue"
        break
        fi
	    echo ""
	    echo "Enter an updated printer IP address:"
	    read newIp
            echo ""
            PS3='Enter connection type: '
	    			options=("socket" "IPP" "LPD")
	    			select conn in "${options[@]}"
	    			do
					case $conn in 
		  			 "socket")
						echo ""
						lpadmin -p "${arrQueue[$Qname]}" -E -v socket://"$newIp" >/dev/null 2>&1  && echo -e "#CUPS-COMMAND\nAutoConfigure" | lp -d "${arrQueue[$Qname]}" >/dev/null
						if [ $? -eq 0 ]; then
			echo "Print queue \"${arrQueue[$Qname]}\" is successfully updated with printer IP address $newIp !"
                else
                        echo "Modification of \"${arrQueue[$Qname]}\" is failed!"
                        exit -1;
                fi
						#lp -d "$newQ" /home/skapas/Desktop/scriptQ/autoConfig
						break
						;;
					  "IPP")
						  echo ""
                                                  lpadmin -p "${arrQueue[$Qname]}" -E -v ipp://"$newIp" >/dev/null 2>&1  && echo -e "#CUPS-COMMAND\nAutoConfigure" | lp -d "${arrQueue[$Qname]}" >/dev/null
						  
						 if [ $? -eq 0 ]; then
			echo "Print queue \"${arrQueue[$Qname]}\" is successfully updated with printer IP address $newIp !"
                else
                        echo "Modification of \"${arrQueue[$Qname]}\" is failed!"
                        exit -1;
                fi
				          break
						  ;;
					   "LPD")
						  echo ""
                                                  lpadmin -p "${arrQueue[$Qname]}" -E -v lpd://"$newIp"/"${arrQueue[$Qname]}" >/dev/null 2>&1  && echo -e "#CUPS-COMMAND\nAutoConfigure" | lp -d "${arrQueue[$Qname]}" >/dev/null
						  
						  if [ $? -eq 0 ]; then
			echo "Print queue \"${arrQueue[$Qname]}\" is successfully updated with printer IP address $newIp !"
                else
                        echo "Modification of \"${arrQueue[$Qname]}\" is failed!"
                        exit -1;
                fi
             break
             ;;
             esac
             done
             break
             ;;	    
	    	
        "Delete")
            echo -e "Delete Print Queue"
	    echo ""
	    listQueue=`lpstat -a | awk '{print $1}'`
	    	if [ -z "$listQueue" ]; then
                        echo "There is no existing queue in the machine!!!"
                        exit -2;
             	fi
		arrIndex=1
	    echo -e "Existing queues\n"
		     for i in $listQueue
                	do
                        	arrQueue[${arrIndex}]="$i"
                        	echo "${arrIndex}) $i"
                        	arrIndex=`expr $arrIndex + 1`
                	done
	    echo ""
	    echo -n "Select an existing queue to delete: "
	    read Dqname
		re='^[0-9]+$'
        if ! [[ $Dqname =~ $re ]] ; then
        echo "error: Not a number" >&2; exit 1
        fi
        if [ $Dqname -ge $arrIndex ]
        then
        echo "No such queue"
        break
        fi
	    echo ""
	    echo -n "Do you want to delete print queue "\"${arrQueue[$Dqname]}"\" ? [y or n] "
	    read ans
	    echo ""
	    if [ "${ans}" = "Y" ] | [ "${ans}" = "y" ]
	    then
		echo ""
		echo "Delete...."
		lpadmin -x "${arrQueue[$Dqname]}"
		echo ""
		echo "Print queue \"${arrQueue[$Dqname]}\" has been deleted."
		echo ""
	    elif [ "${ans}" = "N" ] | [ "${ans}" = "n" ]
	    then
		break;
	    else
		echo "Enter valid option"
	    fi
	    break
            ;;
        "Quit")
            break
            ;;
        *) echo "invalid option $REPLY";;
    esac
done
