#!/bin/sh

[ -f ./procdata ] && cd ..
if [ ! -f tools/procdata ]; then
	echo 'run from the demo root directory' >&2
	exit 1
fi

# process embedded images
if [ ! -f tools/img2bin/img2bin ]; then
	make -C tools/img2bin || exit 1
fi
alias img2bin=tools/img2bin/img2bin

mkdir -p data
if [ ! -f data/loading.img -o data/loading.png -nt data/loading.img ]; then
	echo 'img2bin: loading'
	img2bin data/loading.png || exit 1
fi

# process compiled sprites
if [ ! -f tools/csprite/csprite ]; then
	make -C tools/csprite || exit 1
fi
alias csprite=tools/csprite/csprite

mkdir -p cspr
if [ ! -f cspr/dbgfont.asm -o data/legible.fnt -nt cspr/dbgfont.asm ]; then
	echo 'csprite: dbgfont'
	csprite -n cs_dbgfont -s 8x16 -conv565 -nasm -xor data/legible.fnt >cspr/dbgfont.asm || exit 1
fi
if [ ! -f cspr/confont.asm -o data/console.fnt -nt cspr/confont.asm ]; then
	echo 'csprite: confont'
	csprite -n cs_confont -s 6x7 -pad 1 -conv565 -nasm data/console.fnt >cspr/confont.asm || exit 1
fi

# XXX if you uncomment the font baking, remember to change advance: 12 and
# for glyph 91, o:0,4

# convert ttf font to glyphmaps
#if [ ! -f data/aleph0.gmp -o data/src/aleph0.ttf -nt data/aleph0.gmp -o $0 -nt data/aleph0.gmp ]; then
#	echo 'font2glyphmap: aleph0.ttf'
#	font2glyphmap -o data/aleph0.gmp -size 12 -padding 0 -range 64-92 data/src/aleph0.ttf
#fi

# convert goat3d scenes
srcdir=data/src/3dzoom
dstdir=data/3dzoom

convimg()
{
	if [ ! -f $2 -o $1 -nt $2 -o $0 -nt $2 ]; then
		sz=`identify $1 | awk '{ print $3 }'`
		echo "image $1 -> $2 ($sz -> $3)"
		convert -resize $3 -quality 95 $1 $2
	fi
}

# -- reactor room
mkdir -p $dstdir
if [ ! -f $dstdir/reactor.g3d -o $srcdir/reactor.obj -nt $dstdir/reactor.g3d ]; then
	if ass2goat $srcdir/reactor.obj; then
		mv $srcdir/reactor.goat3d $dstdir/reactor.g3d
	fi
fi

for i in $srcdir/wall*.png; do
	dest=$dstdir/`basename $i .png`.jpg
	convimg $i $dest 256x256
done

convimg $srcdir/pool.png $dstdir/pool.jpg 64x64
convimg $srcdir/base.png $dstdir/base.jpg 256x128
convimg $srcdir/floor.png $dstdir/floor.jpg 128x256
#convimg $srcdir/columns.png $dstdir/columns.jpg 32x256

# -- space scene
srcdir=data/src/space
if [ ! -f data/frig8.g3d -o $srcdir/frigate.obj -nt data/frig8.g3d ]; then
	if ass2goat $srcdir/frigate.obj; then
		mv $srcdir/frigate.goat3d data/frig8.g3d
	fi
fi

convimg $srcdir/frigtex.png data/frigtex.jpg 256x256

convimg data/src/planet.png data/planet.png x125

# generate curved tunnel multi-lut!
#if [ ! -f tools/tungen ]; then
#	if ! cc -o tools/tungen -O3 -fopenmp tools/tungen.c -lm -lgomp -lpthread; then
#		echo "failed to build tunnel LUT generator" >&2
#		exit 1
#	fi
#fi
#tunlut=data/tunnel.lut
#if [ ! -f $tunlut -o $0 -nt $tunlut ]; then
#	curve_frames=32
#	curve_dist=533
#	#size=`awk '/define VSCALE/ { print $3 * 320 "x" $3 * 240 }' src/scr/tunnel.c`
#	size=320x240
#	echo "generating tunnel LUT: $size, $curve_frames frames ..."
#	tools/tungen -s $size -y -c $curve_dist -n $curve_frames >$tunlut
#fi

# create pre-blurred 256c tunnel texture
for i in 1 2 3; do
	infile=data/src/tunnel$i.png
	outfile=data/tunnel$i.png
	if [ ! -f $outfile -o $infile -nt $outfile -o tools/tunblur -nt $outfile ]; then
		echo "pre-blur tunnel texture: $infile -> $outfile"
		tools/tunblur $infile $outfile
	fi
done
