Resident > NIL: C:RequestChoice PURE		;Keeps RequestChoice program in memory, so it doesn't need to be loaded off disk each time it is called
Resident > NIL: C:RequestFile PURE
Resident > NIL: C:BaseName PURE
Resident > NIL: C:Eval PURE
Resident > NIL: C:List PURE

; $VER: LhA2LZX v2.2 (11.7.97) Richard Burke

Failat 60							;Allows errors when using LhA & LZX to be captured

lab Beg
if EXISTS env:drw1
	cd $drw1						;Current Directory is now that which is stated in the contents of the environmental variable 'drw1'
	if EXISTS l2z					;If the temporary drawer used to store the archive files exists
		delete l2z ALL QUIET FORCE	;then it is deleted to start freshly
	endif
	delete env:drw1 QUIET
endif

if NOT EXISTS env:o					;If starting, default path of archive is RAM:
	echo RAM: >env:o				;If converting several archives, the path of the last archive will be used as the default of the next
endif

which lha all >env:wlha				;Finds the location of the LhA tool
if WARN							;If the LhA tool cannot be found
	RequestChoice "LhA2LZX Error Message" "Can't find 'LhA' tool!*nSee docs for details!" "Quit"
	quit
endif

which lzx all >env:wlzx				;Finds the location of the LZX tool e.g. 'C:LZX'
if WARN
	RequestChoice "LhA2LZX Error Message" "Can't find 'LZX' tool!*nSee docs for details!" "Quit"
	quit
endif

lab Path
RequestFile >env:orig TITLE "Full path of drawer of archive?" DRAWERSONLY NOICONS DRAWER "$o"	;Saves the location of the archive in the environment variable 'orig', with the default path as that of the last archive if converting more
if WARN
	skip end
endif

echo >env:o $orig						;Get rid of quotation marks, and also create default path for next archive if converting more
list >env:osz env:o LFORMAT=%L			;Get size of path name
if "$osz" EQ "1"						;Full path not selected
	RequestChoice > NIL: "! Error !" "Choose FULL path!*n(i.e. start with DEVICE listing)" "Try again"
	skip Path BACK
endif

lab File
RequestFile >env:ldfl TITLE "Chose file to convert" POSITIVE Convert ACCEPTPATTERN #?.(lha|lzx) DRAWER $orig		; $ldfl now contains the complete path and filename of the archive
if WARN
	skip More
endif

echo >env:l $ldfl						;Get rid of quotation marks
list >env:lsz env:l LFORMAT=%L			;Get size of file name
if "$lsz" EQ "$osz"						;No file selected
	RequestChoice > NIL: "! Error !" "No file chosen!" "Try again"
	skip File BACK
endif

basename >env:bnm1 $ldfl			;Strips the path of the archive to give only the filename with suffix
basename >env:bnm $bnm1 .lzx		;Removes .lzx suffix from filename if it has it

lab Temp
RequestChoice >env:drw "Default directory" "Where should I store temporary files? A*ntemporary drawer called 'L2Z' will be*ncreated in your selected destination, and*nRAM: will be checked for space if chosen" "RAM:" "Other" "Cancel"
if $drw EQ 1						;If the left button (i.e. RAM:) is chosen
	echo RAM: >env:drw1
endif

if $drw EQ 2						;If "Other" is chosen
	RequestFile >env:drw1 TITLE "Where should 'L2Z' be made?" DRAWERSONLY NOICONS POSITIVE Create
	if WARN
		skip More
	endif
endif

if $drw EQ 0						;If "Cancel" is chosen
	skip More
endif

lab Memchk
set memsz `avail flush total`
cd $orig
list >env:flsz $ldfl LFORMAT=%L
eval >env:flmem $flsz * 4
if NOT $memsz VAL GT $flmem			;If you have less memory than predicted will be needed
	if $drw1 EQ RAM:
		RequestChoice >env:nomem "! Insufficient Memory!" "There may be insufficient memory to store temporary files in RAM:*n(may need at least $flmem bytes)" "Choose other path" "Continue anyway" "Cancel"
		if $nomem EQ 0				;"Cancel"
			skip More
		endif
		if $nomem EQ 1				;"Other"
			skip Temp back
		endif
		if $nomem EQ 2				;"Continue"
			skip MkTemp
		endif
	endif
	if "$drw1" EQ "Ram Disk:"		;If user clicks on RAM: in file requestor
		RequestChoice >env:nomem "! Insufficient Memory!" "There may be insufficient memory to store temporary files in RAM:*n(may need at least $flmem bytes)" "Choose other path" "Continue anyway" "Cancel"
		if $nomem EQ 0				;"Cancel"
			skip More
		endif
		if $nomem EQ 1				;"Other"
			skip Temp back
		endif
		if $nomem EQ 2				;"Continue"
			skip MkTemp
		endif
	endif
endif

lab MkTemp
cd $drw1
if NOT EXISTS l2z					;This creates a temporary drawer in which to de-archive the files in the archive
	makedir > NIL: l2z
else								;Drawer is emptied if it is already there
	delete l2z/#? FORCE QUIET
endif

lab extra
RequestChoice >env:ext "Convert $l to . . ." "Do you want to convert to the other*nfiletype or to the smallest archive?*n*nConvert to:" "Other" "Smallest" "Cancel"
if WARN							;If 'Cancel' is selected
	skip beg BACK					;This skips back to the section labelled 'Beg', at the beginning
endif

if $ext EQ 2						;If the user wants to make the smallest archive
	skip Conv						;This skips forward to the section labelled 'Conv'
endif

