#!/afs/athena/contrib/perl/perl

$filename = $ARGV[0];

open(RAWDATA, $filename) || die "Can't open $filename";
open(OUTFILE, ">&STDOUT") || die "Can't open STDOUT";

print OUTFILE while(<DATA>);

# Sample:
# |6.001	Lec	MWF	10	1	10-250

$i=0;
while (<RAWDATA>) {
  chop;
  next if /^#/;
  ($class, $lec, $days, $start, $length, $place) = split(/ +|\t+/, $_);
  ($a, $b) = split(/:/, $start);
  $time = $a + $b/60;
  ($a, $b) = split(/:/, $length);
  $len = $a + $b/60;
  $class =~ s/_/ /g;  $lec =~ s/_/ /g;  $place =~ s/_/ /g;
  $days =~ /M/ && print OUTFILE
    "classes ".$i++." [($class) ($lec) (M) $time $len ($place)] put\n";
  $days =~ /T/ && print OUTFILE
    "classes ".$i++." [($class) ($lec) (T) $time $len ($place)] put\n";
  $days =~ /W/ && print OUTFILE
    "classes ".$i++." [($class) ($lec) (W) $time $len ($place)] put\n";
  $days =~ /R/ && print OUTFILE
    "classes ".$i++." [($class) ($lec) (R) $time $len ($place)] put\n";
  $days =~ /F/ && print OUTFILE
    "classes ".$i++." [($class) ($lec) (F) $time $len ($place)] put\n";
}

print OUTFILE "\n/user ($ENV{USER}'s schedule) def\n";
print OUTFILE "/numclass $i def\n";
print OUTFILE "\nmaster\n";
close OUTFILE || die "Could not close $filename";
__END__
%!
%Copyright 1992 Dave Cho  All Rights Reserved
%Permission is given to freely distribute and modify this product so long
%as it is done without charge and so long as this Copyright text remains in
%place.
%Certain designs inherent may be copyrighted elsewhere.
%Dave Cho 1992
%
%modifications by Albert Dvornik (bert@MIT.EDU)
%
%7.7 8.5 div   10.1 11 div scale  % for gs
/inch {72 mul} def
/cm {72 mul 2.54 div} def
/yline {10 inch} def
% the following are the parameters you might want to change:
/sp 13 neg def % number of points between returns
/cp 4.25 inch def % where rcenter centers around
/lm 1 inch def % left margin
/tablen {2 inch} def % distance between tab stops
/tabpos {0} def % initial tab position
/shiftover {} def
%/shiftover {4.15 inch 0 translate /yline {9.6 inch} def 0 yline moveto
%enter} def

% don't mess with these:
/stringheight {/stringfoobar exch def gsave
newpath 0 0 moveto  stringfoobar false charpath  flattenpath pathbbox
exch pop 3 -1 roll pop  exch sub  grestore} def
/sqr {dup mul} def
/center   % stack: x y str
{/stringcenter exch def /ycenter exch def /xcenter exch def
	stringcenter stringwidth pop -2 div xcenter add
	fsize -2 div ycenter add moveto
	stringcenter show} def
/enter {lm currentpoint exch pop moveto 0 sp rmoveto
	/yline {currentpoint exch pop} def
	/tabpos lm def
	yline sp neg le {shiftover} if} def
/return {show enter} def
/rcenter {currentpoint exch pop cp exch 3 -1 roll  center enter} def
/tab {tabpos tablen add /tabnew exch def tabnew currentpoint
   exch pop moveto /tabpos tabnew def} def
/tabpos {lm} def
/stab {show tab} def
/blam { show currentpoint pop /xorig exch def /xfin {tablen lm add} def
(.) stringwidth pop /dotlen exch def xfin xorig sub dotlen div /foo exch def
/t {1} def 1 t foo {pop (.) show} for} def
/rm {7.5 inch} def
/right {currentpoint exch pop rm exch moveto dup stringwidth pop neg
0 rmoveto} def
/mark1 {currentpoint /ymark1 exch def /xmark1 exch def} def
/mark2 {currentpoint /ymark2 exch def /xmark2 exch def} def
/gofont {/fname exch def /fsize exch def fname findfont fsize scalefont
	setfont /sp (X) stringheight -1.1 mul def } def
/tb {/Times-Bold gofont} def
/tr {/Times-Roman gofont} def
/ti {/Times-Italic gofont} def
/tbi {/Times-BoldItalic gofont} def
/really {/wanted fsize def
/do-really {
(A) stringheight  wanted lt 
  {fsize 1 add fname gofont do-really} if
} def
do-really
} def
%%Trailer

/classes 100 array def

% [($class) ($lec) (M) $time $len ($place)]

