/*\
 *  StageLengthen can add frames to an animation and extend the actor
 *  bars automaticly.  This is for when you want to add another scene
 *  to an animation and don't wnat to edit 50 actor bars by hand when
 *  you add more frames.
 *
 *  Usage: StageLengthen <stageFileName> <newFrames> <lengthenThreshold>
 *    stageFileName is renamed to stageFileName.old for backup.
 *    NewFrames is how many frames to add to the animation.  You can set
 *    this to zero to not add any frames.
 *    LengthenThreshold is how close can the actor bar be to the end of
 *    the animation before it gets expanded.  Using a paramter of 0 here
 *    would only expand an actor if it was in the last frame.  A 1 would
 *    lengthen bars that were 1 frame away from the end, and a 10000
 *    would lengthen every actor.
 *
 *  Notes:
 *    Each object is given a line of its own in the output.  A "." is
 *    displayed for each timeline bar that is modified.
 *    If you want to stretch the entire animation to fill the new size,
 *    use StageFrames instead.  This is for adding a new scene.
 *    Globals, Lights, objects, and the Camera actor bar are the only
 *    bars that are changed.  Line 78 has a list of every IFF HUNK that
 *    this script will modify.
 *
 *  V1.0 Jan-14-94
 *  IanSmith@moose.erie.net
 *
\*/

Numeric Digits 14                    /* Need at least 12 to handle 32 bits */
Parse Arg FileName Change Threshold .
If Threshold="" Then Do
 Say "Usage: StageLengthen <stageFileName> <newFrames> <lengthenThreshold>"
 Say "       More help can be found in the header of this ARexx file."
 Exit
End

MaxFrames=0
Temp="Temp$Stage."||Date(D)||Time(S)               /* Create temp filename */
Address Command 'Delete >NIL:' FileName||".Old"    /* Erase backup file */
If Open(Out,Temp,"W")=0 Then Do
 Say "Can't open temp staging file!"
 Exit
End
If Open(In,FileName,"R")=0 Then Do
 Say "Can't find '"FileName"' to open!"
 Exit
End
Call Open(Text,"*","W")
Call WriteCH(Text,"Shifting '"||FileName||"' forward "||Change||" frames.")
Call ParseStaging(C2D(SubStr(ReadWrite(In,12),5,4)))
Call WriteLN(Text,"") Close(In) Close(Out) Close(Text)
Address Command 'Rename' FileName 'To' FileName||".Old"
Address Command 'Rename' Temp 'To' FileName

Exit

ParseStaging: PROCEDURE EXPOSE Change Threshold MaxFrames
 Parse Arg MaxSize                             /* Recursive Function     */
 TotalSize=4; New=0
 Do Until TotalSize>=MaxSize                   /* Read MaxSize Bytes     */
  LastName=Name
  Name=ReadWrite(In,4)
  If Name~=LastName Then New=0
  Size=C2D(ReadWrite(In,4))
  If Size//2=1 Then Size=Size+1                /* Add pad byte if needed */
  If Name="ISTG" | Name="SOBJ" Then Do
   TotalSize=TotalSize+ParseStaging(Size)+8    /* Parse into these hunks */
   Iterate
  End
  TotalSize=TotalSize+Size+8                   /* Add SIZE and NAME length */
  If Name="NAME" Then
   Call WriteCH(Text,'0A'x||"  "||ReadWrite(In,Size))
  Else If Name="MAXF" Then Do
   MaxFrames=C2D(ReadCH(In,Size))
   Call WriteCH(Out,Right(D2C(MaxFrames+Change),2,'00'x))
  End; Else If Index("FIL3-LIT2-GLB3-CAMR", Name)~=0 Then Do
   Buffer=ReadCH(In,Size)
   StartF=C2D(SubStr(Buffer,3,2))
   EndF=C2D(SubStr(Buffer,5,2))
   If (StartF>0 & StartF<=MaxFrames & EndF>0 & EndF<=MaxFrames & StartF<=EndF) Then Do
    If (MaxFrames-EndF)<=Threshold Then Do
     Call WriteCH(Text,".")
     Buffer=Overlay(Right(D2C(MaxFrames+Change),2,'00'x),Buffer,5)
    End
   End; Else Do
    Say "Hunk" Name "is not a valid timeline!"
   End
   Call WriteCH(Out,Buffer)
  End; Else
   Call ReadWrite(In,Size)
 End
Return TotalSize

ReadWrite:
 Parse Arg FileHandle , Bytes .
 RWBuffer=ReadCH(FileHandle, Bytes)
 Call WriteCH(Out,RWBuffer)
Return RWBuffer
