#!/bin/sh
#
# This script checks out and prepares the full version Kobo Redux data files.
# (Of course, this will not work without access to the private repository.)
#
. ./BUILDDEFS

if [ $# -eq 2 ]; then
	TAG="$1"
	DATADIR="$2"
else
	echo "Usage: install-data <tag> <data-directory>"
	exit 1
fi

if [ -d "${WORKDIR}/${DATADIR}" ]; then
	# TODO: Check if it's the right version. If not, overwrite!
	echo "Data directory ${DATADIR} already exists!"
	exit 0
fi

set -ex

render=false

# Clone or update repository as needed
if [ ! -d "${DLDIR}" ]; then
	mkdir -p "${DLDIR}"
fi
cd ${DLDIR}
if [ ! -d "koboreduxassets" ]; then
	git clone git@github.com:olofson/koboreduxassets.git
	cd koboreduxassets
	git checkout ${TAG}
	render=true
else
	cd koboreduxassets
	if ! git checkout ${TAG} ; then
		git checkout master
		git pull
		git checkout ${TAG}
		render=true
	fi
	git clean -d -x -f
fi

# Render the TIPE sprites
cd src
eel render-sprites
cd ..

# Copy the checked out and rendered data to the destination directory
if [ ! -d "${WORKDIR}" ]; then
	mkdir "${WORKDIR}"
fi
cp -r "${DLDIR}/koboreduxassets/data" "${WORKDIR}/${DATADIR}"
echo ${TAG} > "${WORKDIR}/${DATADIR}/DATAVERSION"