/daysetup {
/class classes index get 0 get def
/lec classes index get 1 get def
/time classes index get 3 get def
/len classes index get 4 get def
/place classes index get 5 get def
} def

/doday {
1 setgray
0 time block mul neg moveto x 5 div 0 rlineto 0 len block mul neg rlineto
	x -5 div 0 rlineto closepath fill
0 setgray
0 time block mul neg moveto enter
fsize tb class rcenter fsize ti lec rcenter fsize tr place rcenter
0 time block mul neg moveto x 5 div 0 rlineto 0 len block mul neg rlineto
	x -5 div 0 rlineto closepath stroke
} def

/mon {gsave daysetup 0 0 translate doday grestore} def
/tue {gsave daysetup x 0.2 mul 0 translate doday grestore} def
/wed {gsave daysetup x 0.4 mul 0 translate doday grestore} def
/thu {gsave daysetup x 0.6 mul 0 translate doday grestore} def
/fri {gsave daysetup x 0.8 mul 0 translate doday grestore} def

%%%%%%%%% evil lizards.
%%% Look at /mit/bert/ps/esglabel.ps for commented version of this.
/nmatrix -70 0 matrix translate -60 matrix rotate matrix concatmatrix
def /mmatrix nmatrix 120 matrix rotate matrix concatmatrix def
/pmatrix nmatrix [-1 0 0 -1 70 0] matrix concatmatrix def /qmatrix
mmatrix [-1 0 0 -1 70 0] matrix concatmatrix def /oldpt {nmatrix
transform} def /oldinvpt {mmatrix transform} def /oldrevpt {pmatrix
transform} def /oldinvrevpt {qmatrix transform} def /next {70 0
translate 60 rotate} def /origin {0 0 moveto} def /path1 {10 25 lineto
8 45 lineto 16 42 lineto 24 40 lineto 33 38 lineto 25 0 lineto 58 -12
lineto 72 -4 lineto} def /rpath1 {12 12 lineto 45 0 lineto 37 -38
lineto 46 -40 lineto 54 -42 lineto 62 -45 lineto 60 -25 lineto 70 0
lineto} def /inner1 {8 45 moveto 7 55 lineto 33 38 moveto 40 30
lineto} def /outer1 {54 -42 moveto 54 -35 lineto 46 -40 moveto 47 -33
lineto 45 0 moveto 52 2 lineto 12 12 moveto 17 17 lineto 24 17 lineto}
def /path2 {2 3 lineto 5 9 lineto 8 15 lineto 0 20 lineto 0 30 lineto
7 32 lineto 14 33 lineto 21 34 lineto 29 29 lineto 16 0 lineto 34 -23
lineto 43 -24 lineto 70 0 lineto} def /rpath2 {27 24 lineto 36 23
lineto 54 0 lineto 41 -29 lineto 49 -34 lineto 56 -33 lineto 63 -32
lineto 70 -30 lineto 70 -20 lineto 62 -15 lineto 65 -9 lineto 68 -3
lineto 72 -4 oldrevpt lineto} def /inner2 {2 3 moveto -3 6 lineto 5 9
moveto 0 12 lineto 0 30 moveto -5 35 lineto 21 34 moveto 32 38 lineto}
def /outer2 {63 -32 moveto 61 -25 lineto 56 -33 moveto 52 -27 lineto
54 0 moveto 60 3 lineto 36 23 moveto 31 29 lineto} def /path3 {19 30
lineto 44 42 lineto 30 24 lineto 27 -2 lineto 30 -10 lineto 55 -15
lineto 61 -24 lineto 80 -22 lineto 77 -15 lineto 74 -8 lineto 70 0
lineto} def /rpath3 {-4 8 lineto -7 15 lineto -10 22 lineto 9 24
lineto 15 15 lineto 40 10 lineto 43 2 lineto 40 -24 lineto 26 -42
lineto 51 -30 lineto 72 -4 oldinvrevpt lineto} def /inner3 {77 -15
moveto 70 -16 lineto 74 -8 moveto 67 -8 lineto 19 30 moveto 10 27
lineto 44 42 moveto 50 44 lineto} def /outer3 {-10 22 moveto -15 27
lineto 40 10 moveto 37 17 lineto 30 20 lineto} def /face { stroke
gsave 1 setgray 29 5 moveto 29 5 5 90 270 arc fill 57 4 moveto 57 4 5
258.7 78.7 arc fill 0 setgray 29 5 moveto 29 5 5 90 270 arc 57 4
moveto 57 4 5 258.7 78.7 arc stroke 29 5 moveto 29 5 3 90 270 arc fill
57 4 moveto 57 4 3 258.7 78.7 arc fill grestore 36 -20 moveto 35 -9
lineto 29 -2 lineto 29 12 lineto 37 23 lineto 42 -19 moveto 49 -8
lineto 56 -1 lineto 58 9 lineto 53 22 lineto } def /spine {51 8 moveto
44 15 54 40 30 80 curveto 14 110 20 140 27 150 curveto 38 9 moveto 46
15 40 50 30 70 curveto 6 110 20 140 27 150 curveto} def /outline
{gsave origin path1 next path2 next rpath2 next path3 next rpath3 next
rpath1 stroke grestore} def /filled {gsave origin path1 next path2
next rpath2 next path3 next rpath3 next rpath1 fill grestore} def
/insides {gsave inner1 next inner2 face spine next outer2 next inner3
next outer3 next outer1 stroke grestore} def /ZeroGray 0 def /OneGray
1 def /whiteLizard {setgray filled ZeroGray setgray insides outline}
def /twinLizard {dup setgray filled OneGray setgray insides setgray
outline} def /blackLizard {setgray filled OneGray setgray insides
outline} def /cell {1 whiteLizard gsave 120 rotate .94 whiteLizard 120
rotate .86 whiteLizard grestore hmove} def /wcell {1 whiteLizard gsave
120 rotate 1 whiteLizard 120 rotate 1 whiteLizard grestore hmove} def
/hmove {210 0 translate} def /lvmove {120 rotate 210 0 translate -120
rotate} def /rvmove {60 rotate 210 0 translate -60 rotate} def /vmove
{2 mod 1 eq {lvmove} {rvmove} ifelse} def
%%%%%%%%% evil lizards -- END.

