#!/usr/bin/env bash
# uppdeb
# Creates .deb packages for Ultimate++
#

######################################################################
#convert a path to absolute one
rel2abs()
{
    # make sure file is specified
    if [ -z "$1" ]
    then
       echo "$0 <path>"
       return 1
    fi

    # already absolute case
    if [ "${1:0:1}" = "/" ] || [ "$PWD" = "/" ]
    then
       ABS=""
    else
       ABS="$PWD"
    fi

    # loop thru path
    IFS="/"
    for DIR in $1
    do
       if [ -n "$DIR" ]
       then
          if [ "$DIR" = ".." ]
          then
             ABS="${ABS%/*}"

          elif [ "$DIR" != "." ]
          then
             ABS="$ABS/$DIR"
          fi
       fi
    done
    IFS=":"

    echo $ABS
    return 0
} 

######################################################################
echo
echo "UPP .deb PACKAGE GENERATOR"
echo "CopyRight(c) 2009 By Massimo Del Fedele"
echo

# checks for parameters
if [ x"$1" = x -o x"$2" = x -o x"$3" = x -o "$4"x = x ]
then
	echo "uppdeb <upp base folder> <executable full path> <dest folder> <version>"
	exit 1
fi

#gets architecture (i386 or x86_64)
architecture="`uname -m`"
if [ "$architecture" = "x86_64" ]
then
	architecture="amd64"
elif [ "$architecture" = "i386" -o "$architecture" = "i686" ]
then
	architecture="i386"
else
	echo "wrong architecture '$architecture'"
	exit 1
fi

# sets up source and dest folder names
src=$(rel2abs $1)
executable=$(rel2abs $2)
dst=$(rel2abs $3)
ver="$4"

# checks if source folder exists
if [ -d $src ]
then
	true
else
	echo "source folder '$src' does not exist"
	exit 1
fi

# checks if destination folder exists
if [ -d $dst ]
then
	true
else
	echo "dest folder '$dst' does not exist"
	exit 1
fi

# sets up subfolders
uppsrc="uppsrc"
examples="examples"
tutorial="tutorial"
reference="reference"
bazaar="bazaar"

subfolders="$uppsrc $examples $tutorial $reference $bazaar"

# checks if upp subfolder are in source folder
for name in $subfolders
do
	if [ ! -d $src/$name ]
	then
		echo "bad source folder '$src'"
		echo "missing '$src/$name' subfolder"
		exit 1
	fi
done

# checks for executable
if [ -f $executable ]
then
	true
else
	echo "missing executable file... please compile before packaging"
	exit 1
fi

# removes dest file if exist
if [ -f $dst ]
then
	rm $dst
fi

