#!/bin/bash
echo
echo "Packing LÖVE to go v1.2"
echo

arch="i386"

if [ "`uname -m | grep -c -G x86_64`" != 0 ]; then
	arch="amd64"
elif [ "`uname -m | grep -c -G ppc`" != 0 ]; then
	arch="ppc"
fi

if [ -z "$2" ]; then
	echo "Syntax:  ./make-package <package> <version>"
	echo "Example: ./make-package deb 0.3.0-1"
	echo "All files will be created in this folder."
	echo
	exit 0
fi
case "$1" in
	deb )
		strip ../../src/love
		mkdir deb
		mkdir deb/DEBIAN
		cat debian | sed "s/%VERSION%/$2/" | sed "s/%ARCHITECTURE%/$arch/g" > deb/DEBIAN/tmp
		cat deb/DEBIAN/tmp
		sed "s/%INSTALLSIZE%/`ls -l ../../src/love | awk '{ sum += $5 } END { printf "%.2f", sum / 1000 }'`/" < deb/DEBIAN/tmp > deb/DEBIAN/control
		rm deb/DEBIAN/tmp
		cp postinst deb/DEBIAN/postinst
		cp postrm deb/DEBIAN/postrm
		mkdir deb/usr
		mkdir deb/usr/bin
		cp ../../src/love deb/usr/bin/love
		mkdir deb/usr/share
		mkdir deb/usr/share/mime
		mkdir deb/usr/share/mime/packages
		cp love.xml deb/usr/share/mime/packages
		mkdir deb/usr/share/icons
		mkdir deb/usr/share/icons/gnome
		mkdir deb/usr/share/icons/gnome/scalable
		mkdir deb/usr/share/icons/gnome/scalable/apps
		cp app.svg deb/usr/share/icons/gnome/scalable/apps/love.svg
		mkdir deb/usr/share/icons/gnome/scalable/mimetypes
		cp game.svg deb/usr/share/icons/gnome/scalable/mimetypes/gnome-mime-application-x-love-game.svg
		mkdir deb/usr/share/applications
		cp love.desktop deb/usr/share/applications
		dpkg -b deb love-$2.deb && \
		echo "  * love-$2.deb created"
		rm -rf deb
		;;
	src )
		cd ../../../
		tar -czf love/platform/unix/love-$2-src.tar.gz --exclude=*.o --exclude=.* love/demos/* love/*.txt love/src/ && \
		echo "  * love-$2-src.tar.gz created"
		tar -cjf love/platform/unix/love-$2-src.tar.bz2 --exclude=*.o --exclude=.* love/demos/* love/*.txt love/src/ && \
		echo "  * love-$2-src.tar.bz2 created"
		zip -9q love/platform/unix/love-$2-src.zip love/*.txt love/demos/* `find love/src/* | grep -viE *.o` && \
		echo "  * love-$2-src.zip created"
		;;
	* )
		echo "Unknown package type: $1"
		echo "Valid types: deb src"
esac
echo
