#!/usr/bin/env python3

import os
import os.path
import subprocess
import sys

#PRINTDOC - a c-shell script (version  30 Apr 93)
#PRINTDOC  determines from the file extension what sort of file is
#          to be printed. It then generates the appropriate print
#          command for the PostScript printer
# 
# Synopsis:  printdoc <filename>

if len(sys.argv) != 2:
    print("Usage: printdoc.py  FILE");
    exit();

filename = sys.argv[1]
extension = os.path.splitext(filename)[1]

if extension == '.ps':
    #PostScript
    subprocess.call(['lpr', '-Pccp', filename])

elif extension in ('.nroff', '.man', '.me'):
    #nroff
    #man (same as nroff)
    #nroff.me
    p1 = subprocess.Popen(['nroff', '-ms'], stdin=open(filename, 'r'), stdout=subprocess.PIPE)
    p2 = subprocess.Popen(['xlp', '-f', 'BDJ1', '-d', 'xerox'], stdin=p1.stdout)


elif extension == '.tex':
    # .tex files contain page breaks to begin each page
    # header and footer supplied by pr are suppressed to
    # accommodate this type of 'preformatting'
    p1 = subprocess.Popen(['/usr/5bin/pr', '-t', '-o5', filename], stdout=subprocess.PIPE)
    p2 = subprocess.Popen(['xlp', '-f', 'BDJ1', '-d', 'xerox'], stdin=p1.stdout)
else:
    #.txt, .doc are the same as the default!
    #elif extension == '.txt':
    #elif extension == '.doc':
    # .doc files print at 12cpi with a 1in. left margin
    p1 = subprocess.Popen(['pr', '-f', filename], stdout=subprocess.PIPE)
    p2 = subprocess.Popen(['xlp', '-f', 'BDJ1', '-d', 'xerox'], stdin=p1.stdout)
