/* .key ARCHIVE/A,TEMPDIR */

/*
 *
 * Generic argument parsing within ARexx. Fixes some problems within the
 * RX program, as it won't handle "'s. The results will be put in argv,
 * argc will be the number of arguments.
 *
 */


/********************  START OF COMMAND LINE PARSING  ********************/

Parse Arg CmdLine

argv = ""

argc = 1

/* First, split the command into its argvs */

Do While CmdLine ~= ""
	CmdLine = Strip(CmdLine)

	If Left(CmdLine, 1) = '"' Then Do
		CmdLine = Strip(CmdLine, 'B', '"')
		EndPos = Index(CmdLine, '"')
		StripChar = '"'
	End
	Else Do
		EndPos = Index(CmdLine, ' ')
		StripChar = ' '
	End

	If EndPos = 0 Then EndPos = Length(CmdLine)

	argv.argc = Strip(SubStr(CmdLine, 1, EndPos), 'B', StripChar)

	argc = argc + 1

	CmdLine = Right(CmdLine, Length(CmdLine) - EndPos)
End

argc = argc - 1

Drop EndPos
Drop CmdLine
Drop StripChar

/*********************  END OF COMMAND LINE PARSING  *********************/

/* Read environment file */

Options Failat 6

rexxscript = "REXX:SignArch.rexx"
lhacommand = "LhA"

If Open('pgpenv', "ENV:SignArchGUIPaths", 'R') Then Do
	Do While Eof('pgpenv') = 0
		tmpline = ReadLn('pgpenv')
		If Left(Upper(tmpline), 4) = "SCR:" Then rexxscript = Right(tmpline, Length(tmpline)-4)
		If Left(Upper(tmpline), 4) = "LHA:" Then rexxscript = Right(tmpline, Length(tmpline)-4)
	End
	Close('pgpenv')
End


/* Init variables */


Select
	When argc = 1 Then TempDir = "T:"
	When argc = 2 Then TempDir = argv.2
	Otherwise Do
		Say "Illegal number of parameters!"
		Exit
	End
End

Archive = argv.1

LastDot = 0
LastSlash = 0
LastColon = 0

TmpPos = 0

Do Until TmpPos = 0
	TmpPos = Index(Archive, ".", TmpPos + 1)
	If TmpPos ~= 0 Then LastDot = TmpPos
End

TmpPos = 0

Do Until TmpPos = 0
	TmpPos = Index(Archive, "/", TmpPos + 1)
	If TmpPos ~= 0 Then LastSlash = TmpPos
End

TmpPos = 0

Do Until TmpPos = 0
	TmpPos = Index(Archive, ":", TmpPos + 1)
	If TmpPos ~= 0 Then LastColon = TmpPos
End

StartBase = Max(LastColon, LastSlash)


If StartBase < LastDot Then Do
	ArcName = SubStr(Archive, StartBase + 1, LastDot - StartBase - 1)
End
Else Do
	ArcName = SubStr(Archive, StartBase + 1, Length(Archive) - StartBase - 1)
End

If Right(TempDir, 1) ~= '/' & Right(TempDir, 1) ~= ':' Then Tempdir = TempDir||'/'

ArcDir = TempDir||ArcName

If Exists(ArcDir) Then Address Command 'Delete "'||ArcDir||'" ALL'
Address Command 'MakeDir "'||ArcDir||'"'

If Right(ArcDir, 1) ~= '/' & Right(ArcDir, 1) ~= ':' Then ArcDir = ArcDir||'/'

Address Command lhacommand||' e "'||Archive||'" "'||ArcDir||'"'

/* Create the filelist file */

ArcFiles = TempDir||Compress(ArcName)||".fls"

Open('filelist', ArcFiles, 'W')

WriteLn('filelist', "!!FILELIST!!")
WriteLn('filelist', "***ROOTDIR "||ArcDir)
WriteLn('filelist', "***ARCHIVE "||Archive)
WriteLn('filelist', "***SUMFILE "||ArcDir||ArcName||".sum")
WriteLn('filelist', "***NOWILDS")
WriteLn('filelist', "***SUMONLY")
WriteLn('filelist', "***LASTOPT")
Close('filelist')

Address Command 'List >>"'||ArcFiles||'" "'||ArcDir||'" FILES ALL LFORMAT %f%s'

Address Command 'RX '||rexxscript||' '||ArcFiles

If rc = 0 Then Address Command lhacommand||' a "'||Archive||'" "'ArcDir||ArcName||'.sum"'

Address Command 'Delete >nil: "'||ArcFiles||'"'
Address Command 'Delete >nil: "'||ArcDir||'" ALL'
