@SetLocal
@Echo off

IF "%1" == "" (
	echo Usage: ForkIt FILETYPE [SCANONLY] [DEBUG] PATH [SERVER]
	echo FILETYPE - Can be GIF, HTM, HTML, JAR, JPE, JPEG, JPG, PDF, ZIP, or VPS. Enter ALL for all types.
	echo SCANONLY - Just scan folders, do not fix file.
	echo DEBUG    - Show file names as it scans.
	echo PATH     - Is the full local path to the file. For remote servers, use the UNC path.
	echo          - Cannot fix files on remote servers, can just scan files.
	echo SERVER   - Is remote server name or blank for local
	GOTO :EX
)

set scanOnly=%2
set scanOnly=%scanOnly:"=%
IF "%scanOnly%"=="SCANONLY" (
	SHIFT /2
	set scanOnly=1
) ELSE (
	set scanOnly=0
)

set debug=%2
set debug=%debug:"=%
IF "%debug%"=="DEBUG" (
	SHIFT /2
	set debug=1
	echo Enabling debug mode...
) ELSE (
	set debug=0
)


IF NOT "%3" == "" (
	set scanOnly=1
)

IF "%scanOnly%"=="1" echo Just scanning folders, will not forkize files...

set filePath=%2
set filePath=%filePath:"=%
set slash=%filePath:~-1%
if not "%slash%"=="\" set filePath=%filePath%\
set filePath=%filePath%*%fileExtension%
IF NOT "%3"=="" SET Server=/Server:%3

IF "%1" == "ALL" (
	call :ForkMe PDF
	call :ForkMe JAR
	call :ForkMe ZIP
	call :ForkMe HTM
	call :ForkMe HTML
	call :ForkMe GIF
	call :ForkMe JPG
	call :ForkMe JPE
	call :ForkMe JPEG
	call :Forkme VPS
) ELSE (
	call :ForkMe %1
)


GOTO :EX

REM ForkMe FILETYPE
:ForkMe

	IF "%1" == "PDF" (
		SET fileCreator=CARO
		SET fileType=PDF
		SET fileExtension=.PDF
	)
	
	IF "%1" == "JAR" (
		SET fileCreator=java
		SET fileType=ZIP
		SET fileExtension=.JAR
	)
	
	IF "%1" == "ZIP" (
		SET fileCreator=java
		SET fileType=ZIP
		SET fileExtension=.ZIP
	)
	
	
	IF "%1" == "HTM" (
		SET fileCreator=MSIE
		SET fileType=TEXT
		SET fileExtension=.HTM
	)
	
	IF "%1" == "HTML" (
		SET fileCreator=MSIE
		SET fileType=TEXT
		SET fileExtension=.HTML
	)
	
	IF "%1" == "GIF" (
		SET fileCreator=MSIE
		SET fileType=GIFf
		SET fileExtension=.GIF
	)
	
	IF "%1" == "JPG" (
		SET fileCreator=MSIE
		SET fileType=JPEG
		SET fileExtension=.JPG
	)
	
	IF "%1" == "JPE" (
		SET fileCreator=MSIE
		SET fileType=JPEG
		SET fileExtension=.JPE
	)
	
	IF "%1" == "JPEG" (
		SET fileCreator=MSIE
		SET fileType=JPEG
		SET fileExtension=.JPEG
	)

	IF "%1" == "VPS" (
		SET fileCreator=Cre2
		SET fileType=TIFF
		SET fileExtension=.VPS
	)
	
	echo Checking %1 files...
	FOR /F "delims=; tokens=1" %%A IN ('dir "%filePath%" /b /s') DO (
REM		echo %%A
		IF NOT "%%~xA" == "" (
			echo %fileExtension% | findstr /I /C:"%%~xA" > NUL
			IF not errorlevel 1 (
				IF "%debug%" == "1" echo %%A
				echo %%A | findstr /I /C:"?" > NUL
				IF NOT errorlevel 1 (
					echo File has weird characters, not checking: %%A
				) ELSE (
					IF NOT exist "%%A" (
						echo Cannot find file, not checking: %%A
					) ELSE (
						more < "%%A:AFP_AfpInfo" | findstr /I /C:LMAN > NUL
						IF not errorlevel 1 (
							echo Requires forkizing: %%A
							IF NOT "%scanOnly%"=="1" (
								macfile forkize %Server% /Creator:%fileCreator% /Type:%fileType% /TargetFile:"%%A"
							)
						)
					)
				)
			)
		)
	)
	
	
	GOTO :EOF


:EX

EndLocal