#!/bin/bash

PGM=`basename "$0"`
TMP="/tmp/$PGM.$$"

esc_html() {
  string="$@"
  # Escape any single quotes
  echo "$string" | sed "s/'/\\\\\\\'/g"
}

echo "<?php"

if [ "$1" = "-s" ]
then
	printf "\$customnames=array(\n"
	mode=source
	sort=true
	shift
else
	printf "\$reserve=array(\n"
	sort=false
fi

if [ "$1" ] ## a text list was provided as reference
	then
	for param in "$@"
	do
		if [ -f "$param" ]
			then
			# Process file line by line to handle both regular entries and sections
			cat "$param" | while read line; do
				# Check if line is a section marker
				if echo "$line" | grep -q "^[[:blank:]]*//[[:blank:]]*section:[[:blank:]]*"; then
					# Extract section name
					section_name=$(echo "$line" | sed -E 's/^[[:blank:]]*\/\/[[:blank:]]*section:[[:blank:]]*//' | sed 's/[[:blank:]]*$//')
					# Create section ID
					section_id=$(echo "$section_name" | tr '[:upper:]' '[:lower:]' | sed 's/[[:blank:]]\+/-/g' | sed 's/[^a-z0-9-]//g')
					echo "SECTION:#$section_id:$section_name"
				else
					echo "$line"  | cut -d '"' -f 2 \
				| sed "s|$| //|" \
				| sed "s|[[:blank:]]*//|.mp4 //|" \
				| sed "s|[[:blank:]]*//[[:blank:]]*$||" \
			 	| sed "s|/|-SLASH-|g"
				fi
			done
		elif [ -d "$param" ]
			then
			sort=true
			ls "$param"/*.mov "$param"/*.mp4 "$param"/*.m4v "$param"/*.mp3 "$param"/*.mpg "$param"/*.pdf "$param"/*.jpg  2>/dev/null \
				| grep -v "\-large."\
				| grep -v "\-dv."
		fi
	done | while read name
	do
		echo "$name"
	done
else # no text list was provide, simply scan files in folder
	sort=true
	ls *.mov *.mp4 *.m4v *.mp3 *.mpg *.jpg *.pdf 2>/dev/null | grep -v "\-large."\
		| grep -v "\-dv."
fi | grep -v "^qt-thumbnails" | while read name
do
	# Check if this is a section entry
	if echo "$name" | grep -q "^SECTION:"; then
		# Extract section ID and name
		section_id=$(echo "$name" | cut -d: -f2)
		section_name=$(echo "$name" | cut -d: -f3-)
		printf "\t'$section_id' => '$section_name',\n"
		continue
	fi
	
	unset info
	info=$(echo "$name" | grep "//" | sed "s|[^/]*//[[:blank:]]*||" | sed "s| */[/ ]*| |" | sed "s|^| // |")
	info=$(echo "$name" | grep "\-SLASH--SLASH-" | sed "s|[^/]*-SLASH--SLASH-[[:blank:]]*||" | sed "s| */[/ ]*| |")
	name=$(echo "$name" | sed "s|[[:blank:]]*-SLASH--SLASH-.*||" )
	file=`echo "$name" | webnormalize | sed "s/[_-]*SLASH-.*/_/g" | sed "s/_*$//" | sed "s/\.jpg\.mp4$/.jpg/"`
	name=$(basename "$name" | sed "s|-SLASH-|/|g" | sed "s|\.[a-zA-Z0-9]*$||")
	#file=$name
	name=`echo "$name" | sed "s/\.[a-zA-Z0-9]*$//" | sed "s/_/ /g" | sed "s/[-\. ][0-9]*$//"`
	name=$(esc_html "$name")
	second=`echo "$file" | egrep "^[0-9]*,[0-9]*-" | cut -d "-" -f 1 | grep "," | cut -d "," -f 2`
	first=`echo "$file" | egrep "^[0-9]" | cut -d "-" -f 1 | cut -d "," -f 1`

	if [ "$second" ]
		then
		if [ $second -gt $first ]
			then printf "${second}b\t"
		else
			printf "${first}b\t"
		fi
	else
		printf "${first}\t"
	fi
	unset infohtml
	unset infocomment
	if [ "$info" ]
	then
		infohtml=" <span class=info>$info</span>"
		infocomment=$(
		echo $info | sed -E "s#[[:blank:]]*(//*|,)[[:blank:]]*#\n#g" | grep . | while read comment
		do
			printf "// _$(webnormalize "$comment") "
		done
		)
		# infocomment=$(webnormalize " // $info");
	fi
	if [ "$mode" = "source" ]
	then
		nr="$name"
		mp4=../.mp4/$(basename "$file" .mov).mp4
		[ -f "$mp4" ] && file=$mp4 && res="" || res="(HD)"
		name=$(egrep "^$nr[[:blank:],-]" liste | sed "s/[0-9,-]*[[:blank:]][[:blank:]]*//")
		name=$(esc_html "$name")
		time=$(grep "^$nr.mov" times | cut -d " " -f 2 | sed "s/00://" | sed "s/:/\\\\'/")
		printf "\t\"$file\" => '$nr - $name $res $time',\n"
		# printf "\t\"$file\" => '$name$infohtml',$infocomment\n"
	else
		printf "\t\"$file\" => '$name$infohtml',$infocomment\n"
	fi
	folder=$(echo "$file" | cut -d. -f 1)
	# echo "$PWD/$folder" >&2
	if [ -d "../$folder" ] 
	then
		# echo "adding $folder" >&2
		printf "\t\"$folder\" => '$name',\n" >> $TMP.folderlist
	fi
done > "$TMP.playlist"

if [ "$sort" = "true" ]
	then
	cat "$TMP.playlist" | sort -n | cut -f 2-
else
	cat "$TMP.playlist" | cut -f 2-
fi

echo ");"
grep -q . $TMP.folderlist 2>/dev/null && echo '$customfolders = array(' && cat $TMP.folderlist && echo ");"
found=$(cat "$TMP.playlist" "$TMP.folderlist" 2>/dev/null | wc -l)
[ $found -eq 0 ] && echo "\$hidden=true;"
echo "?>"

rm -f "$TMP.playlist"
