#!/bin/sh

# This script takes the path of a .decl file
# and puts information from local mirrors
# of the file into ./index/$PKGNAME/* files

# The files are separated so they can be
# individually checked/regenerated when
# missing as neccessary

# We need to generate the sums and signatures
# somewhere before we let the remote
# CI/build process assemble the package lists
# as the remote has no access to local mirrors
# of the files.

DECL="$1"
DECL_IMG=$(basename "$1" | cut -d '.' -f 1)
DECLDIR=$(dirname "$DECL")
TYPE=$(dirname "$DECLDIR")
TYPE=$(basename "$TYPE")
GAMEDIR=$(basename "$DECLDIR")

# grep the values
NAME=$(grep packageInfo "$DECL" | awk '{ print $2 }')
URL_FALLBACK=$(grep \"url\" "$DECL" | awk -F"\""  '{ print $4 }')
URL=$(grep \"url_archive\" "$DECL" | awk -F"\""  '{ print $4 }')
LOCAL_URL=$(grep \"local_url\" "$DECL" | awk -F"\""  '{ print $4 }')
PKGLIST=$(grep \"gamename\" "$DECL" | awk -F"\""  '{ print $4 }')
PACKPREFIX=$(grep \"packprefix\" "$DECL" | awk -F"\""  '{ print $4 }')
STARTMAP=$(grep \"startmap\" "$DECL" | awk -F"\""  '{ print $4 }')

if [ -z "$URL" ]
then
	# If it's public, it has to at least be this.
	URL="$URL_FALLBACK"
fi

if [ -z "$LOCAL_URL" ]
then
	LOCAL_URL="$(pwd)/_files/$(basename "$URL")"
fi

if [ ! -f "$LOCAL_URL" ]
then
	printf "Error: Package %s not on local server.\n" "$NAME"  >&2
	exit 2
fi

if [ $TYPE = Addons ]
then
	PREFIX="addon"
fi

if [ $TYPE = Maps ]
then
	PREFIX="map"
fi

GPREFIX=$GAMEDIR

if [ $TYPE = Mods ]
then
	PREFIX="mod"
	GAMEDIR=$(grep \"gamedir\" "$DECL" | awk -F"\""  '{ print $4 }')
fi

if [ $TYPE = Campaigns ]
then
	PREFIX="campaign"
fi

if [ $TYPE = Patches ]
then
	PREFIX="patch"
fi

PKGNAME=$(echo "$GPREFIX"-"$PREFIX"-"$NAME" | awk '{print tolower($0)}')
mkdir -p "index/$PKGNAME"

if [ ! -f "./index/$PKGNAME/size" ]
then
	FILESIZE=$(du -b "$LOCAL_URL" | awk '{ print $1 }')
	echo "$FILESIZE" > ./index/$PKGNAME/size
	echo "Recalculated FILESIZE: $FILESIZE"
fi

if [ ! -f "./index/$PKGNAME/sha512" ]
then
	SHA512=$(sha512sum "$LOCAL_URL" | awk '{ print $1 }')
	echo "$SHA512" > ./index/$PKGNAME/sha512
	echo "Recalculated SHA512: $SHA512"
fi

if [ -n "$PACKPREFIX" ]
then

	if [ ! -f "./index/$PKGNAME/qhash" ]
	then
		QHASH=$(./fteqw64 -qhash -prefix "$PACKPREFIX" "$LOCAL_URL")
		echo "$QHASH" > ./index/$PKGNAME/qhash
		echo "Recalculated QHASH: $QHASH"
	fi
	
	if [ ! -f "./index/$PKGNAME/signature" ]
	then
		SIGNATURE=$(./fteqw64 -privkey ./private-key.pem -pubkey ./public-key.pem -prefix "$PACKPREFIX" -certhost $AUTH_HOST -sign2 "$LOCAL_URL")
		echo "$SIGNATURE" > ./index/$PKGNAME/signature
		echo "Recalculated SIGNATURE: $SIGNATURE"
	fi
else
	if [ ! -f "./index/$PKGNAME/qhash" ]
	then
		QHASH=$(./fteqw64 -qhash "$LOCAL_URL")
	fi

	if [ ! -f "./index/$PKGNAME/signature" ]
	then
		SIGNATURE=$(./fteqw64 -privkey ./private-key.pem -pubkey ./public-key.pem -certhost $AUTH_HOST -sign2 "$LOCAL_URL")
		echo "$SIGNATURE" > ./index/$PKGNAME/signature
		echo "Recalculated SIGNATURE: $SIGNATURE"
	fi
fi

if [ ! -f "./index/$PKGNAME/maplist" ]
then
	MAPLIST=$(unzip -l "$LOCAL_URL" | grep \\.bsp | awk '{ print $4 }' | sed 's/maps\//\"map=/g' | sed 's/\.bsp/\"/g' | tr '\n' ' ')
	echo "$MAPLIST" > ./index/$PKGNAME/maplist
	echo "Recalculated MAPLIST: $MAPLIST"
fi
