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:Eval PURE
Resident > NIL: C:List PURE

; $VER: LhA2LZX v2.6 (9.12.97) Richard Burke

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

lab Beg
if EXISTS env:drw1				;If the file ENV:drw1 exists, the script will perform the actions to endif
	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

if NOT EXISTS env:d				;If starting, default path of temporary drawer l2z is RAM:
	echo RAM: >env:d			;If converting several archives, the previous path of l2z will be used as the default of the next
endif

which >env:wlha lha all				;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 >env:wlzx lzx all				;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

if NOT EXISTS $orig
	RequestChoice >nil: "! Error !" "PATH does not exist!" "Try again"
	skip Path BACK
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|lzh) DRAWER $orig		; $ldfl now contains the complete path and filename of the archive
if WARN
	skip More
endif

if NOT EXISTS $ldfl
	RequestChoice >nil: "! Error !" "FILE does not exist!" "Try again"
	skip File BACK
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

list >env:bnm1 $ldfl LFORMAT=%S			;Strips the path of the archive to give only the filename with suffix
list >env:bnm $ldfl LFORMAT=%M			;Removes .lzx, .lha or .lzh suffix from filename
list >env:suf $ldfl LFORMAT=%E			;Gives suffix only

if $suf NOT EQ lha				;If file doesn't have .lha suffix
	if $suf NOT EQ lzx			;If file doesn't have .lzx suffix
		if $suf NOT EQ lzh		;If file doesn't have .lzh suffix (which is de-archived by LhA)
			RequestChoice "! Error !" "File is NOT an LhA or LZX archive!" "Choose again!"
		endif
	endif
endif

if $suf EQ lha
	echo LZX >env:nsuf			;Gives the type of file to convert to in a requestor later
	echo LhA >env:suf
endif

if $suf EQ lzx
	echo LhA >env:nsuf
	echo LZX >env:suf			;Makes it look better in the requestor
endif

if $suf EQ lzh
	echo LZX >env:nsuf
	echo LZH >env:suf
endif

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

lab Other
if $drw EQ 2					;If "Other" is chosen
	RequestFile >env:drw1 TITLE "Where should 'L2Z' be made?" DRAWERSONLY NOICONS POSITIVE Create DRAWER "$d"
	if WARN
		skip More
	endif
	echo >env:d $drw1			;Get rid of quotation marks
	list >env:dsz env:d LFORMAT=%L		;Get size of path name
	if $dsz EQ 1				;Full path not selected
		RequestChoice > NIL: "! Error !" "Choose FULL path!*n(i.e. start with DEVICE listing)" "Try again"
		skip Other BACK
	endif
	if NOT EXISTS $drw1
		RequestChoice "! Error !" "PATH does not exist!" "Choose again!"
		skip Other BACK
	endif
endif

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

lab Memchk					;This labels the following section as "Memchk" so the script can skip to this point from anywhere in the script if it encounters the line "skip Memchk"
set memsz `avail flush total`			;Saves the result from "avail flush total" (try it in a shell - it gets the total size of available memory) as ENV:memsz
cd $orig					;CD's to the directory of the original archive
list >env:flsz $ldfl LFORMAT=%L			;Gets size of original archive
eval >env:flmem $flsz * 4			;Multiplies the archive size by 4
if NOT $memsz VAL GT $flmem			;If your available memory size is less than 4 times the size of the original archive (LZX often decompresses to 3-4 times the size of the archive)
	if $drw1 EQ "RAM:"			;and RAM: has been chosen to store temporary files
		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
echo "*e[0 p"				;This clears the display (the output CLI)
RequestChoice >env:ext "Convert $l to . . ." "Do you want to convert to the other*nfiletype or to the smallest archive?*n*nConvert from $suf to:" "$nsuf" "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 $suf EQ lha			;Means it is an LhA archive
	lab L				;Label 'L' so the script can skip straight here if told to
		cd $drw1
		cd l2z
		$wlzx x -a -F -x $ldfl		;As the LZX keyfile is now available, the ability to de-archive LhA files is possible, and also much quicker
		if $RC EQ 20			;If user aborts it will skip to 'More'
			skip More		;RC stands for Return Code - the number returned by LhA if the operation fails
		endif
		$wlzx -r -e -3 -X -F a -Qf $bnm #?
		if $RC EQ 50			;If user aborts it will skip to 'More'
			skip More
		endif
		list >env:old $ldfl LFORMAT=%L			;Gets the size of the original archive
		list >env:new $bnm.lzx 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
		delete $ldfl QUIET				;Original archive is deleted
		copy $bnm.lzx TO $orig QUIET			;New archive is copied in place of the original
		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
			RequestChoice > NIL: "! Operation Successful !" "$o$bnm.lzx*n  now replaces*n$l*n  and is $diff bytes smaller!" "Nice one!"
		else
			RequestChoice > NIL: "! Operation Successful !" "$o$bnm.lzx*n  now replaces*n$l*n  and is $diff1 bytes larger" "Okay"
		endif
	else							;"If that fails, try this..." - i.e. means it is NOT an LhA archive
		if $suf EQ lzh					;It is an LZH archive (processed by LhA)
			skip L BACK
		endif
		cd $drw1					;If this line is reached, it is an LZX archive
		cd l2z
		$wlzx x -a -F -x $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 -2 -a -r -F -y -M -e -x 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
		list >env:old $ldfl LFORMAT=%L
		list >env:new $bnm.lha LFORMAT=%L
		eval >env:diff $old - $new
		eval >env:diff1 $new - $old
		delete $ldfl QUIET				;Original archive is deleted
		copy $bnm.lha TO $orig QUIET			;New archive is copied in place of the original
		if $old GT $new VAL
			RequestChoice > NIL: "! Operation Successful !" "$o$bnm.lha*n  now replaces*n$l*n  and is $diff bytes smaller!" "Nice one!"
		else
			RequestChoice > NIL: "! Operation Successful !" "$o$bnm.lha*n  now replaces*n$l*n  and is $diff1 bytes larger" "Okay"
		endif
	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 $suf EQ lha					;Means it is an LhA archive
	lab L2
	cd $drw1
	cd l2z
	$wlzx x -a -F -x $ldfl			;As the LZX keyfile is now available, the ability to de-archive LhA files is possible, and also much quicker
	if $RC EQ 20				;If user aborts it will skip to 'More'
		skip More			;RC stands for Return Code - the number returned by LhA if the operation fails
	endif
	$wlzx -r -e -3 -X -F a -Qf $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 not an LhA archive
	if $suf EQ lzh				;Means it is an LZH archive
		skip L2 BACK
	endif
	cd $drw1				;If this line is reached, it is an LZX archive
	cd l2z
	$wlzx x -a -F -x $ldfl
	if $RC EQ 50				;If user aborts it will skip to 'More'
		skip More
	endif
	$wlha -2 -a -r -F -y -M -e -x 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
	delete $ldfl QUIET
	copy $bnm.(lzx|lha) TO $orig 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
echo "*ec"
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 "*ec"									;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 - known as ANSI - see "v2.2" in "Program History" in the .guide
echo "*n*n*n"
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:d
	delete env:d QUIET
endif

if EXISTS env:dsz
	delete env:dsz 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:suf
	delete env:suf QUIET
endif

if EXISTS env:nsuf
	delete env:nsuf 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