#!/bin/sh

# This scripts takes a .decl file
# and prints the projected package entry.
# It will skip it if it lacks necessary
# meta data.

if [ ! -d ./index ]
then
	echo "run 'make index' first"
	exit 2
fi

mkdir -p pkgs

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 }')
TITLE=$(grep \"title\" "$DECL" | awk -F"\""  '{ print $4 }')
VERSION=$(grep \"ver\" "$DECL" | awk -F"\""  '{ print $4 }')
FILE=$(grep \"file\" "$DECL" | awk -F"\""  '{ print $4 }')
URL_FALLBACK=$(grep \"url\" "$DECL" | awk -F"\""  '{ print $4 }')
URL=$(grep \"url_archive\" "$DECL" | awk -F"\""  '{ print $4 }')
AUTHOR=$(grep \"author\" "$DECL" | awk -F"\""  '{ print $4 }')
DESCRIPTION=$(grep \"desc\" "$DECL" | awk -F"\""  '{ print $4 }')
WEBSITE=$(grep \"website\" "$DECL" | awk -F"\""  '{ print $4 }')
LICENSE=$(grep \"license\" "$DECL" | awk -F"\""  '{ print $4 }')
PKGLIST=$(grep \"gamename\" "$DECL" | awk -F"\""  '{ print $4 }')
PREVIEW=$(grep \"preview\" "$DECL" | awk -F"\""  '{ print $4 }')
PACKPREFIX=$(grep \"packprefix\" "$DECL" | awk -F"\""  '{ print $4 }')
DEPENDS=$(grep \"depends\" "$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 "$LICENSE" ]
then
	# If it's public, it has to at least be this.
	LICENSE="Freeware"
fi

if [ -z "$PKGLIST" ]
then
	PKGLIST="all"
fi

if [ -z "$WEBSITE" ]
then
	WEBSITE="about:blank"
fi

if [ -z "$AUTHOR" ]
then
	AUTHOR="Unknown"
fi

if [ -z "$VERSION" ]
then
	VERSION="1.0"
fi

AUTHOR_SHORT=$(echo "$AUTHOR" | sed 's/ /_/g' | sed 's/\,//g'| sed 's/-//g' | sed "s|'||g" )
VERSION_SHORT=$(echo "$VERSION" | awk '{print tolower($0)}' | sed 's/ /_/g')

if [ $TYPE = Addons ]
then
	PREFIX="addon"
	CATEGORY="Addon"
	PRIORITY=40

	if [ -z "$DESCRIPTION" ]
	then
		DESCRIPTION="Game enhancing addon."
	fi
fi

if [ $TYPE = Maps ]
then
	PREFIX="map"
	CATEGORY="Map"
	PRIORITY=30

	if [ -z "$DESCRIPTION" ]
	then
		DESCRIPTION="Open ended and/or multiplayer levels."
	fi
fi

GPREFIX=$GAMEDIR

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

	if [ -z "$DESCRIPTION" ]
	then
		DESCRIPTION="A total-conversion or modification."
	fi
fi

if [ $TYPE = Campaigns ]
then
	PREFIX="campaign"
	CATEGORY="Campaign"
	PRIORITY=30

	if [ -z "$DESCRIPTION" ]
	then
		DESCRIPTION="A single player campaign."
	fi
fi

if [ $TYPE = Patches ]
then
	PREFIX="patch"
	CATEGORY="Patch"
	PRIORITY=10

	if [ -z "$DESCRIPTION" ]
	then
		DESCRIPTION="Updates, tweaks and fixes for the game."
	fi
fi

PKGNAME=$(echo "$GPREFIX"-"$PREFIX"-"$NAME" | awk '{print tolower($0)}')
FILE="$PREFIX"-"$AUTHOR_SHORT"-"$NAME".pk4

# if we lack the hash, we probably lack everything else.
# update the index for this package
if [ ! -f "index/$PKGNAME/qhash" ]
then
	./index-decl.sh "$1"
fi

if [ -f "index/$PKGNAME/qhash" ]
then
	QHASH="$(head -n 1 index/$PKGNAME/qhash)"
else
	echo "skipping due to missing index/$PKGNAME/qhash"
	exit 1
fi

if [ -f "index/$PKGNAME/sha512" ]
then
	SHA512="$(head -n 1 index/$PKGNAME/sha512)"
else
	echo "skipping due to missing index/$PKGNAME/sha512"
	exit 1
fi

if [ -f "index/$PKGNAME/signature" ]
then
	SIGNATURE="$(head -n 1 index/$PKGNAME/signature)"
else
	echo "skipping due to missing index/$PKGNAME/signature"
	exit 1
fi

if [ -f "index/$PKGNAME/size" ]
then
	SIZE="$(head -n 1 index/$PKGNAME/size)"
else
	echo "skipping due to missing index/$PKGNAME/size"
	exit 1
fi


if [ -f "index/$PKGNAME/maplist" ]
then
	MAPLIST="$(head -n 1 index/$PKGNAME/maplist)"
fi

if [ -z "$URL" ]
then
	printf "Error: Package %s no remote url.\n" "$NAME"  >&2
 	exit 2
fi

if [ -n "$PREVIEW" ]
then
	PREVIEWSTR="\"preview=$PREVIEW\""
fi

if [ -n "$STARTMAP" ]
then
	MAPLIST="\"map=$STARTMAP\""
fi

if [ -n "$MAPLIST" ]
then
	echo "\"$PKGNAME\" \"priority=$PRIORITY\" \"category=$CATEGORY\" \"title=$TITLE\" \"ver=$VERSION\" \"gamedir=$GAMEDIR\" \"file=$FILE\" \"packprefix=$PACKPREFIX\" \"url=$URL\" \"qhash=$QHASH\" \"author=$AUTHOR\" \"desc=$DESCRIPTION\" \"website=$WEBSITE\" \"depends=$DEPENDS\" \"dlsize=$FILESIZE\" \"sha512=$SHA512\" \"license=$LICENSE\" \"sign=$AUTH_HOST:$SIGNATURE\" $MAPLIST $PREVIEWSTR" >> "pkgs/$PKGLIST.txt"
else
	echo "\"$PKGNAME\" \"priority=$PRIORITY\" \"category=$CATEGORY\" \"title=$TITLE\" \"ver=$VERSION\" \"gamedir=$GAMEDIR\" \"file=$FILE\" \"packprefix=$PACKPREFIX\" \"url=$URL\" \"qhash=$QHASH\" \"author=$AUTHOR\" \"desc=$DESCRIPTION\" \"website=$WEBSITE\" \"depends=$DEPENDS\" \"dlsize=$FILESIZE\" \"sha512=$SHA512\" \"license=$LICENSE\" \"sign=$AUTH_HOST:$SIGNATURE\" $PREVIEWSTR" >> "pkgs/$PKGLIST.txt"
fi