# gets basename from ide executable full path
# it's used to set up proper ide exec name and launcher
# in package
file=${executable##*/}
base=${file%%.*}

# depending on base name, gets Icon name and Package name
if [[ $base =~ [sS][vV][nN] ]]
then
	iconname="TheIDE-SVN"
	packagename="upp-svn"
elif [[ $base =~ [dD][eE][vV] ]]
then
	iconname="TheIDE-DEV"
	packagename="upp-dev"
elif [[ $base =~ [bB][eE][tT][aA] ]]
then
	iconname="TheIDE-BETA"
	packagename="upp-beta"
else
	iconname="TheIDE"
	packagename="upp"
fi

#tmp="/tmp/uppdeb"
#rm -rf $tmp
tmp="`mktemp -t -d uppdeb.XXXXXXX`"
#mkdir $tmp

echo "Building package '$packagename' from folder '$src'"
echo "Dest package path is '$dst'"
echo "IDE executable is '$executable'"

#creates package tree
mkdir $tmp/DEBIAN
mkdir $tmp/usr
mkdir $tmp/usr/bin
mkdir $tmp/usr/sbin
mkdir $tmp/usr/share
mkdir $tmp/usr/share/applications
mkdir $tmp/usr/share/doc
mkdir $tmp/usr/share/doc/$packagename
mkdir $tmp/usr/share/pixmaps
mkdir $tmp/usr/share/$packagename

#creates theide.png file (actually, $base.png)
shopt -s xpg_echo
echo "\
\x89\x50\x4e\x47\x0d\x0a\x1a\x0a\x00\x00\x00\x0d\x49\x48\x44\x52\
\x00\x00\x00\x30\x00\x00\x00\x30\x08\x06\x00\x00\x00\x57\x02\xf9\
\x87\x00\x00\x00\x06\x62\x4b\x47\x44\x00\xff\x00\xff\x00\xff\xa0\
\xbd\xa7\x93\x00\x00\x00\x09\x70\x48\x59\x73\x00\x00\x0b\x13\x00\
\x00\x0b\x13\x01\x00\x9a\x9c\x18\x00\x00\x00\x07\x74\x49\x4d\x45\
\x07\xd6\x0b\x1a\x0b\x0d\x24\xc5\x3f\xa6\x05\x00\x00\x08\x51\x49\
\x44\x41\x54\x68\xde\xed\x99\xcb\x6f\x1b\xd7\x15\xc6\x7f\x77\x1e\
\xe4\x90\x94\x25\xd7\x2f\x24\x41\x51\x64\x23\xe4\x81\x28\x36\xe4\
\x56\x90\x03\xa7\x28\x10\x08\x05\x02\x74\x91\x5d\xff\x84\x6e\xea\
\xa0\x05\xb4\x6b\x37\x6d\xa5\xa5\xb3\xb1\x90\xbf\xa1\xf1\xc6\x8b\
\xc6\x5d\x15\x30\xdc\x4d\xd0\xa2\x89\x8d\x00\x8e\x64\x14\x68\x1d\
\x40\x76\x40\x3d\x39\x22\x39\x9c\xc7\x9d\xd3\xc5\x3c\x38\xa4\x86\
\x12\x29\xa9\xc9\xa2\xbd\x02\x21\x72\xc8\x39\xf7\x9c\x7b\xbe\xfb\
\x9d\xef\x9e\x81\xff\x8f\xef\x76\xa8\x33\xb2\x63\x00\x53\x40\xa5\
\x70\x4d\x0a\xf6\x47\xbd\x07\x88\x81\x03\x20\x3c\xc9\xc4\xd6\x19\
\x05\x30\x05\xfc\x06\x58\x4c\x1d\x91\x31\xef\xb3\x81\x3d\xe0\x77\
\xc0\xe7\xdf\x65\x00\x26\xb0\x00\xbc\x7b\x7c\xce\x15\x88\xe4\xe9\
\x17\xd8\x07\xce\x9f\x74\x62\xeb\x0c\x20\x11\x01\x17\xfa\xfe\x29\
\x1c\xc7\xc1\x34\x4d\x24\x75\x14\x95\xfc\xd4\xf0\x3c\x0c\xad\x11\
\xa0\x03\xe8\x3e\x84\xa6\x81\x99\xd4\x1f\x35\x09\xcc\x46\xed\x81\
\xe9\x09\x20\xa1\x81\x2a\x30\x07\x5c\xa8\xd5\x6a\x2c\x2f\x2f\x33\
\x3f\x3f\x8f\xeb\xba\x28\x11\xb4\xe3\x60\xf8\x3e\xaf\xac\xac\xf0\
\xbd\x8d\x0d\xba\xc0\x2f\x81\x47\xc9\xfd\x21\xf0\x25\xb0\x95\x66\
\x52\x9d\x05\xcc\x2e\x01\x0f\x53\xc7\x27\x79\xc5\x53\x53\x53\x72\
\xef\xde\x3d\xe9\xb4\xdb\xf2\xec\xeb\xaf\xe5\xdf\xdf\x7c\x23\xff\
\xec\x76\xe5\x5f\xcd\xa6\xe8\x1b\x37\x44\x40\x42\x90\xf7\xc6\xb4\
\xa9\x94\x12\x12\x98\xfd\x74\xd2\x3d\x10\x66\x90\xa8\xd7\xeb\x58\
\x96\x45\x1c\xc7\x25\x90\x4e\x16\xac\xd3\xe9\xa0\xb5\x56\x22\x82\
\xeb\xba\x6c\xef\xee\xb2\xb3\xb3\x93\x64\xc0\x75\x31\x3c\x8f\x4b\
\x41\xc0\x14\xe0\x01\x41\xe1\xfe\xcc\xbe\x88\xa0\xb5\xce\xaf\x7b\
\x9e\x97\x7d\x8e\xfa\x88\x1b\x2f\x80\x6c\x05\xa8\xd7\xeb\xdc\xbe\
\x7d\x9b\x85\x85\x05\x0e\x0e\x0e\x0e\xfd\xd0\x71\x1c\x7a\xbd\x1e\
\xb7\x6e\xdd\xe2\xd1\xa3\x04\x14\x4a\x04\xa9\x54\x50\x22\x7c\x7f\
\x65\x85\xfa\x93\x27\xc4\xf5\x3a\xb5\x8d\x8d\x43\x1b\x28\xb3\xbf\
\xb8\xb8\x88\xeb\xba\xbc\x78\xf1\x82\x28\x8a\x88\xa2\x88\xd5\xd5\
\x55\xd6\xd7\xd7\x07\xfc\x19\x37\x80\x1c\x87\x96\x65\xb1\xb0\xb0\
\xc0\xb5\x6b\xd7\x46\xa6\x2a\x8a\x84\x8b\x17\xaf\xe4\x1b\x56\x3b\
\x0e\xd1\xf4\x34\xda\x75\xa9\x3f\x79\x42\xfd\xab\xaf\x86\x4c\xf7\
\x7d\xb1\x2c\x9b\x85\x85\x9b\xbc\xfd\xf6\x9b\x00\xec\xed\xb5\xd0\
\x3a\x20\x0c\x23\xd6\xd6\xd6\x8a\x37\xa9\x13\xd1\x68\x1c\xc7\x85\
\x95\x17\xa0\x9b\x66\x34\xb3\x57\xc5\xf3\x7c\x82\xa0\xd7\xa7\x30\
\xdf\xc7\x6c\xb7\x31\x3c\x8f\xb8\x5e\xef\xbb\x50\x07\x6c\x49\x38\
\x25\xa5\xa1\xc4\xfe\x0e\xa0\x89\xe3\x2e\xcd\xe6\x16\x5a\x87\x44\
\x51\x48\x18\x86\x27\xa6\x51\x19\xc6\x78\x32\xba\xc0\xaf\x81\xbf\
\xa5\x44\x95\x30\xae\x48\x8c\x52\x8f\x73\xaa\x7c\x65\x65\x85\x1f\
\x7c\xfc\x31\x97\x82\x20\x87\x0d\x75\xe0\x36\x70\x13\xd8\x01\x6e\
\x25\x34\xa4\x54\x07\xf8\x10\x98\x46\x6b\xcd\xd3\xa7\xef\xd3\x6c\
\xbe\x86\x69\xba\xb4\xdb\xed\x43\x90\x3e\x65\x21\x8b\x52\xe7\x1f\
\x8d\x2c\x1f\x4a\x6b\x6a\x1b\x1b\x79\x21\xc9\x97\x3f\x16\xf8\x21\
\xf0\x66\x6a\xe6\x62\x96\x16\x0d\x7c\x91\x66\x5b\xd1\x6a\xbd\x41\
\xb3\xf9\x12\xb6\xbd\x73\xaa\x0c\xe4\xcb\x9e\x17\xa3\xfc\xf2\xb9\
\xa3\x00\x87\x14\xe9\x22\x83\x4d\x2c\x09\x9b\x67\x0b\x9a\xd3\x90\
\x50\x34\xaf\x54\x05\xcb\x6a\x50\xa9\xd8\xd8\xb6\x8d\x61\x18\xa7\
\xdf\x03\xa7\xd2\x7f\x75\xe0\x23\xe0\x7a\xea\xfc\xd5\x11\x72\xae\
\x88\x5d\x91\xf4\x35\xb0\x78\x32\x69\x06\x72\xde\xcd\xa4\x41\xb6\
\x61\x13\x95\x31\x38\xaa\x55\xc8\x16\x4b\x31\x24\x00\xae\x83\xcc\
\x0f\x5e\x2f\x5a\x51\x0a\x32\xf3\x96\xa5\xa8\xd5\x2a\x38\x4e\x0d\
\xdb\xae\x16\x33\x40\xa1\x74\x8c\x0c\x20\x83\xad\x99\x6a\x9b\x6a\
\xf6\x45\xaf\xd7\x23\x8a\x04\xcf\xf3\x11\x89\x0f\x39\xdf\xeb\x41\
\x5e\xe3\x72\xd8\x90\xc3\x46\x15\xf8\xcb\x48\x2b\x64\xd1\x4a\xaf\
\x97\xe8\xbb\x6e\x17\x82\x20\x24\x0c\x7d\xe2\x38\x28\x66\xc0\x48\
\x7d\x3a\x9f\xbe\xcf\x75\x91\x2a\xd1\x3f\x0b\xe9\xe7\x39\xe0\x82\
\x69\x9a\xcc\xcd\xcd\x71\xf1\xe2\x15\x82\xa0\x97\xb2\x4d\xab\x1f\
\xb5\x91\x38\xff\xf8\x31\xb4\x5a\x30\xdd\x80\x3f\x7d\x04\xef\x16\
\x61\x33\x93\xb0\xe6\xaf\x52\x31\xd3\x70\xe1\xf1\xcf\xa0\xf5\x10\
\x4c\x13\xe6\xe6\xe0\xf2\x65\xf0\x7d\x83\x66\xf3\x65\x7c\x7f\x0a\
\xc3\x88\xd8\xdc\xdc\xa4\xd7\xeb\x15\xf5\xd2\x3e\xe0\x02\xbf\x2f\
\xd3\x45\x65\xfa\x27\x9e\x4c\x0b\x29\x99\x9e\x41\x1e\x7e\x51\x80\
\xb2\x20\x12\x23\x5d\x41\xae\x0b\xc9\x5f\x0b\xe1\xc7\x88\x09\xa2\
\x26\xb0\x5f\xa6\x8b\xac\x51\xfa\x27\x95\xc4\x6a\x90\x85\x18\xa9\
\x85\x72\xdd\x92\x09\xdf\x22\x6c\x54\x62\xb8\x71\x88\xb3\x92\x4d\
\x50\x1b\x96\xdf\x47\xd9\x1f\xd2\x45\x56\x59\x01\x73\x1c\x67\x40\
\x12\x1f\x35\x1c\xc7\xc1\xf7\x7d\x56\x56\x56\x58\x5f\x5f\x1f\xa0\
\x8b\x6e\x11\x36\xc0\xe3\x92\xc9\x6a\xc7\xcc\x35\x6c\xff\xa8\x4d\
\x9c\xcf\x6d\x9a\x26\xf3\xf3\xf3\x2c\x2d\x2d\xb1\xbd\xbd\x3d\x54\
\x8d\x07\x29\xaf\xd1\x68\xe0\x79\x1e\x77\xee\xdc\xe9\xb3\x4a\x61\
\xe7\x7d\x0e\xfc\x63\xa4\x12\x3a\x7a\xae\x32\xfb\xc3\x55\xd9\x1a\
\x55\xbc\x5c\xd7\x65\x7b\x7b\x3b\x91\xc4\x47\x04\xd0\xeb\xf5\xe8\
\xf5\x7a\x03\x55\xb3\x97\xce\x30\x08\x9b\x84\x9e\x04\x9b\xa2\x18\
\x3a\x6a\xae\x11\xf6\x07\x8a\x5a\x69\x06\x8a\xf8\x1b\xe5\xfc\xa8\
\xd1\x49\x65\xce\x95\x34\x90\xc7\x03\x55\xad\x44\x0c\x4d\x3e\xd7\
\xc8\x0c\x9c\xc9\xd0\x25\x4a\x29\x81\x4d\x4c\x89\x18\x3a\xf5\xb0\
\xce\xbc\x47\x24\xc3\xae\xd7\x53\xe7\x4b\xc5\xd0\x49\x7b\x59\xe3\
\x41\x28\x23\xf2\x91\xbe\x1e\xf3\xfd\x24\x62\xa8\xcc\xd6\x08\xfb\
\xc7\x43\x28\xab\x03\x8d\x46\x23\xab\x84\x23\x03\x98\x9a\x9a\xc2\
\x34\xcd\xbe\x6e\x99\x50\x0c\x1d\x35\x57\xa9\xfd\x71\x21\xe4\xfb\
\x3e\x9e\xe7\x1d\x19\x40\x46\x83\x9e\xe7\xf5\x57\x4a\x26\x17\x43\
\x47\xcd\x75\xc8\xfe\x10\x84\x8a\x4e\x5f\x04\xfe\x08\xbc\x67\x9a\
\x26\xb3\xb3\xb3\xcc\xcc\xcc\x1c\x7b\xa8\x30\x0c\x03\x11\x61\x63\
\x63\x23\x39\x41\x4d\x28\x86\x8e\x9b\xeb\x90\x7d\xd8\x06\x7e\x0e\
\xfc\xa5\x2c\x03\xb6\x52\x0a\xad\x75\x69\xd5\x1b\xa7\xc3\x1b\x63\
\x01\x3f\x02\xae\x0d\xc2\xd6\x50\x87\xaa\x9a\x32\x21\x8e\xc7\x9f\
\x4b\x29\x85\x88\x58\xa9\x62\x3e\x04\xa1\x18\xd8\x13\x91\xfd\xf4\
\xfd\xb9\x2c\xa0\xa4\x6f\x63\xa7\x7d\xa1\xce\x40\x8b\x26\xa3\xee\
\x4e\x07\x62\x5d\xec\x02\x1e\x2f\x86\x24\x4e\x56\xad\xd1\x00\xcb\
\x4a\x24\x75\x10\x54\x10\x31\x51\x4a\x08\x82\x20\xeb\x45\xc5\x40\
\x5b\x44\x34\xb0\x0b\xf8\x65\x01\x1c\xa4\xed\xbb\xf3\xa9\xb4\xfe\
\x2d\x30\xdf\xef\x0b\xdd\x4c\xbb\x07\x1f\xe6\x67\xd8\x44\xab\x24\
\x7a\xfe\xd6\x2d\x78\xf4\x28\xdb\x03\xe3\x8b\xa1\x7a\x03\x6e\xdf\
\x86\x77\xde\x81\xfd\xfd\x0a\x0f\x1e\x7c\x40\xb3\xf9\x3a\xa6\xd9\
\xe2\xee\xdd\xbb\x6c\x6e\x6e\x92\x62\xf1\x0f\xc0\xdf\xd3\x65\xf9\
\xb2\x2c\x80\xb0\xa0\xb1\x67\x80\x5f\x64\x7d\xa1\xc5\xc5\xc5\xb4\
\x6f\xa3\x0b\xdd\x88\x22\x5b\x24\x7a\x3e\x07\xe2\x51\x62\x28\xff\
\x22\x3b\x85\x25\xce\xbf\xf5\x56\x12\x51\xa7\xf3\x2a\xcf\x9e\x5d\
\xc7\xb2\x9a\xdc\xbf\x7f\xbf\x78\x1a\xfb\x0c\xf8\xeb\x70\x7d\xb4\
\x8e\x28\x70\x56\x51\x17\x25\x5d\x83\x2e\x5a\x6b\xe2\x58\xa1\x54\
\x25\x3f\x06\x76\xbb\xe0\xfb\x41\x9f\x5a\x4a\xc4\x50\x72\x74\xd4\
\x58\x56\xb2\x29\x3d\x0f\xb4\x4e\x82\xdf\xdf\xaf\x00\x82\xef\x9b\
\xec\xed\x79\x1c\x1c\xec\x63\x59\x6d\xa2\x28\x2a\xfa\x54\x29\x2b\
\x99\xc7\x4a\x09\xad\x35\x2f\x5e\xbc\x60\x6f\xaf\x45\xb3\xb9\xc5\
\xd3\xa7\xef\xd3\x6a\xbd\x81\x65\x35\x10\x11\x6a\xb5\x0a\x41\x10\
\xd2\x6c\xde\x05\x36\x47\x8a\x21\xc7\x89\x59\x5e\xde\xe4\xc6\x0d\
\xd8\xda\x0a\x59\x5d\xed\xb2\xbe\x9e\x60\xfe\xc1\x83\x0f\xe8\x74\
\x5e\x65\x6f\xcf\x63\x7b\xfb\x65\xaa\xd5\x2e\x5a\x9f\xee\xf9\x80\
\x1a\x6c\x1d\x46\x68\x1d\xa0\x75\x48\xb3\xf9\x1a\xcd\xe6\x4b\x54\
\x2a\x36\x22\x82\xe3\xd4\x08\x43\x1f\xdf\xff\xf3\x68\x31\x44\x72\
\xec\xbc\x7a\xb5\xc3\xd2\x52\xc0\xf3\xe7\x9a\xb5\xb5\x28\x85\x9f\
\x49\xb3\xf9\x3a\xcf\x9e\x5d\xe7\xe0\x60\x9f\x6a\xb5\x4b\xa3\x61\
\x11\x04\xd5\xb3\x69\xab\x28\xa5\x88\xa2\x88\x30\x8c\x88\xa2\x10\
\xd3\x74\xb1\xed\x1d\x6c\xdb\x46\x04\x6c\xbb\x4a\x1c\x07\x18\x46\
\x54\xfa\x30\xc6\x71\x62\xe2\x18\x2c\x4b\x70\x5d\x93\xbd\x3d\x9b\
\xdd\x5d\x45\x18\x1a\xe9\x6f\x04\xcb\x6a\x61\x59\x4d\x2c\xab\x8d\
\xd6\x10\x04\x55\x7c\x3f\xe0\xb8\xd3\xe0\x58\xad\x45\xcf\xf3\x58\
\x5d\x5d\x65\x6d\x6d\x8d\x30\x0c\x69\xb7\xdb\x84\x61\x98\x17\x98\
\xec\x7f\xca\x16\x03\x92\xca\x71\x34\xcb\xcb\x9b\x5c\xbd\xda\xc1\
\x75\x4d\x66\x67\x3d\x5c\xb7\x4a\x51\x35\x07\x41\xc0\x27\x9f\xdc\
\xe5\xd3\x4f\xef\xe7\x98\x3f\x6c\xf3\x64\xdd\xe9\x89\x8b\x5a\xf2\
\xf8\xab\x4f\x43\x96\x65\x70\xe3\x06\x2c\x2d\x05\xec\xee\xda\x1c\
\x1c\x54\xa9\x54\xce\x51\xaf\x7b\x39\x3c\xe2\x38\x2e\x3a\x3a\x56\
\xf1\x1a\x27\x80\xac\xa8\xb5\x52\xf1\x7e\x9c\x80\x36\x80\x73\x92\
\x1c\xb7\xd2\xdd\xab\x11\xe9\xb2\xb5\x15\xf2\xfc\xb9\x66\x77\x57\
\xa1\x14\xd4\xeb\xde\xb0\xb6\x89\x53\x9e\x0f\xca\xf6\x9f\x88\x98\
\xe9\x09\xc8\x9f\xa4\x4f\x68\xa7\x7d\xa1\xcb\xa3\x9e\x8c\x0c\x75\
\xcc\x2e\x64\x85\x2f\x59\xa8\x39\xe0\x12\xa6\x19\x30\x3b\xdb\x65\
\x66\x26\xca\x31\x5f\xa2\x6d\xdc\xb4\x48\x7d\x96\x52\xa5\x2a\x29\
\x1a\x7e\x5a\xbc\x76\xff\x5b\x0f\xcc\xcf\x27\xe2\x4a\x15\x7a\x38\
\x63\xf7\x79\x76\x81\x9f\x9c\xb4\x31\x7b\x56\x47\x4a\x23\x59\xc9\
\x3e\xe4\xc6\x20\x90\x0c\x1e\xbb\x13\x3c\x18\x97\x53\xb4\x9a\x8f\
\x7d\xe2\x3e\x2e\xe4\xbe\x75\x78\x7c\x5b\x43\xf1\xbf\x38\xfe\x03\
\xb3\x05\xe4\x47\x8e\x1e\xd0\xe3\x00\x00\x00\x00\x49\x45\x4e\x44\
\xae\x42\x60\x82" > $tmp/usr/share/pixmaps/$base.png

#copies source files to tmp folder
for name in $subfolders
do
#	rsync -av --exclude '.svn' $src/$name $tmp/usr/share/$packagename > /dev/null
	rsync -rl --chmod=ugo+r,Dugo+x --exclude '.svn' $src/$name $tmp/usr/share/$packagename > /dev/null
done

#copies the executable file
cp -a $executable $tmp/usr/bin/$base

#creates launcher
cat > $tmp/usr/share/applications/$base.desktop << EOF
[Desktop Entry]
Version=1.0
Encoding=UTF-8
EOF
echo "Name=$iconname" >> $tmp/usr/share/applications/$base.desktop
echo "GenericName=$iconname" >> $tmp/usr/share/applications/$base.desktop
cat >> $tmp/usr/share/applications/$base.desktop << EOF
Comment=IDE for cross-platform C++ development
MimeType=application/x-upp;
EOF
echo "Exec=$base" >> $tmp/usr/share/applications/$base.desktop
echo "Icon=$base.png" >> $tmp/usr/share/applications/$base.desktop
cat >> $tmp/usr/share/applications/$base.desktop << EOF
Terminal=false
Type=Application
Categories=Development;IDE;
StartupNotify=false
EOF
echo "GenericName[de_DE]=$iconname" >> $tmp/usr/share/applications/$base.desktop
echo "GenericName[it_IT]=$iconname" >> $tmp/usr/share/applications/$base.desktop

#creates md5sums file
md5tmp="`mktemp -t -d uppdebMD5.XXXXXXX`"
cd $tmp
find ./ -type f -exec md5sum {} \; | sed s/.[/]// | sed /[.]svn/d >> $md5tmp/md5sums
mv $md5tmp/md5sums $tmp/DEBIAN
rmdir $md5tmp

#gets the installed size (approx...)
#sigh... there must be a nicer way
szline="`du -s $tmp/`"
for sz in $szline
do
	size=$sz
	break
done

# removed... should not be necessary anymore
#Conflicts: ultimate++, upp-dev

#creates control file
echo "Package: $packagename" > $tmp/DEBIAN/control 
echo "Version: $ver" >> $tmp/DEBIAN/control 
cat >> $tmp/DEBIAN/control << EOF
Section: devel
Priority: optional
EOF
echo "Architecture: $architecture" >> $tmp/DEBIAN/control
cat >> $tmp/DEBIAN/control << EOF
Depends: g++, libx11-dev (>= 1.0.0), libxft-dev (>= 2.1.8), libpng12-dev (>= 1.2.8), libexpat1-dev (>= 1.95.8), libgtk2.0-dev (>= 2.8.17), libnotify-dev
EOF
echo "Installed-Size: $size" >> $tmp/DEBIAN/control 
echo "Maintainer: $MAINTAINER <$EMAIL>" >> $tmp/DEBIAN/control 
cat >> $tmp/DEBIAN/control << EOF
Description: C++ library for cross-platform development with IDE
 Ultimate++ is a radical and innovative GUI toolkit whose number one priority
 is programmer productivity. C++ is a great programming language but
 C++ programmers are sometimes hampered by the lack of effective libraries.
 U++ libraries enable genuine productivity gains with shorter development
 times and greatly reduced application source code size.
EOF

cat > $tmp/DEBIAN/postinst << _EOF_
#!/bin/bash

cver=
sver=9
	until [ \$sver -lt 0 ]; do
    	if [ \$(which g++-4.\$sver) ]; then
    		if [ \$sver = 2 ]; then # buggy compiler
    			sver=\$((sver - 1))
    			continue
    		fi
    		cver="4.\$sver"
    		#creates GCC.bm file
			cat > /usr/share/$packagename/GCC.bm <<- EOF
				BUILDER = "GCC";
				COMPILER = "g++-\$cver";
				DEBUG_INFO = "2";
				DEBUG_BLITZ = "1";
				DEBUG_LINKMODE = "1";
				DEBUG_OPTIONS = "-O0";
				DEBUG_FLAGS = "";
				RELEASE_BLITZ = "0";
				RELEASE_LINKMODE = "1";
				RELEASE_OPTIONS = "-O3 -ffunction-sections -fdata-sections";
				RELEASE_SIZE_OPTIONS = "-Os -finline-limit=20 -ffunction-sections -fdata-sections";
				RELEASE_FLAGS = "";
				RELEASE_LINK = "-Wl,--gc-sections";
				DEBUGGER = "gdb";
				PATH = "";
				INCLUDE = "/usr/include/freetype2;/usr/include/gtk-3.0;/usr/include/glib-2.0;/usr/lib/glib-2.0/include;/usr/lib/gtk-3.0/include;/usr/include/cairo;/usr/include/pango-1.0;/usr/include/atk-1.0;/usr/X11R6/include;/usr/X11R6/include/freetype2;/usr/X11R6/include/gtk-3.0;/usr/X11R6/include/glib-2.0;/usr/X11R6/lib/glib-2.0/include;/usr/X11R6/lib/gtk-3.0/include;/usr/X11R6/include/cairo;/usr/X11R6/include/pango-1.0;/usr/X11R6/include/atk-1.0";
				LIB = "/usr/X11R6/lib;/usr/lib";
				REMOTE_HOST = "";
				REMOTE_OS = "";
				REMOTE_TRANSFER = "";
				REMOTE_MAP = "";
				LINKMODE_LOCK = "0";
			EOF
			chown \$USER:\$(id -g \$USER) /usr/share/$packagename/GCC.bm
			chmod 644 /usr/share/$packagename/GCC.bm
			break
    	fi
    	sver=\$((sver - 1))
	done

_EOF_

chmod 0775 $tmp/DEBIAN/postinst

cat > $tmp/DEBIAN/prerm << _EOF_
#!/bin/bash
# remove GCC*.bm files
rm /usr/share/$packagename/GCC*.bm
_EOF_

chmod 0775 $tmp/DEBIAN/prerm

#creates final .deb package
dpkg-deb -b $tmp $dst
result=$?

#wipes temporary folder
rm -rf $tmp
