#include "ps.h"

void printFile(ostream& out){

  out<<"%!\n";
  out<<"/inch {72 mul} def\n";
  out<<"\n";
  out<<"/coneX 0.25 inch def % width of the cones on the ends of modules\n";
  out<<"/coneY 0.5 inch def % Height of tapered part of cone\n";
  out<<"/spacing 0.1 inch def % spacing between adjacent modules\n";
  out<<"/linkheight 1 inch def % height of the linkages between adjacent modules\n";
  out<<"\n";
  out<<"\n";
  out<<"%<< /PageSize [17 inch 11 inch] >> setpagedevice\n";
  out<<"<< /PageSize [11 inch 8.5 inch] >> setpagedevice\n";
  out<<"\n";
  out<<"0.70 0.70 scale\n";
  out<<"0 50 translate\n";
  out<<"\n";
  out<<"\n";
  out<<"\n";
  out<<"%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n";
  out<<"\n";
  out<<"\n";
  out<<"\n";
  out<<"% <n> moddim <size>\n";
  out<<"% returns the size in points of a module\n";
  out<<"% with n racks in a dimension.\n";
  out<<"/moddim{\n";
  out<<"0.5625 inch mul 0.5 inch add\n";
  out<<"} def\n";
  out<<"\n";
  out<<"% <start> <rack> rackpos <pos>\n";
  out<<"% Returns the position in points of a rack\n";
  out<<"% at rack coord <rack> in a module starting\n";
  out<<"% at point coord <start>\n";
  out<<"/rackpos{\n";
  out<<"0.5625 inch mul add 0.25 inch add\n";
  out<<"}def\n";
  out<<"\n";
  out<<"\n";
  out<<"\n";
  out<<"%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n";
  out<<"\n";
  out<<"\n";
  out<<"\n";
  out<<"% <startx> <starty> <width> <height> module -\n";
  out<<"% draws a module holding width by height\n";
  out<<"%racks with bottom-left coord at (startx, starty)\n";
  out<<"/module{\n";
  out<<"gsave\n";
  out<<"10 dict begin\n";
  out<<"newpath\n";
  out<<"/height exch moddim def\n";
  out<<"/width exch moddim def\n";
  out<<"/starty exch def\n";
  out<<"/startx exch def\n";
  out<<"\n";
  out<<"startx starty moveto\n";
  out<<"2 setlinewidth\n";
  out<<"0 height rlineto\n";
  out<<"width 0 rlineto\n";
  out<<"0 -1 height mul rlineto\n";
  out<<"closepath\n";
  out<<"-1 coneX mul coneY rlineto\n";
  out<<"0 height 1 inch sub rlineto\n";
  out<<"coneX coneY rlineto\n";
  out<<"width 0 rmoveto\n";
  out<<"coneX -1 coneY mul rlineto\n";
  out<<"0 height 1 inch sub -1 mul rlineto\n";
  out<<"-1 coneX mul -1 coneY mul rlineto\n";
  out<<"stroke\n";
  out<<"2 setlinewidth\n";
  out<<"\n";
  out<<"startx coneX sub starty height 2 div linkheight 2 div sub add moveto\n";
  out<<"-1 spacing mul 0 rlineto\n";
  out<<"0 linkheight rlineto\n";
  out<<"spacing 0 rlineto\n";
  out<<"closepath\n";
  out<<"gsave 0.5 setgray fill grestore\n";
  out<<"stroke\n";
  out<<"\n";
  out<<"startx width add coneX add starty height 2 div linkheight 2 div sub add moveto\n";
  out<<"spacing 0 rlineto\n";
  out<<"0 linkheight rlineto\n";
  out<<"-1 spacing mul 0 rlineto\n";
  out<<"closepath\n";
  out<<"gsave 0.5 setgray fill grestore\n";
  out<<"stroke\n";
  out<<"end\n";
  out<<"grestore\n";
  out<<"}def\n";
  out<<"\n";
  out<<"% <startx> <starty> <width> <height> rotmodule -\n";
  out<<"% draws a module holding width by height racks\n";
  out<<"% with bottom-left coord at (startx, starty) and\n";
  out<<"% cones on the top and bottom\n";
  out<<"/rotmodule{\n";
  out<<"gsave\n";
  out<<"10 dict begin\n";
  out<<"/height exch def\n";
  out<<"/width exch def\n";
  out<<"/starty exch def\n";
  out<<"/startx exch def\n";
  out<<"\n";
  out<<"startx width moddim add starty translate\n";
  out<<"90 rotate\n";
  out<<"0 0 height width module\n";
  out<<"end\n";
  out<<"grestore\n";
  out<<"}def\n";
  out<<"\n";
  out<<"\n";
  out<<"\n";
  out<<"%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n";
  out<<"\n";
  out<<"\n";
  out<<"\n";
  out<<"% <locname> <rackname> <startx> <starty> <rackx> <racky> rack -\n";
  out<<"%\n";
  out<<"% Draws a rack at (rackx, racky) rack coords \n";
  out<<"% in a module starting at (startx, starty)\n";
  out<<"% screen coords, using locname and rackname\n";
  out<<"% as the text (in 8 pt Courier)\n";
  out<<"/rack{\n";
  out<<"gsave\n";
  out<<"10 dict begin\n";
  out<<"/racky exch def % get vars\n";
  out<<"/rackx exch def\n";
  out<<"/starty exch def\n";
  out<<"/startx exch def\n";
  out<<"/rackname exch def\n";
  out<<"/locname exch def\n";
  out<<"% Set rack start coords\n";
  out<<"/rackx startx rackx rackpos def\n";
  out<<"/racky starty racky rackpos def\n";
  out<<"\n";
  out<<"newpath\n";
  out<<"1 setlinewidth\n";
  out<<"rackx racky moveto\n";
  out<<"0 0.5 inch rlineto\n";
  out<<"0.5 inch 0 rlineto\n";
  out<<"0 -0.5 inch rlineto\n";
  out<<"closepath\n";
  out<<"clip\n";
  out<<"stroke\n";
  out<<"\n";
  out<<"/Courier findfont 8 scalefont setfont\n";
  out<<"rackx 1 add 0.3 inch racky add moveto\n";
  out<<"locname show\n";
  out<<"rackx 1 add 0.2 inch racky add moveto\n";
  out<<"rackname show\n";
  out<<"end\n";
  out<<"grestore\n";
  out<<"} def\n";
  out<<"\n";
  out<<"\n";
  out<<"\n";
  out<<"%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n";
  out<<"\n";
  out<<"\n";
  out<<"\n";
  out<<"% <name> <x> <y> <rackx> <racky> adaptercircle -\n";
  out<<"% Draws an adapter circle for the\n";
  out<<"% rack at <rack> in module at (x,y)\n";
  out<<"% with the text <name>\n";
  out<<"/adaptercircle{\n";
  out<<"10 dict begin\n";
  out<<"gsave\n";
  out<<"/rad 0.25 inch def\n";
  out<<"/racky exch def\n";
  out<<"/rackx exch def\n";
  out<<"/y exch racky rackpos rad add def\n";
  out<<"/x exch rackx rackpos rad add def\n";
  out<<"/name exch def\n";
  out<<"newpath\n";
  out<<"x y rad 0 360 arc stroke\n";
  out<<"x y rad 0.05 inch sub 0 360 arc clip stroke\n";
  out<<"/Courier findfont 8 scalefont setfont\n";
  out<<"/x x name stringwidth pop 2 div sub def\n";
  out<<"/y y name stringwidth exch pop 2 div sub 2 sub def\n";
  out<<"x y moveto\n";
  out<<"name show\n";
  out<<"grestore\n";
  out<<"end\n";
  out<<"}def\n";
  out<<"\n";
  out<<"% <startx> <starty> <labels> adapter -\n";
  out<<"% Draws a block of module adapters labeled\n";
  out<<"% with the values in the array <labels>\n";
  out<<"/adapter{\n";
  out<<"10 dict begin\n";
  out<<"/labels exch def\n";
  out<<"/starty exch def\n";
  out<<"/startx exch def\n";
  out<<"\n";
  out<<"gsave\n";
  out<<"newpath\n";
  out<<"2 setlinewidth\n";
  out<<"startx 0 rackpos starty moveto\n";
  out<<"0 4 moddim rlineto\n";
  out<<"0.5 inch 0 rlineto\n";
  out<<"0 -1 4 moddim mul rlineto\n";
  out<<"closepath\n";
  out<<"stroke\n";
  out<<"1 setlinewidth\n";
  out<<"0 1 3\n";
  out<<"{\n";
  out<<"/n exch def\n";
  out<<"labels 3 n sub get startx starty 0 n adaptercircle\n";
  out<<"} for\n";
  out<<"stroke\n";
  out<<"grestore\n";
  out<<"end\n";
  out<<"}def\n";
  out<<"\n";
  out<<"% <startx> <starty> <labels> rotadapter -\n";
  out<<"% Draws a left-right block of module adapters labeled\n";
  out<<"% with the values in the array <labels>\n";
  out<<"/rotadapter{\n";
  out<<"10 dict begin\n";
  out<<"/labels exch def\n";
  out<<"/starty exch def\n";
  out<<"/startx exch def\n";
  out<<"\n";
  out<<"gsave\n";
  out<<"newpath\n";
  out<<"2 setlinewidth\n";
  out<<"startx starty 0 rackpos moveto\n";
  out<<"4 moddim 0 rlineto\n";
  out<<"0 0.5 inch rlineto\n";
  out<<"-1 4 moddim mul 0 rlineto\n";
  out<<"closepath\n";
  out<<"stroke\n";
  out<<"1 setlinewidth\n";
  out<<"0 1 3\n";
  out<<"{\n";
  out<<"/n exch def\n";
  out<<"labels n get startx starty n 0 adaptercircle\n";
  out<<"} for\n";
  out<<"stroke\n";
  out<<"grestore\n";
  out<<"end\n";
  out<<"}def\n";
  out<<"\n";
  out<<"\n";
  out<<"\n";
  out<<"%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n";
  out<<"\n";
  out<<"\n";
  out<<"\n";
  out<<"% <x> <y> <text> label -\n";
  out<<"% Draw a module label at x,y\n";
  out<<"/label{\n";
  out<<"10 dict begin\n";
  out<<"gsave\n";
  out<<"/text exch def\n";
  out<<"/y exch def\n";
  out<<"/x exch def\n";
  out<<"\n";
  out<<"x y moveto\n";
  out<<"/Times-Roman findfont 24 scalefont setfont\n";
  out<<"text show\n";
  out<<"grestore\n";
  out<<"end\n";
  out<<"}def\n";
  out<<"\n";
  out<<"\n";
  out<<"\n";
  out<<"%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n";
  out<<"\n";
  out<<"\n";
  out<<"\n";
  out<<"/LAB {7.5 inch 4 inch} def\n";
  out<<"\n";
  out<<"/AIR{\n";
  out<<"LAB exch 6 moddim add 1 moddim sub\n";
  out<<"exch 4 moddim add spacing add coneX add\n";
  out<<"}def\n";
  out<<"\n";
  out<<"/NOD2{\n";
  out<<"LAB exch 3 moddim sub spacing sub coneX 2 mul sub\n";
  out<<"exch\n";
  out<<"} def\n";
  out<<"\n";
  out<<"/NOD1{\n";
  out<<"LAB pop 6 moddim add coneX 2 mul add spacing add % LAB's X plus LAB+cones+spacing\n";
  out<<"LAB exch pop % LAB's Y\n";
  out<<"} def\n";
  out<<"\n";
  out<<"/NOD3{\n";
  out<<"NOD1 pop 3 moddim 2 div 4 moddim 2 div sub add\n";
  out<<"4 inch spacing sub coneX sub 3 moddim sub % NOD1's Y minus spacing+cone\n";
  out<<"} def\n";
  out<<"\n";
  out<<"/CAM{\n";
  out<<"NOD2 exch 3 moddim 2 div 4 moddim 2 div sub add % centered on NOD2 X\n";
  out<<"exch spacing add coneX add 4 moddim add % NOD2's Y minus spacing+cone\n";
  out<<"} def\n";
  out<<"\n";
  out<<"/HAB{\n";
  out<<"NOD3 exch spacing coneX add sub 6 moddim sub\n";
  out<<"exch spacing coneX add sub\n";
  out<<"} def\n";
  out<<"\n";
  out<<"/JEM{\n";
  out<<"NOD2 pop 6 moddim sub coneX sub %spacing sub\n";
  out<<"HAB exch pop\n";
  out<<"}def\n";
  out<<"\n";
  out<<"/APM{\n";
  out<<"JEM pop\n";
  out<<"CAM exch pop\n";
  out<<"}def\n";
  out<<"\n";
  out<<"/JEMELMPS{\n";
  out<<"JEM exch coneX sub exch\n";
  out<<"4 moddim add coneX spacing add add\n";
  out<<"} def\n";
  out<<"\n";
  out<<"\n";
  out<<"\n";
  out<<"%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n";
  out<<"\n";
  out<<"\n";
  out<<"\n";
  out<<"JEM 6 4 module\n";
  out<<"CAM 4 5 rotmodule\n";
  out<<"HAB 6 4 module\n";
  out<<"LAB 6 4 module\n";
  out<<"APM 4 4 module\n";
  out<<"AIR 1 4 module\n";
  out<<"\n";
  out<<"NOD2 3 4 module\n";
  out<<"NOD1 3 4 module\n";
  out<<"NOD3 4 3 rotmodule\n";
  out<<"\n";
  out<<"JEMELMPS 4 2 rotmodule\n";
  out<<"\n";
  out<<"NOD1 [(Z1) (AIR) (NOD3) (CUP)] adapter\n";
  out<<"NOD2 [(CAM) (APM) (MPLM) (JEM)] adapter\n";
  out<<"NOD3 [() (CRV) () (HAB)] rotadapter\n";
  out<<"\n";
  out<<"JEM 20 sub spacing sub (JEM) label\n";
  out<<"JEMELMPS 2 moddim add coneX add spacing 2 mul add (JEM ELM-PS) label\n";
  out<<"APM 4 moddim add spacing add (ESA APM) label\n";
  out<<"NOD2 20 sub spacing sub (Node 2) label\n";
  out<<"HAB 20 sub spacing sub (Rephased HAB) label\n";
  out<<"LAB 4 moddim add spacing add (US Lab) label\n";
  out<<"NOD3 20 sub spacing 2 mul sub coneX sub (Node 3) label\n";
  out<<"AIR 4 moddim add spacing add (Airlock) label\n";
  out<<"CAM 5 moddim add spacing 2 mul add coneX add (CAM) label\n";
  out<<"NOD1 4 moddim add spacing add (Node 1) label\n";
  out<<"\n";
  out<<"% Draw the CAM rotor\n";
  out<<"newpath\n";
  out<<"CAM exch 0 rackpos exch 4 rackpos moveto\n";
  out<<"0 0.5 inch rlineto\n";
  out<<"CAM exch 3 rackpos 0.5 inch add exch 4 rackpos 0.5 inch add lineto\n";
  out<<"0 -0.5 inch rlineto\n";
  out<<"closepath stroke\n";
  out<<"/Courier findfont 16 scalefont setfont\n";
  out<<"CAM exch 4 moddim 2 div add (CAM Rotor) stringwidth pop 2 div sub\n";
  out<<"exch 4 rackpos 0.25 inch 8 2 div sub add moveto\n";
  out<<"(CAM Rotor) show\n";
  out<<"\n";
}
