#!/bin/sh

set -e
set -x

DB="psql -At -d gallery"

db() {
	$DB -c "$1"
}

OP=$PWD

userbase='/var/www/granicus.if.org/gallery/users'

cd $userbase

# get a file lock

hashfile() {
 	sha1sum "$1" | awk '{print $1}'
}

thumbnail() {
	convert "$1" -thumbnail '160x90' png:${2}.tmp
	mv ${2}.tmp $2
}

basepic() {
	convert "$1" -thumbnail '800x450' png:${2}.tmp
	mv ${2}.tmp $2
}

registerfile() {
	hashf=$(hashfile "$1")
	if [ -z "$3" ]; then
		$OP/dbq 'insert into media (id, owner, filename,valid) values (?,?,?,false) on conflict do nothing returning id' "$hashf" "$2" "$1"
	else
		$OP/dbq 'insert into media (id, owner, filename,tags,valid) values (?,?,?,ARRAY[?]::text[], false) on conflict do nothing returning id' "$hashf" "$2" "$1" "$3"
	fi
	printf '%s\n' $hashf
}

process() {
	file=$1
	dest=$2
	ext=$3
	owner=$4

	mkdir -p ../media ../thumb ../base

	hashf=$(hashfile "$file")

	registerfile $file $owner
	thumbnail "$file" ../thumb/${hashf}.png
	basepic "$file" ../base/${hashf}.png
	ln -f "$file" ../media/$hashf.$ext
	$OP/dbq 'update media set thumb = ?, base = ?, valid = true where id = ? and owner = ? returning id' thumb/${hashf}.png base/${hashf}.png $hashf $owner
	rm "$file"
}

video() {
	file=$1
	owner=$2
	ext=$3

	hashf=$(hashfile "$file")
	# extract first frame
	ffmpeg -i "$file" -f image2 -ss 0 -vframes 1 ../unknown/${hashf}.png
	# convert first frame to thumbnail
	thumbnail ../unknown/${hashf}.png ../thumb/${hashf}.png
	# convert first frame to base
	basepic ../unknown/${hashf}.png ../base/${hashf}.png
	rm ../unknown/${hashf}.png
	$OP/dbq 'update media set thumb = ?, base = ?, valid = true where id = ? and owner = ? returning id' thumb/${hashf}.png base/${hashf}.png $hashf $owner
	ln -f "$file" ../media/$hashf.$ext
	rm "$file"
}

transcode() {
	file=$1
	ffmpeg -i "$file" ../video/${file%.*}.ogg
	ffmpeg -i "$file" ../video/${file%.*}.webm
	ffmpeg -i "$file" ../video/${file%.*}.mp4
}

run=1
while [ $run -ne 0 ]; do
	run=0
for u in $(ls); do
	if [ ! -d $u/incoming ]; then continue; fi
	userdir="$userbase/$u"
	userspool="$userbase/$u/incoming"
	usergroups="$userbase/$u/groups"
	cd $userspool
	for file in $(ls); do
		echo processing "'$file'"
		case $file in
			*.png|*.PNG)
				process "$file" image png $u
				;;
			*.jpg|*.jpeg|*.JPG)
				process "$file" image jpg $u
				;;
			*.MOV|*.mov)
				video "$file" $u mov
				true
				;;
			*.tar.gz)
				gallery=$(basename "$file" .tar.gz)
				mkdir -p $usergroups/$gallery
				tar -C $usergroups/$gallery --no-recursion -xf "$file"
				run=1
				# race condition.  unpack, die, then not registered?
				# ok.  will be picked up later?
				cd $usergroups/$gallery
				for tagged in $(ls); do
					registerfile $tagged $u $gallery
					mv $tagged $userspool
				done
				cd $userspool
				rm $file
				;;
			*.zip)
				gallery=$(basename "$file" .zip)
				mkdir -p $usergroups/$gallery
				(cd $usergroups/$gallery && unzip -jo $userspool/$file)
				run=1
				# race condition.  unpack, die, then not registered?
				# ok.  will be picked up later?
				cd $usergroups/$gallery
				for tagged in $(ls); do
					echo $owner
					registerfile $tagged $u $gallery
					mv $tagged $userspool
				done
				cd $userspool
				rm $file
				;;
			*.ZIP)
				gallery=$(basename "$file" .ZIP)
				mkdir -p $usergroups/$gallery
				(cd $usergroups/$gallery && unzip -jo $userspool/$file)
				run=1
				# race condition.  unpack, die, then not registered?
				# ok.  will be picked up later?
				cd $usergroups/$gallery
				for tagged in $(ls); do
					registerfile $tagged $u $gallery
					mv $tagged $userspool
				done
				cd $userspool
				rm $file
				;;
			*)
				mv "$file" ../unknown
				;;
		esac
	done
	cd ../..
	#cd ../library
	#for file in *; do
	#	convert $file -thumbnail '100x100>' \
	#		-background skyblue -gravity center \
	#		-extent 100x100 ../thumb/$(basename $file .jpg).png
	#done
done
done
