#! /usr/bin/ruby

# globals for bash script segments
# --------------------------------

# bash code that preceeds the print command
$preCommand = <<-EndPre
#! /bin/bash
for file in "$@" ; do
    if [ -f $file ] ; then
EndPre

# bash code that follows the print command
$postCommand = <<-EndPost
    else
       echo "file $file not found, not printed"
    fi
done
EndPost

# methods in use
# --------------

# getPrinterList
# --------------
# method to obtain a list of the available printers
def getPrinterList ()
    printers = [] # array of printer names
    numPrinters = 0 # number of printers identified so far
    statRes = `lpstat -a`  # get list of avaiable printers (with status information)
    statRes.each_line do | line |
       # get printer name, the first word of the line,
       #     and add it to the array of printer names
       words = line.split
       printers[numPrinters] = words[0]
       numPrinters = numPrinters + 1
    end
    return printers
end

# choosePrinter
# -------------
# method to handle the selection of printer,
#    returns "-P printername" when a valid printer is chosen,
#    or an empty string if there are no printers available
#    (so lpr will use the default when run)
def choosePrinter ()
   printer = "csci-lq"
   available = getPrinterList
   # handle the case were there are no accessible printers
   if available.length < 1 then
      return ""
   end
   done = false
   begin
      puts "Which printer would you like to use?  The choices available are:\n"
      available.each do | prt |
         puts "   #{prt}\n"
      end
      printer = gets.chomp
      available.each do | prt |
         if prt == printer then
            done = true
         end
      end
      if !done then
         puts "sorry, #{printer} is not a valid selection\n"
      end
   end until done
   # return the -P printer string
   return "-P #{printer}"
end

# chooseOrientation
# -----------------
# method to handle the portrait/landscape selection, defaults to portrait
def chooseOrientation()
   orientation = "portrait"
   puts "Do you wish to use landscape mode (Y for yes, anything else for no)?"
   puts "(otherwise portrait mode will be used)"
   orchoice = gets
   orchoice = orchoice.capitalize
   if orchoice.start_with?("Y") then
      orientation = "landscape"
   end
   return orientation
end

# chooseLines
# -----------
# method to handle the lpi selection, defaults to 8
def chooseLines ()
   linesPer = 8
   done = false
   begin
      puts "How many lines per inch would you like (any integer 1..24)?\n"
      entry = gets.chomp
      linesPer = entry.to_i
      if linesPer < 1 or linesPer > 24 then
         puts "Sorry, #{entry} is an invalid selection, please try again\n"
      else
         done = true
      end
   end until done
   return linesPer
end

# chooseChars
# -----------
# method to handle the cpi selection
def chooseChars ()
   charsPer = 12
   done = false
   begin
      puts "How many characters per inch would you like (any integer 1..24)?\n"
      entry = gets.chomp
      charsPer = entry.to_i
      if charsPer < 1 or charsPer > 24 then
         puts "Sorry, #{charsPer} is an invalid selection, please try again\n"
      else
         done = true
      end
   end until done
   return charsPer
end

# choosePretty
# ------------
# method to handle the prettyprint selection, defaults to off
def choosePretty ()
   prettyChoice = ""
   puts "Do you wish to use prettyprint mode (Y for yes, anything else for no)?"
   prchoice = gets
   prchoice = prchoice.capitalize
   if prchoice.start_with?("Y") then
      prettyChoice = "-o pretty"
   end
   return prettyChoice
end


# main()
# ------
# main routine: obtain options, build script, write to file
def main ()
   printer = choosePrinter
   orientation = chooseOrientation
   linesPer = chooseLines
   charsPer = chooseChars
   prettyChoice = choosePretty
   printCommand = "       lpr #{printer} -o #{orientation} -o lpi=#{linesPer} -o cpi=#{charsPer} #{prettyChoice} $file"
   fname = "myprint.sh"
   fhandle = File.open("myprint.sh", "w")
   fhandle.puts("#{$preCommand}#{printCommand}\n#{$postCommand}")
   fhandle.close
   # rescue will print an error message if an exception was raised earlier
   #    (due to failure to open the myprint.sh file)
   rescue
      puts "Unable to write to file #{fname}\n"
end

# run the main routine
main()


