#!/bin/bash

LATITUDE=51
DEC_ADJ=$(($LATITUDE - 90))

if [ ! -f "anim/bg.png" ]; then
	echo "You need to put a 280*280 pixel file called bg.png in an anim/ directory."
	echo "Yes, I know there's probably a better way of doing this."
	echo "Your animation will land in there as well."
	exit 1
fi

frame=0

# Earth goes anticlockwise round the sun (viewed from above) so when we look
# north at midnight the direction we face moves anticlockwise throughout the
# year. This means we have to whirl the celestial sphere clockwise gradually
# to simulate that. A clockwise right ascension adjustment is negative, to
# confuse you.

# Each frame shifts our point of view by 5 degrees.

for ra in `seq 0 -5 -355`; do
	echo $frame;
	echo $ra;
	./starbox -f 280 -g 2.5 -C 7 -G -r $ra -d $DEC_ADJ hyg_ra_dec_mag.csv
	convert anim/bg.png face4.pnm anim/bg.png anim/bg.png +append row1.png
	convert face1.pnm face0.pnm face3.pnm face2.pnm +append row2.png
	convert anim/bg.png face5.pnm anim/bg.png anim/bg.png +append row3.png
	convert row1.png row2.png row3.png -append anim/frame${frame}.gif
	frame=$(($frame + 1))
done

# Don't leave all our cruft behind
rm row*.png
rm face*.pnm

# Make the animation
for i in `seq 0 71`; do
	echo anim/frame${i}.gif
done | xargs gifsicle -O2 --loopcount=forever --delay=10 -o anim/animation.gif
