// P R E P R E S S C O M M A N D S
//
//Bcpl/f PrepressCommands.bcpl
//

//Last modified January 20, 1982  10:33 PM by Lyle Ramshaw, PARC:
//  Changed command-line parameter code to properly round points
//  to micas, rather than truncate.

//Most of the code in this file was removed from Prepress.bcpl
//  by Kerry A. LaPrade, XEOS in order to decrease the size
//  of resident code.

//Last modified September 26, 1980  11:20 AM by Lyle Ramshaw, PARC:
//  Added Verify flag for the dictionary commands.

//Modified May 24, 1980  4:51 PM by Lyle Ramshaw, PARC:
//  Updated call on EncodeFace for new faces;  added the
//  MakeKS and ReadKS commands.  Also added the Compact
//  command, and changed ListFast to FastFlag.  Added "Clip"
//  flag after the command name as /K.
//
//Modified March 27, 1980  5:51 PM (by LaPrade)
//  Changed /B and /D switches for Edit, got rid of
//  FSGetX(), updated list of device resolutions for /D
//  and /E and changed Supercede to Supersede.

//Created January 16, 1980  2:19 PM

get "GoodFoo.d"
get "ix.dfs"
get "OverlayStuff.d"

// outgoing procedures
external
   [
   Disambiguate
   GetOverlayNumber
   InitCommands
   ReadParams
   ]

// outgoing statics
external
   [
   @angleToRotate	//extra rotation in DEGREES
   @BackgroundFileName
   @bigfilename
   @bitfactor
   @dotsize
   @fam
   @face
   @incline
   @InputFileName
   @fastFlag
   @OutputFileName
   @params
   @percent
   @resolutionx
   @resolutiony
   @rotation
   @siz
   @tridentDriveNumber
   @tridentNeeded
   @updateflag
   @xfp
   @yfp
   ]

static
   [
   @angleToRotate = 90	//extra rotation in DEGREES
   @BackgroundFileName
   @bigfilename
   @bitfactor = 1
   @dotsize = 20
   @fam
   @face = 0
   @incline = 0
   @InputFileName
   @fastFlag = false
   @OutputFileName
   @params = 0
   @percent = 50
   @resolutionx = 0
   @resolutiony = 0
   @rotation = 0
   @siz = 0
   @tridentDriveNumber = 0
   @tridentNeeded = false
   @updateflag = false
   @xfp
   @yfp
   ]

// incoming procedures
external
   [
//Float
   FDV
   FLDI
   FML
   FST
   FTR
   FLDDP
   FAD

//PrePressUtil
   EncodeFace
   IllCommand
   Scream

//Scan
   ReadCom
   ReadComInit
   ReadNumber
   StrCop
   StrEq
   ]
//incoming static
external
   [
   @Clipped	//from Playout.
   @verifyFlag	//from MergeDelete
   ]


// Procedures

