#!/bin/bash
if [[ $WIN32 -gt 0 ]]; then
CC=i586-mingw32msvc-gcc
CFLAGS="-O2 -Wall"
PLAT=win32
BZLIB=-lbz2.win32
else
CC=gcc
CFLAGS="-O2 -Wall"
PLAT=linux
BZLIB=-lbz2
fi

if [ -e "$1" ]; then
#TODO: check $2 is not empty

size=`stat -c "%s" $1`
echo '
#include "unpack.h"
int main(int argc, char **argv) {
    unpack_and_run(argv[0], '$size', "'$2'");
    return 0;
}' > temporary_program.c
$CC $CFLAGS -static -o temporary_program temporary_program.c -L. -llinstall.$PLAT $BZLIB
cat temporary_program $1 > setup.exe
chmod +x setup.exe
rm temporary_program.c
rm temporary_program

else
    echo 'Makes self-extracting and runnning programs
(useful for creating installer programs).

Usage: pack TAR.BZ2FILE EXECNAME

This will produce a file called "setup.exe". When run, this will extract
a bzipped tarfile in a temporary directiory (/tmp in Linux, $TMP in Windows)
and the executable EXECNAME will be run automatically.'
fi
