@echo off

REM ####################################################################
REM # Polaroid film serial Calculator
REM # Olivier BORDRON - 2018
REM #
REM # This script reads the serial number of your Polaroid Originals
REM # or Impossible Project film and returns related informations
REM ####################################################################

SETLOCAL EnableDelayedExpansion

SET SERIAL_NUMBER=%1


TITLE Polaroid Film Serial Calculator
COLOR 1F &::White with dark blue back

REM ********************************************************************
REM *		Code start 
REM ********************************************************************

call :display_welcome_message
call :init_months
call :init_film_types

:code_loop
SET ERROR_NUMBER=0
echo:

REM If SERIAL_NUMBER is not defined, enter it from the keyboard.
REM If just 'Enter' is typed, set a wrong serial.
IF NOT DEFINED SERIAL_NUMBER SET /P SERIAL_NUMBER=Please enter your photo's serial number : || SET SERIAL_NUMBER=0

echo:

REM Get serial number length 
call :str_len %SERIAL_NUMBER% SERIAL_LENGTH

REM Control if the length of the serial is OK
IF NOT %SERIAL_LENGTH% EQU 10 IF NOT %SERIAL_LENGTH% EQU 11 (
	SET ERROR_NUMBER=1
	)

REM Process the two kinds of serial
IF %ERROR_NUMBER% EQU 0 call :process_serial_%SERIAL_LENGTH%

REM Set a fake ERROR_NUMBER to the length of the serial entered.
REM That way, I can display the message related to the serial.
IF %ERROR_NUMBER% EQU 0 SET ERROR_NUMBER=%SERIAL_LENGTH%

call :display_message_%ERROR_NUMBER%


REM Loop to :code_loop to enter a new serial
echo:
choice /M "Do you want to enter a new serial ? "
IF %ERRORLEVEL% EQU 1 (
	SET SERIAL_NUMBER=
	cls
	goto :code_loop
	)

call :destroy_variables
echo:
echo Thank you for using Polaroid Film Serial Decoder
echo:
pause

REM ********************************************************************
REM *	Exit script with a return value = 200
REM ********************************************************************
exit /B 200


REM ********************************************************************
REM *	Process serial number of 11 chars
REM ********************************************************************
:process_serial_11

SET FILM_DD=%SERIAL_NUMBER:~3,2%
SET FILM_MM=%SERIAL_NUMBER:~5,2%
SET FILM_YY=%SERIAL_NUMBER:~7,2%
SET FILM_MACHINE=%SERIAL_NUMBER:~0,2%
SET FILM_TT=%SERIAL_NUMBER:~9,2%
SET FILM_SHIFT=%SERIAL_NUMBER:~2,1%

call :check_date %FILM_DD% %FILM_MM%

exit /B 0

REM ********************************************************************
REM *	Process serial number of 10 chars
REM ********************************************************************
:process_serial_10

SET FILM_DD=%SERIAL_NUMBER:~8,2%
SET FILM_MM=%SERIAL_NUMBER:~0,2%
SET FILM_YY=%SERIAL_NUMBER:~2,2%
SET FILM_MACHINE=%SERIAL_NUMBER:~4,2%
SET FILM_TT=%SERIAL_NUMBER:~6,2%

call :check_date %FILM_DD% %FILM_MM%

exit /B 0

REM ********************************************************************
REM *	End of main code. No execution after this line, except
REM * functions called.
REM * Add exit with return value of 201, just in case
REM ********************************************************************
exit /B 201

REM ********************************************************************
REM *	Check date coherence
REM * Very simple test without leap year as it is not easy to test dates
REM * in batch
REM ********************************************************************
:check_date

call :check_day %~1
call :check_month %~2
REM IF %ERROR_NUMBER% EQU 0 call :check_month %~2

exit /B 0

REM ********************************************************************
REM *	Check day coherence
REM ********************************************************************
:check_day

SET TEMPVAR=

IF %~1 LSS 01 SET TEMPVAR=1
IF %~1 GTR 31 SET TEMPVAR=1
IF DEFINED TEMPVAR SET ERROR_NUMBER=2

SET TEMPVAR=

exit /B 0

REM ********************************************************************
REM *	Check month coherence
REM ********************************************************************
:check_month
SET TEMPVAR=

IF %~1 LSS 01 SET TEMPVAR=1
IF %~1 GTR 12 SET TEMPVAR=1
IF DEFINED TEMPVAR SET ERROR_NUMBER=3

SET TEMPVAR=


exit /B 0



REM ********************************************************************
REM *	Destroy environment variables
REM ********************************************************************
:destroy_variables

FOR /L %%A in (1,1,12) do (
	SET MONTH[%%A]=
)

FOR /L %%A in (1,1,99) do (
	SET FILM_TYPE[%%A]=
)
SET SERIAL_NUMBER=
SET SERIAL_LENGTH=
SET FILM_MACHINE=
SET ERROR_NUMBER=
SET FILM_MESSAGE=
SET DAY_SUFFIX=
SET FILM_SHIFT=
SET FILM_TT=
SET FILM_DD=
SET FILM_MM=
SET FILM_YY=
SET SUFFIX=
SET LEN=
SET S=

exit /B 0

REM ********************************************************************
REM *	Destroy environment variables
REM @echo off
REM Setlocal EnableDelayedExpansion
REM :: strLen String [RtnVar]
REM ::             -- String  The string to be measured, surround in quotes if it contains spaces.
REM ::             -- RtnVar  An optional variable to be used to return the string length.
REM Endlocal&
REM ********************************************************************
:str_len


