#!/bin/bash

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

########################################################################
# Display a welcome message
########################################################################
display_welcome_message() {

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

########################################################################
# Display an exit message
########################################################################
display_goodbye_message() {

cat <<- EOF

Thank you for using Polaroid Film Serial decoder
Press any key to exit.
EOF
read -s -n 1
echo
 
}

########################################################################
# Initialize months.
# Specifying the number base 10 (10#)
########################################################################

init_months() {
	
	MONTH[10#01]="January"
	MONTH[10#02]="February"
	MONTH[10#03]="March"
	MONTH[10#04]="April"
	MONTH[10#05]="May"
	MONTH[10#06]="June"
	MONTH[10#07]="July"
	MONTH[10#08]="August"
	MONTH[10#09]="September"
	MONTH[10#10]="October"
	MONTH[10#11]="November"
	MONTH[10#12]="December"
	
}

########################################################################
# Initialize known film types.
# Specifying the number base 10 (10#)
########################################################################

init_film_types() {

for (( i=10#01 ; i <= 99 ; i++ ))
do
	FILM_TYPE[10#$i]="Unkown film"
done

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

}

########################################################################
# Set the suffix of the day number
########################################################################

set_day_suffix(){

local NUMBER=$1
local LAST_NUMBER=${1: -1}
local SUFFIX=th

case $LAST_NUMBER in
	1)
		if [ ! $1 -eq 11 ];then
			SUFFIX=st
		fi
	;;
	2)
		if [ ! $1 -eq 12 ];then
			SUFFIX=nd
		fi
	;;
	3)
		if [ ! $1 -eq 13 ];then
			SUFFIX=rd
		fi
	;;
	*)
		SUFFIX=th
	;;
esac

echo $SUFFIX
}

########################################################################
# Jumping here if the serial number has 10 digits
########################################################################
process_serial_10() {

FILM_DD=${1:8:2}
FILM_MM=${1:0:2}
FILM_YY=${1:2:2}
FILM_MACHINE=${1:4:2}
FILM_TT=${1:6:2}

# Check date coherence
ERROR_NUMBER=$( check_date $FILM_YY$FILM_MM$FILM_DD )

# echo 10 cars
	
}

########################################################################
# Jumping here if the serial number has 11 digits
########################################################################
process_serial_11() {
	
FILM_DD=${1:3:2}
FILM_MM=${1:5:2}
FILM_YY=${1:7:2}
FILM_MACHINE=${1:0:2}
FILM_TT=${1:9:2}
FILM_SHIFT=${1:2:1}

# Check date coherence
ERROR_NUMBER=$( check_date $FILM_YY$FILM_MM$FILM_DD )

# echo 11 cars
	
}


########################################################################
# main_proc : 
# Initialize variables
# Verify the number of chars of the serial number (ARGS_LENGTH)
# Run the function depending on the number of chars (process_serial_$ARGS_LENGTH)
# Calls usage function if needed
########################################################################
main_proc() {

SERIAL_NUMBER=$2

if [ $NUMBER_OF_ARGS -eq 0 ]; then
	enter_serial
fi

typeset ERROR_NUMBER=0

init_film_types
init_months

typeset -i SERIAL_NUMBER_LENGTH=${#SERIAL_NUMBER}

# Check if the length of the SN is OK
if [[ $SERIAL_NUMBER_LENGTH -ne 11 && $SERIAL_NUMBER_LENGTH -ne 10 ]]; then
	ERROR_NUMBER=2
fi 

# Initialize variables related to the SN and check date coherence.
if [ $ERROR_NUMBER -eq 0 ]; then
	process_serial_$SERIAL_NUMBER_LENGTH $SERIAL_NUMBER
fi

# If no error encountered, set error number to the length of the serial
# That way, I can display the related message.
if [ $ERROR_NUMBER -eq 0 ]; then
	ERROR_NUMBER=$SERIAL_NUMBER_LENGTH
fi

	display_message_$ERROR_NUMBER

# Ask for continue or not
echo
read -s -n 1 -p "Do you want to enter a new serial (Y/N) [N] ? " ANSWER_NEW_SERIAL
echo
case $ANSWER_NEW_SERIAL in
	[Yy][Ee][Ss]|[Yy])
		ARG=
		NUMBER_OF_ARGS=0
		CONTINUE_DECODE=1
		clear
		;;
	[Nn][Oo]|[Nn])
		CONTINUE_DECODE=0
		;;
	*)
		CONTINUE_DECODE=0
		;;
esac

}

########################################################################
# If error number = 1 display message "bad date"
########################################################################
display_message_1 () {
	
	echo "Error in day or month : please check."
	
}

########################################################################
# Usage : display usage of the script
########################################################################
display_message_2 () {
	PROG_BASENAME=$(basename $0)
		cat <<- EOF
		
		Usage : $PROG_BASENAME serial_number_of_your_film

		or      $PROG_BASENAME
		        --> then enter the serial number when asked
		
		The serial number must be 10 or 11 characters.
		
		EOF
}



########################################################################
# Display message related to a 10 digit serial number
########################################################################
display_message_10 () {
	DAY_SUFFIX=$(set_day_suffix $FILM_DD)
	echo Your ${FILM_TYPE[10#$FILM_TT]} was produced on ${MONTH[10#$FILM_MM]} 20$FILM_YY, the $FILM_DD$DAY_SUFFIX on machine $FILM_MACHINE
	echo
}

########################################################################
# Display message related to a 11 digit serial number
########################################################################
display_message_11 () {
	DAY_SUFFIX=$(set_day_suffix $FILM_DD)
	echo Your ${FILM_TYPE[10#$FILM_TT]} was produced on ${MONTH[10#$FILM_MM]} 20$FILM_YY, the $FILM_DD$DAY_SUFFIX on machine $FILM_MACHINE during the $FILM_SHIFT shift
	echo
}


########################################################################
# check_date : check date coherence.
# Check date given as argument (format : YYMMDD)
# return value echoed = 0 => date OK
# return value echoed != 0 => date NOT OK
########################################################################
check_date (){
	#echo Date checked : $1
	date "+%y%m%d" -d "$1" > /dev/null
	echo $?	
}

########################################################################
# Read keyboard entry 
########################################################################
enter_serial (){
	
	echo
	read -p "Please enter your photo's serial number : " SERIAL_NUMBER
	
}
########################################################################
# Entry point of the script
# Read keyboard entry if no argument given
# call main_proc
########################################################################

display_welcome_message

ARG=$1
NUMBER_OF_ARGS=$#
CONTINUE_DECODE=1

while [ $CONTINUE_DECODE -eq 1 ];do
	main_proc $NUMBER_OF_ARGS $ARG
done

display_goodbye_message
