@echo off
rem PSID.Amiga_Long_File_Name convertor to MSDOS short filename convertor.
rem Also handles wierd characters in names and other strange filenames.
rem This script needs 4dos, lha, sort and more.
rem Original script written by Mike Paull (mike@dino.eng.monash.edu.au or
rem be165@cleveland.freenet.edu)
REM Script modified by Adam Lorentzon (d93-alo@nada.kth.se) in March -95
REM to extract the files into proper directories and add file descriptions.
REM All my modifications were made with the NemeSID archives in mind.
IF %# != 1 (ECHO Usage: epex archivename ^ QUIT)
lha v /n %1 |sort > list
echo ---- Start of %1 ---- >> names.log
set fh=%@fileopen[list,read]
set ct=1
set lf=
set mf=
set fn=
:start
set ctr=1
set FULLNAME=%@fileread[%fh]
SET SLASHIDX=%@INDEX[%FULLNAME,/]
IFF %SLASHIDX == -1 THEN
    SET PATHNAME=""
ELSE
    SET PATHNAME=%@SUBSTR[%FULLNAME,0,%SLASHIDX]
ENDIFF
SET fn=%@SUBSTR[%FULLNAME,%@INC[%SLASHIDX],%@LEN[%FULLNAME]]
if "%@substr[%fn,5,8]" == "%lf" goto message
set ct=1
set mf=%@substr[%fn,5,8]
set lf=%mf
if "%fn" == "**EOF**" goto end
do %@len[%fn]
if "%@substr[%fn,%ctr,1]" == "+" gosub noplus
set ctr=%@inc[%ctr]
enddo
:extract
echo extracting %fn
lha e /a1 %1 %fn
set tf=%@substr[%fn,0,8]
set cnt=5
set len=%@len[%fn]
set len=%@eval[%len-5]
do %len
if "%@substr[%fn,%cnt,1]" == "." gosub conv
if "%@substr[%fn,%cnt,1]" == "." leave
set cnt=%@inc[%cnt]
enddo
attrib -h %tf
GOSUB CONVPATH
SET DESCRIPT=%@SUBSTR[%FN,5,%@LEN[%FN]]
echo Converting %FULLNAME to %PATHCONV\%mf.dat >> names.log
IFF %PATHCONV == "" THEN
    ECHO MOVE %TF %MF.DAT
    MOVE %TF %MF.DAT
    DESCRIBE %MF.DAT "%DESCRIPT"
ELSE
    IF NOT ISDIR %PATHCONV (MD %PATHCONV ^ DESCRIBE %PATHCONV "%PATHNAME")
    MOVE %tf %PATHCONV\%mf.dat
    DESCRIBE %PATHCONV\%mf.dat "%DESCRIPT"
ENDIFF
goto start
:end
set fh=%@fileclose[%fh]
del list > nul
echo ---- finish of %1 ---- >> names.log
quit
:message
set ct=%@inc[%ct]
if %ct lt 10 set mf=%@substr[%mf,0,7]%ct
if %ct ge 10 set mf=%@substr[%mf,0,6]%ct
if %ct ge 100 set mf=%@substr[%mf,0,5]%ct
goto extract

:CONVPATH
REM ** Removes all '.' characters from directory names
SET LEN=%@LEN[%PATHNAME]
IFF %@INDEX[%PATHNAME,.] != -1 THEN
    SET PATHCONV=
    SET NUM=0
    SET CHR=
    DO %LEN
        SET CHR=%@SUBSTR[%PATHNAME,%NUM,1]
        IF "%CHR" != "." SET PATHCONV=%PATHCONV%%CHR
        SET NUM=%@INC[%NUM]
    ENDDO
    SET LEN=%@LEN[%PATHCONV]
ELSE
    SET PATHCONV=%PATHNAME
ENDIFF
REM ** Removes the 'Unknown-' and 'Various-' prefixes
IFF %LEN LE 8 THEN
    RETURN
ELSEIFF "%@SUBSTR[%PATHCONV,0,8]" == "Unknown-" THEN
    SET PATHCONV=%@SUBSTR[%PATHCONV,8,%@EVAL[%LEN-8]]
ELSEIFF "%@SUBSTR[%PATHCONV,0,8]" == "Various-" THEN
    SET PATHCONV=%@SUBSTR[%PATHCONV,8,%@EVAL[%LEN-8]]
ENDIFF
REM ** Truncates the pathname to 8 characters
SET PATHCONV=%@SUBSTR[%PATHCONV,0,8]
RETURN

:conv
set num=0
set wrd=
set chr=
set len=%@len[%mf]
do %len
set chr=%@substr[%mf,%num,1]
if "%chr" == "." set chr=%@char[00]
set num=%@inc[%num]
set wrd=%wrd%%chr
enddo
set mf=%wrd
:conv2
set num=0
set wrd=
set chr=
set len=%@len[%tf]
do %len
set chr=%@substr[%tf,%num,1]
if "%chr" == "." set chr=%@char[95]
set num=%@inc[%num]
set wrd=%wrd%%chr
enddo
set ext=%@ext[%fn]
set tf=%wrd.%%ext
return

:noplus
set num=0
set nn=
do %@len[%fn]
set chr=%@substr[%fn,%num,1]
if "%chr" == "+" set chr=%@char[42]
set num=%@inc[%num]
set nn=%nn%%chr
if "%chr" == "*" leave
enddo
set num=0
set chr=
set nm=
do %@len[%mf]
set chr=%@substr[%mf,%num,1]
if "%chr" == "+" set chr=%@char[95]
set num=%@inc[%num]
set nm=%nm%%chr
enddo
set mf=%nm
set fn=%nn
return


