@ECHO OFF

IF %2 NE ARJ .AND. %2 NE ZIP .AND. %2 NE RAR .AND. %2 NE LHA .AND. %2 NE UC2 .AND. %2 NE RKV .AND. %2 NE JAR GOTO HELP

FOR %a in (%1) do GOSUB PROCESS
GOTO END

:PROCESS
echos %a
archid %a >nul

if errorlevel 255 goto NOT_RECOGNIZED
if errorlevel 249 goto OTHER_ERROR
if errorlevel 107 goto NON_SUPPORTED
if errorlevel eq 106 set df=LHA
if errorlevel eq 105 set df=LHA
if errorlevel eq 104 set df=RAR
if errorlevel eq 103 set df=ZIP
if errorlevel eq 102 set df=ZIP
if errorlevel eq 101 set df=ARJ
if errorlevel eq 41 set df=JAR
if errorlevel eq 40 set df=RKV
if errorlevel eq 6 set df=UC2
if errorlevel eq 5 set df=LHA
if errorlevel eq 4 set df=LHA
if errorlevel eq 3 set df=RAR
if errorlevel eq 2 set df=ZIP
if errorlevel eq 1 set df=ARJ
if df=="" goto NON_SUPPORTED

set fn=%@name[%a].%2
if exist %fn% goto ALREADY_EXISTS
echos ` to `%fn%...
MD $A
echos ` un-`%df%...
gosub %df%_U
cd $A
echos ` `%2 comp...
gosub %2_P
gosub CLEAN
echo ` (`%@filesize[%a] to %@filesize[%fn%] bytes)
unset fn
unset df
IF "%3"=="/D" GOSUB DEL_OPTION
IF "%3"=="/S" GOSUB SMALLEST
RETURN

:CLEAN
cd ..
del /q /s /x /z /y $A
RETURN

:DEL_OPTION
echo Deleting original file %a
DEL %a /Q
RETURN

:SMALLEST
IF %@filesize[%a%] GE %@filesize[%fn%] set delfile=%a%
IF %@filesize[%fn%] GT %@filesize[%a%] set delfile=%fn%
echo Deleting larger file %delfile%
del %delfile% /Q
unset delfile
RETURN

:ZIP_U
PKUNZIP -D %a $A >NUL
IF ERRORLEVEL 1 GOTO UNPACK_ERROR
RETURN

:ARJ_U
ARJ X %a $A\ >NUL
IF ERRORLEVEL 1 GOTO UNPACK_ERROR
RETURN

:RAR_U
RAR X -std %a $A\ >NUL
IF ERRORLEVEL 1 GOTO UNPACK_ERROR
RETURN

:LHA_U
LHA X %a $A\ >NUL
IF ERRORLEVEL 1 GOTO UNPACK_ERROR
RETURN

:UC2_U
UC E -S %a #$A >NUL
IF ERRORLEVEL 1 GOTO UNPACK_ERROR
RETURN

:RKV_U
cd $A
rkive -x ..\%a >NUL
cd ..
IF ERRORLEVEL 1 GOTO UNPACK_ERROR
RETURN

:JAR_U
jar16 x %a $A\ >NUL
IF ERRORLEVEL 1 GOTO UNPACK_ERROR
RETURN

:ZIP_P
pkzip -rP -ex ..\%fn% >NUL
IF ERRORLEVEL 1 GOTO PACK_ERROR
RETURN

:ARJ_P
arj a -r -m1 -jm ..\%fn% >NUL
IF ERRORLEVEL 1 GOTO PACK_ERROR
RETURN

:RAR_P
rar a -m5 -s -r -std ..\%fn% >NUL
IF ERRORLEVEL 1 GOTO PACK_ERROR
RETURN

:UC2_P
uc a -s ..\%fn% >NUL
IF ERRORLEVEL 1 GOTO PACK_ERROR
RETURN

:LHA_P
lha a -r ..\%fn% >NUL
IF ERRORLEVEL 1 GOTO PACK_ERROR
RETURN

:RKV_P
rkive -c -mtx -o+ -r -pr ..\%fn% *.* >NUL
IF ERRORLEVEL 1 GOTO PACK_ERROR
RETURN

:JAR_P
jar16 a -r -m4 ..\%fn% >NUL
IF ERRORLEVEL 1 GOTO PACK_ERROR
RETURN

:UNPACK_ERROR
echo Error uncompressing %a.
unset fn
unset df
GOSUB CLEAN
GOTO END

:PACK_ERROR
echo Error compressing %fn%.
unset fn
unset df
GOSUB CLEAN
GOTO END

:ALREADY_EXISTS
echo Destination file (%fn%) already exists.
unset fn
unset df
RETURN

:NOT_RECOGNIZED
echo %a not recognized as an archive!
GOTO END

:OTHER_ERROR
echo SH Archive Identifier terminated with an error.
GOTO END

:NON_SUPPORTED
echo ARCHID recognized %a but this batch file does not support this format.
GOTO END

:HELP
echo.
echo Archive Conversion BTM for SH Archive Identifier 2.41 [10 Feb 97]
echo.
echo Converts archives from ARJ, PKZIP, RAR, LHA (+LHarc), UC2, RKIVE, JAR, and
echo ARJ SFX, PKZIP SFX (+SFXjr), RAR SFX, LHA SFX (+LHarc SFX) format to
echo ARJ, PKZIP, RAR, LHA, UC2, RKIVE, or JAR format (non-SFX).
echo.
echo Usage: ACVT filespec ARJ.ZIP.RAR.LHA.UC2 [/D]
echo   (arg.1) filespec -- files to convert, wildcards ok
echo   (arg.2) arc.type -- destination format, only one allowed
echo   (arg.3) options  -- optional /D or /S (see below)
echo.
echo Options:
echo   /D -- Delete original file(s)
echo   /S -- leave only Smallest archive file(s)

:END
CANCEL 0