Set "s=#%~1"
Set "len=0"
For %%N in (4096 2048 1024 512 256 128 64 32 16 8 4 2 1) do (
  if "!s:~%%N,1!" neq "" (
    set /a "len+=%%N"
    set "s=!s:~%%N!"
  )
)
if "%~2" neq "" (set %~2=%len%) else echo %len%


Exit /b 0


REM ********************************************************************
REM *	If error number = 1 display message "bad serial"
REM ********************************************************************
:display_message_1
	echo	Usage : %~n0 serial_number_of_your_film
	echo:
	echo	or      %~n0
	echo	        --^> then enter the serial number when asked
	echo:	
	echo	The serial number must be 10 or 11 characters.
	echo:
exit /B 0

REM ********************************************************************
REM *	If error number = 2 display message "bad day" :P
REM ********************************************************************
:display_message_2

	echo Error in day : %FILM_DD%.

exit /B 0

REM ********************************************************************
REM *	If error number = 3 display message "bad month"
REM ********************************************************************
:display_message_3

	echo Error in month : %FILM_MM%.

exit /B 0

REM ********************************************************************
REM *	If error number = 10 display message 10-chars-serial related
REM ********************************************************************
:display_message_10

call :set_day_suffix %FILM_DD% DAY_SUFFIX

SET FILM_MESSAGE=Your !FILM_TYPE[%FILM_TT%]! was produced on !MONTH[%FILM_MM%]! 20%FILM_YY%, the %FILM_DD%%DAY_SUFFIX% on machine %FILM_MACHINE%.
echo %FILM_MESSAGE%

call :copy_to_clipboard

exit /B 0

REM ********************************************************************
REM *	If error number = 11 display message 11-chars-serial related
REM ********************************************************************
:display_message_11

call :set_day_suffix %FILM_DD% DAY_SUFFIX

SET FILM_MESSAGE=Your !FILM_TYPE[%FILM_TT%]! was produced on !MONTH[%FILM_MM%]! 20%FILM_YY%, the %FILM_DD%%DAY_SUFFIX% on machine %FILM_MACHINE% during the %FILM_SHIFT% shift.

echo %FILM_MESSAGE%

call :copy_to_clipboard

exit /B 0

REM ********************************************************************
REM Copy result to clipboard
REM ********************************************************************
:copy_to_clipboard

echo:
choice /M "Do you want to copy the result to the clipboard ? "
IF %ERRORLEVEL% EQU 1 (echo %FILM_MESSAGE% | CLIP)

exit /B 0

REM ********************************************************************
REM Initialize months.
REM ********************************************************************

:init_months
	
SET	MONTH[01]=January
SET	MONTH[02]=February
SET MONTH[03]=March
SET	MONTH[04]=April
SET	MONTH[05]=May
SET	MONTH[06]=June
SET	MONTH[07]=July
SET	MONTH[08]=August
SET	MONTH[09]=September
SET	MONTH[10]=October
SET	MONTH[11]=November
SET	MONTH[12]=December
	
exit /B 0

REM ********************************************************************
REM Initialize known film types.
REM ********************************************************************

:init_film_types
FOR /L %%A in (1,1,99) do (
	IF %%A LSS 10 (
		SET FILM_TYPE[0%%A]=Unknown film
	) ELSE (
		SET FILM_TYPE[%%A]=Unknown film
	)
)

SET FILM_TYPE[02]=Black and White film for SX-70
SET FILM_TYPE[32]=Black and White film for 600, Image/Spectra or 8x10
SET FILM_TYPE[70]=Color film fox SX-70
SET FILM_TYPE[72]=Color film fox SX-70
SET FILM_TYPE[73]=Color film fox SX-70
SET FILM_TYPE[75]=Color film fox SX-70
SET FILM_TYPE[80]=Color film for 600, Image/Spectra or 8x10
SET FILM_TYPE[82]=Color film for 600, Image/Spectra or 8x10
SET FILM_TYPE[83]=Color film for 600, Image/Spectra or 8x10
SET FILM_TYPE[85]=Color film for 600, Image/Spectra or 8x10
SET FILM_TYPE[33]=Black and White film for I-Type
SET FILM_TYPE[81]=Color film for I-Type
SET FILM_TYPE[84]=Color film for I-Type
SET FILM_TYPE[86]=Color film for I-Type

EXIT /B 0

REM ********************************************************************
REM Set the suffix of the day number
REM ********************************************************************

:set_day_suffix

SET NUMBER=%~1
SET LAST_NUMBER=%NUMBER:~-1%

SET %~2=th

IF %LAST_NUMBER% EQU 1 IF %~1 NEQ 11 SET %~2=st
IF %LAST_NUMBER% EQU 2 IF %~1 NEQ 12 SET %~2=nd
IF %LAST_NUMBER% EQU 3 IF %~1 NEQ 13 SET %~2=rd

SET LAST_NUMBER=
SET NUMBER=

EXIT /B 0


:display_welcome_message

echo ####################################################################
echo # Welcome to Polaroid film serial decoder
echo # Olivier BORDRON - 2018
echo #
echo # This script reads the serial number of your Polaroid Originals
echo # or Impossible Project film and returns related informations
echo ####################################################################

exit /B 0
