#!/bin/bash
PKG="hello"
VER="0.0.1"
SECTION="user/hidden"
PRIORITY="Optional"
ARCH="amd64"
MAINTAINER="Luca Baldesi <luca.baldesi@unitn.it>"
DESC="Optional description is missing"
BUILD_DIRS="usr/"
DEPS=""
BUILD_DEPS="bc"
# more conf fields are available, check:
# http://www.debian.org/doc/debian-policy/ch-controlfields.html

WD="$PWD"

# propose build dependencies
echo "Please install the following development files:"
echo "apt install $BUILD_DEPS -o APT::Install-Recommends=0 -o APT::Install-Suggests=0"
read -n1 -r -p "Press \"q\" to exit or anything else to continue." key
if [ "$key" = 'q' ]; then
    echo ""; exit 0;
fi

# build our software
wget -O hello-2.7.tar.gz "http://ftp.gnu.org/gnu/hello/hello-2.7.tar.gz"
tar xzf hello-2.7.tar.gz
cd hello-2.7 
./configure && make

# create the destination folder tree
cd $WD
mkdir -p usr/bin/
cp hello-2.7/src/hello usr/bin/

# configuration part
echo 2.0 > debian-binary
echo '#!/bin/bash' > preinst
echo '#!/bin/bash' > postinst
echo '#!/bin/bash' > prerm
echo '#!/bin/bash' > postrm

cat > control << EOL
Package: $PKG
Version: $VER
Section: $SECTION
Priority: $PRIORITY
Architecture: $ARCH
Installed-Size: `du -ks $BUILD_DIRS | cut -f 1 | paste -sd+ - | bc`
Maintainer: $MAINTAINER
Depends: $DEPS
Description: $DESC
EOL

# pack everything together
tar czf control.tar.gz control preinst postinst prerm postrm
tar czf data.tar.gz $BUILD_DIRS
ar -r "$PKG.deb" debian-binary control.tar.gz data.tar.gz

# cleanup
rm -rf control control.tar.gz preinst postinst prerm postrm data.tar.gz