if $ext EQ 1						;If the user wants to convert to the other format - original format is found and is intelligently converted to the other
	if $bnm1 EQ $bnm				;Means it is an LhA archive
		delete env:bnm QUIET
		basename >env:bnm $bnm1 .lha
		cd $drw1
		cd l2z
			$wlha x -a $ldfl
		if $RC EQ 20				;If user aborts it will skip to 'More'
			skip More
		endif
		$wlzx -r -e -3 a $bnm #?
		if $RC EQ 50				;If user aborts it will skip to 'More'
			skip More
		endif
		delete $ldfl QUIET
		copy $bnm.lzx TO $orig QUIET
		RequestChoice > NIL: "! Operation Successful !" "$o$bnm.lzx*n  now replaces*n$l" "Okay"
	else									;Means it is NOT an LhA archive (i.e. suffix has been stripped)
		cd $drw1
		cd l2z
		$wlzx x -a $ldfl					;LZX extracts the original archive to temporary drawer 'l2z'
		if $RC EQ 50						;If user aborts it will skip to 'More'
			skip More
		endif
		$wlha -2ar a $bnm #?				;LhA archives everything in 'l2z' and saves it as the original name with .lha suffix
		if $RC EQ 20						;If user aborts it will skip to 'More'
			skip More
		endif
		delete $ldfl QUIET					;Original archive is deleted
		copy $bnm.lha TO $orig QUIET			;New archive is copied in place of the original
		RequestChoice > NIL: "! Operation Successful !" "$o$bnm.lha*n  now replaces*n$l" "Okay"
	endif
endif

skip More

lab Conv						;This part until 'lab More' converts the archive to either LhA or LZX, and saves whichever is smaller
if $bnm1 EQ $bnm				;Means it is an LhA archive (i.e. suffix not stripped)
	delete env:bnm QUIET
	basename >env:bnm $bnm1 .lha
	cd $drw1
	cd l2z
	$wlha x -a $ldfl
	if $RC EQ 20				;If user aborts it will skip to 'More'
		skip More
	endif
	$wlzx -r -e -3 a $bnm #?	;If not aborted, file is archived
	if $RC EQ 50				;If user aborts it will skip to 'More'
		skip More
	endif
else							;Means it is an LZX archive
	cd $drw1
	cd l2z
	$wlzx x -a $ldfl
	if $RC EQ 50				;If user aborts it will skip to 'More'
		skip More
	endif
	$wlha -2ar a $bnm #?		;If not aborted, file is archived
	if $RC EQ 20				;If user aborts it will skip to 'More'
		skip More
	endif
endif

list >env:old $ldfl LFORMAT=%L				;Gets the size of the original archive
list >env:new $bnm.(lzx|lha) LFORMAT=%L			;Gets the size of the new archive
eval >env:diff $old - $new					;Subtracts the size of the new from the original
eval >env:diff1 $new - $old					;Subtracts the size of the original from the new

if $old GT $new VAL													;Original archive is larger than the new one, so the original is deleted and replaced by the smaller new one
	copy $bnm.(lzx|lha) TO $orig QUIET
	delete $ldfl QUIET
	list >env:arc $o$bnm.(lzx|lha) LFORMAT=%S%S							;Gets name of new archive
	echo >env:arc1 $arc												;Removes quotation marks
	RequestChoice > NIL: "! $arc1 used !" "Using the new archive because it's $diff bytes smaller!" "Nice one!"
else																;Original archive is smaller than the new one, so the smaller original one is kept
	RequestChoice > NIL: " $l used" "Using the original archive because it's $diff1 bytes smaller!" "Okay"		;The difference in file sizes is cleverly given
	cd $drw1
	delete l2z ALL QUIET FORCE
endif

lab More
RequestChoice >env:ano "Other archives?" "Do you want to convert another archive?" "Yes" "No"
if $ano EQ 0
	skip end
else
	skip beg back
endif

lab end
echo "*e[1;1H*e[J"													;This clears the output window
echo "*n*n*n                       *e[1;32m  Thank you for using"
echo "                              *e[33m LhA*e[0m*e[3;1m2*e[0m*e[2mLZX"		;All the *e[... parts do is change the colour/style of the text
if EXISTS env:old
	delete env:old QUIET
endif

if EXISTS env:diff
	delete env:diff QUIET
endif

if EXISTS env:diff1
	delete env:diff1 QUIET
endif

if EXISTS env:flsz
	delete env:flsz QUIET
endif

if EXISTS env:memsz
	delete env:memsz QUIET
endif

if EXISTS env:flmem
	delete env:flmem QUIET
endif

if EXISTS env:new
	delete env:new QUIET
endif

if EXISTS env:ano
	delete env:ano QUIET
endif

if EXISTS env:osz
	delete env:osz QUIET
endif

if EXISTS env:o
	delete env:o QUIET
endif

if EXISTS env:arc
	delete env:arc QUIET
endif

if EXISTS env:arc1
	delete env:arc1 QUIET
endif

if EXISTS env:l
	delete env:l QUIET
endif

if EXISTS env:lsz
	delete env:lsz QUIET
endif

if EXISTS env:wlha
	delete env:wlha QUIET
endif

if EXISTS env:ext
	delete env:ext QUIET
endif

if EXISTS env:wlzx
	delete env:wlzx QUIET
endif

if EXISTS env:bnm1
	delete env:bnm1 QUIET
endif

if EXISTS env:bnm
	delete env:bnm QUIET
endif

if EXISTS env:nomem
	delete env:nomem QUIET
endif

if EXISTS env:orig
	delete env:orig QUIET
endif

if EXISTS env:ldfl
	delete env:ldfl QUIET
endif

if EXISTS env:drw1
	cd $drw1
	if EXISTS l2z
		delete l2z ALL QUIET FORCE
	endif
	delete env:drw1 QUIET
endif

if EXISTS env:drw
	delete env:drw QUIET
endif
quit