#!/bin/bash
# printpdf - script
# usage: 
# printpdf file.pdf
# printpdf file1.pdf file2.pdf
# printpdf *.pdf
# by i_magnific0
# Modified by jab2870

#COLORS=" -nb #303030 -nf khaki -sb #CCFFAA -sf #303030"
#if dmenu --help 2>&1 | grep -q '\[-rs\] \[-ni\] \[-nl\] \[-xs\]'
#then
#        DMENU="dmenu -i -xs -rs -l 10" # vertical patch
#else
#        DMENU="dmenu -i" # horizontal, oh well!
#fi

DMENU="rofi -dmenu"

printer=$( lpstat -p | awk '{print $2}' | $DMENU -p 'Printer:' $COLORS | perl -p -e 's/^.*?: ?//' )

set_options=$( echo -e 'No\nYes' | $DMENU -p 'Set options?' $COLORS | perl -p -e 's/^.*?: ?//' )

options=""

# standard options to lpr, not in the printer lpstat -l
standard_options="page-ranges\nlandscape"

while [ $set_options == "Yes" ]; do
	custom_options=$( lpoptions -d $printer -l | grep -v NotInstalled | awk '{ print $1 }' | sed 's/:\+//g' )
	option_to_set=$( echo -e "$standard_options\n$custom_options" | $DMENU -p 'Option:' $COLORS | perl -p -e 's/^.*?: ?//' )
	option_value=$( lpoptions -d $printer -l | grep $option_to_set | awk 'BEGIN { FS = ": " } ; { print $2 }' | sed 's/ \+/\n/g' | sed 's/*\+//g' | $DMENU -p 'Setting:' $COLORS | perl -p -e 's/^.*?: ?//' )
	option_to_set_1d=$( echo $option_to_set | awk 'BEGIN { FS = "/" } ; { print $1 }' )
        if [ -z $option_value ]; then
		options=$options"-o $option_to_set_1d "
	else
		options=$options"-o $option_to_set_1d=$option_value "
	fi
	set_options=$( echo -e 'No\nYes' | $DMENU -p 'Set more options?' $COLORS | perl -p -e 's/^.*?: ?//' )
done

lpr $options -P $printer $@