Program StripHeader101;

Uses Crt,DOS;

{$I Cursor.inc}
{$I FLength.inc}

Var
  IFS,OFS:Text;
  INF,OUT:String;
  CTR,LEN:LongInt;
  RCH,BRK:Char;
  CHK:String[4];
  WHY:Integer;
  OK:Boolean;

Begin
  If ParamCount<>2 Then Begin
    WriteLn('Usage: StripHeader <infile> <outfile>');
    WriteLn(#$a,'Greets to TBSNC - happy ripping!   ;)');
    Halt(20);
    Write('$VER: StripHeader v1.01 (01-Jan-97) ');
  End;
  INF:=ParamStr(1);
  OUT:=ParamStr(2);
  WriteLn(#$9b,'0;1;4mStripHeader v1.01 by Zebedee ©1996 Carnage (01-Jan-97)',#$9b,'0m');
  WriteLn;
  LEN:=FLength(INF);
  If LEN=0 Then Begin
    WriteLn('ERROR: Couldn''t locate the infile!');
    Halt(20);
  End;
  Reset(IFS,INF);
  ReadLn(IFS,CHK);
  Close(IFS);
  If (CHK[1]<>#$00) and (CHK[2]<>#$00) and (CHK[3]<>#$03) and (CHK[4]<>#$f3) Then Begin
    WriteLn('ERROR: infile isn''t an executable!');
    Halt(20);
  End;
  WriteLn('Length of file is ',LEN,' bytes');
  Reset(IFS,INF);
  ReWrite(OFS,OUT);
  If IOResult<>0 Then Begin
    WriteLn(ParamStr(0),': Unable to create outfile!');
    Close(IFS);
    Halt(20);
  End;
  WriteLn;
  OK:=True;
  WHY:=WhereY;
  Dec(WHY);
  GotoXY(0,WHY);
  WriteLn('Stripping header...');
  GotoXY(20,WHY);
  Cursor(False);
  For CTR:=1 To 32 Do
    Read(IFS,RCH);
  For CTR:=1 To LEN-32 Do
  Begin
    Read(IFS,RCH);
    If OK Then Begin
      Write(OFS,RCH);
      If IOResult<>0 Then Begin
        OK:=False;
        GotoXY(20,WHY);
        WriteLn('failed!      ',#$a,#$a,'Write error - disk space perhaps?');
        Close(OFS);
        Close(IFS);
        Cursor(True);
        Halt(20);
      End;
      GotoXY(20,WHY);
      WriteLn(CTR,'/',LEN);
    End;
    If KeyPressed Then Begin
      BRK:=ReadKey;
      If BRK=#3 Then Begin
        GotoXY(20,WHY);
        WriteLn('break!         ',#$a,#$a,'User abort.');
        Close(OFS);
        Close(IFS);
        Cursor(True);
        Halt(20);
      End;
    End;
  End;
  Cursor(True);
  GotoXY(20,WHY);
  Close(OFS);
  Close(IFS);
  WriteLn('done           ',#$a,#$a,'New file length is ',LEN-32,' bytes');
End.