//*********************************************************
let InitCommands(lvstr, lvsw, lvswt, lvfileCode, lvdefFileCode, lvcommandCode) = valof
//*********************************************************
   [
   if OsVersion ls 17 then 
      [
      Wl("Warning: This version of Prepress requires OS 17 or greater.")
      Ws("Type *"BANZAI!*" to continue: ")
      let char = nil
      for I = 1 to "BANZAI!">> STRING.length do
         [
         char = Gets(keys)
         Puts(dsp, char)
         unless char eq "BANZAI!">> STRING.char↑I do abort
         ]
      Wl("")
      ]

   fam = Allocate(sysZone, 13)  //Vector for family name
   fam!0 = 0  //fam>> STRING.length = 0

   bigfilename = Allocate(sysZone, 13)  //Vector for big file name
   bigfilename!0 = 0  //bigfilename>> STRING.length = 0

   InputFileName = Allocate(sysZone, 21)
   InputFileName!0 = 0  //InputFileName>> STRING.length = 0

   OutputFileName = Allocate(sysZone, 21)
   OutputFileName!0 = 0  //OutputFileName>> STRING.length = 0

   BackgroundFileName = Allocate(sysZone, 21)
   BackgroundFileName!0 = 0  //BackgroundFileName>> STRING.length = 0

   xfp = Allocate(sysZone, 2)
   yfp = Allocate(sysZone, 2)

   FLDI(1,1)
   FST(1,xfp)
   FST(1,yfp)

   let str, sw, swt, fileCode, defFileCode, commandCode =
      nil, nil, 0, 0, nil, nil

   str = Allocate(sysZone, 11)
   sw = Allocate(sysZone, 11)

   ReadComInit()
   ReadCom(str, sw)  //Bypass "PREPRESS"

//   if sw!0 ne 0 then swt = sw!1
//   if swt eq $T then tridentNeeded = true  //try Trident disk
   if sw!0 ne 0 then tridentNeeded = (sw!1 eq $T)  //try Trident disk

   let AltoVersion =(table [ #61014; #1401 ] )()
   let eng=AltoVersion<<VERS.eng
   if eng eq 5 then tridentDriveNumber=1  //change default drive to 1 
		//on a Dorado, since drive 0 is simulating DP0

   let useCommandLine = ReadCom(str,sw)
   if useCommandLine then
      [
      // Now interpret switches on the command name:
      for i = 1 to sw!0 do
         [
         swt = sw!i
         switchon swt into
            [
            case $C:
               fileCode = 1
            endcase

            case $S:
               fileCode = 2
            endcase

            case $W:
               fileCode = 3
            endcase

            case $F:
               fastFlag = true
            endcase

            case $K:
               Clipped = true
            endcase

            case $V:
               verifyFlag = true
            endcase

            default:
            endcase
            ]
          ]

      // Look up the command itself; need only type enough to disambiguate.
      commandCode = Disambiguate(str)		//Look it up.
      if commandCode eq 0 then IllCommand()
      if commandCode ge 12 then ReadParams()
      if commandCode ge 30 & (fileCode eq 0) then IllCommand()
      defFileCode = (fileCode ? fileCode, 1)
      ]

   @lvstr, @lvsw, @lvswt, @lvfileCode, @lvdefFileCode, @lvcommandCode =
      str, sw, swt, fileCode, defFileCode, commandCode
   resultis useCommandLine
   ]

//*********************************************************
and GetOverlayNumber(commandCode) = selecton commandCode into
//*********************************************************
   [
   case  1:
   case 13:
      Conversion
   case  2:
   case  3:
   case  4:
   case  6:
   case  7:
   case  8:
   case 16:
   case 17:
   case 18:
   case 19:
   case 20:
   case 21:
   case 22:
   case 23:
      CUMunge
   case  9:
   case 10:
   case 11:
      ReadALov
   case 12:
      Edit
   case 14:
   case 15:
   case 24:
   case 30:
   case 31:
   case 32:
   case 33:
   case 34:
      TridentFile
   ]

//*********************************************************
and Disambiguate(str) = valof
//*********************************************************
   [
   let len = str>>STRING.length
   let matchNo = nil
   let matchCnt = 0
   for i = 1 to 100 do
      [
      let s = selecton i into
         [
         case 1: "ReadSf"
         case 2: "Show"
         case 3: "MakeCu"
         case 4: "MakeAl"
//         case 5: "MakeMM"
         case 6: "MakeStrike"
         case 7: "ReadWidths"
         case 8: "ReadCu"
         case 9: "ReadAL"
         case 10: "MakeKS"
         case 11: "ReadKS"

         case 12: "Edit"
         case 13: "Convert"
         case 14: "Delete"
         case 15: "List"
         case 16: "Coordinate"
         case 17: "OrbitFormat"
         case 18: "Scale"
         case 19: "ImposeWidths"
         case 20: "DeOrbitize"
         case 21: "Rotate"
         case 22: "Grow"
         case 23: "Shrink"
         case 24: "Compact"

         case 30: "Extract"
         case 31: "Merge"
//         case 32: "Supercede"
         case 32: "Supersede"
         case 33: "Width"
         case 34: "Rename"

         default: 0
         ]
      if s eq 0 % len gr s>> STRING.length then loop
      let match = true
      for j = 1 to len do
         if ((str>> STRING.char↑j xor s>> STRING.char↑j) & (not #40)) ne 0 then
            match = false
      if match then
         [ 
         matchCnt = matchCnt+1
         matchNo = i
         ]
      ]
   resultis ((matchCnt eq 1) ? matchNo, 0)
   ]

//Read family, face, size, rotation, resolution etc from
// command line.

//*********************************************************
and ReadParams() = valof
//*********************************************************
   [
//   params = 0  //Mask of what is params
//   siz = 0				//Set defaults
//   rotation = 0
//   incline = 0
   resolutionx = 2000
   resolutiony = 2000

   let str = vec 20
   let sw = vec 10
      [
      if ReadCom(str, sw) eq 0 then break //End of line
      for i = 1 to sw!0 do switchon sw!i into
         [
         case $N:  //Name
            StrCop(str, fam)
            params = params%gotname
         endcase

         case $F:  //Face
            face = EncodeFace(str)
            if face eq -1 then
               Wl("Illegal face in command.")
            params = params % gotface
         endcase

         case $S:  //Source file name
//            InputFileName = FSGetX(20)// small names only!
            StrCop(str, InputFileName)
         endcase

         case $O:  // output file name
//            OutputFileName = FSGetX(20)// small names only!
            StrCop(str, OutputFileName)
         endcase

         case $P:  //Size in points
            ReadNumber(str)
            FLDI(2, 635); FML(1, 2); FLDI(2, 18); FDV(1, 2)  //to micas
            FLDDP(2, table [ 0; #100000 ] ); FAD(1,2)  //  add one-half
            siz = FTR(1)  //and truncate, to achieve rounding
            params = params % gotsize
         endcase

         case $M:  //Size in micas
            siz = ReadNumber(str)
            params = params % gotsize
         endcase

         case $R:  //Rotation in degrees
            ReadNumber(str)
            FLDI(2, 60)
            FML(1, 2)  //convert to minutes
            rotation = FTR(1)
            params = params % gotrotation
         endcase

         case $I:  //Incline
            incline=ReadNumber(str)
            params=params%gotincline
         endcase

         case $B:
	//Big (dictionary) file name or Edit Background file name.
            StrCop(str, bigfilename)
            StrCop(str, BackgroundFileName)
         endcase

         case $D:  //Device x and y resolutions or Edit dotsize
         case $E:  //Device y resolution
         [
            let number = 0
            let c = str>> STRING.char↑1
            test  $0 le c & c le $9
            ifso
               [
               number = ReadNumber(str)
               if sw!i eq $D then dotsize = number
               ]
            ifnot
               [
               if StrEq(str,"ALTO") then number = 720
               if StrEq(str, "XGP") % StrEq(str,"VERSATEC") then
                  number = 2000
               if StrEq(str, "RAVEN") then number = 3000
//               if StrEq(str,"DOVER") then res=3500
               if StrEq(str,"DOVER") % StrEq(str,"SEQUOIA") % StrEq(str,"PENGUIN") % StrEq(str,"PUFFIN") then number = 3840
//               if StrEq(str,"SLOT") % StrEq(str,"EARS") then
               if StrEq(str,"SLOT") then
                  number = 5000
               if number eq 0 do Scream("Illegal device.")
               ]
            resolutiony = number
            if sw!i eq $D then resolutionx = number
            params = params % gotresolution
         endcase
         ]

         case $X:
         case $Y:  //Factors
            ReadNumber(str)
            if (sw!i eq $X) then FST(1, xfp)
            if (sw!i eq $Y) then FST(1, yfp)
            params = params % gotfactors
         endcase

         case $A:  //Angle to Rotate in degrees
            angleToRotate = ReadNumber(str)
         endcase

         default:
         case $z:
            IllCommand()
         endcase
         ]
      ] repeat
   ]