/norm {1 sub 12 mod 1 add} def

/master {
10 tr

/earliest 23 def
/latest 1 def
0 1  numclass 1 sub {/index exch def
	classes index get 3 get /time exch def
	time earliest lt {/earliest time floor def} if
	classes index get 4 get time add latest gt {
		/latest classes index get 4 get time add ceiling def} if
} for

/block (X) stringheight 6 mul def
/x 300 def /y latest ceiling earliest floor sub block mul def
/cp x 10 div def

2.25 inch  9 inch earliest block mul add translate

%%%%%%%%% groooooss.  (draw the lizards)
gsave
newpath
x -0.1 mul earliest 1 sub block mul neg moveto x 1.2 mul 0 rlineto
0 y block 1.5 mul add neg rlineto x -1.2 mul 0 rlineto
closepath clip
newpath
-20 8.3 inch earliest block mul add neg translate
.25 dup scale
0 setlinewidth
1 1 15 {gsave 1 1 8 {pop wcell} for grestore vmove} for
%1 1 2 {gsave 1 1 8 {pop wcell} for grestore vmove} for
%1 1 8{gsave wcell wcell hmove hmove hmove hmove wcell wcell grestore vmove}for
%1 1 2 {gsave 1 1 8 {pop wcell} for grestore vmove} for
grestore
gsave
newpath
0 earliest block mul neg moveto x 0 rlineto 0 y neg rlineto x neg 0 rlineto
closepath clip
newpath
-20 8.3 inch earliest block mul add neg translate
.25 dup scale
0 setlinewidth
1 1 14 {gsave 1 1 8 {pop cell} for grestore vmove} for
grestore
%%%%%%%%% groooooss -- END.

0 setgray
earliest floor cvi 1 latest ceiling cvi {/oclock exch def
	-15 oclock block mul neg -4 add moveto oclock norm (  ) cvs show
	0 oclock block mul neg moveto x 0 rlineto
} for stroke

0 1  numclass 1 sub {/index exch def
	classes index get 2 get /day exch def
	day (M) eq {mon} if
	day (T) eq {tue} if
	day (W) eq {wed} if
	day (R) eq {thu} if
	day (F) eq {fri} if
} for

0 0.2 1 {x mul earliest block mul neg moveto 0 y neg rlineto} for stroke
0 earliest block mul neg moveto x 0 rlineto 0 y neg rlineto x neg 0 rlineto
closepath stroke
fsize tr
x 0.1 mul  earliest 0.2 sub block mul neg (Mon) center
x 0.3 mul  earliest 0.2 sub block mul neg (Tue) center
x 0.5 mul  earliest 0.2 sub block mul neg (Wed) center
x 0.7 mul  earliest 0.2 sub block mul neg (Thu) center
x 0.9 mul  earliest 0.2 sub block mul neg (Fri) center
fsize 2 mul tb
x 0.5 mul earliest 0.7 sub block mul neg user center
x -0.1 mul earliest 1 sub block mul neg moveto x 1.2 mul 0 rlineto
0 y block 1.5 mul add neg rlineto x -1.2 mul 0 rlineto closepath stroke

showpage
} def % master
