#!bash
# vim:ff=unix:enc=utf8:ts=3:sw=3:et

# src2pkg-ng - package creation toolkit
# Copyright (C) 2005-2009 Gilbert Ashley
# Copyright (C) 2009 Timothy Goya

# src2pkg-ng is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License, version 2 as 
# published by the Free Software Foundation

# src2pkg-ng is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with src2pkg-ng.  If not, see <http://www.gnu.org/licenses/>.

if [[ -z "${PKG_FORMAT:-""}" && -f "/etc/debian_version" ]] ; then
   PKG_FORMAT="debian"
fi
if [[ -z "${PKG_POLICY:-""}" && "${PKG_FORMAT:-""}" == "debian" ]] ; then
   PKG_POLICY="LSB"
fi
if [[ -z "${PKG_COMPRESSION:-""}" && "${PKG_FORMAT:-""}" == "debian" ]] ; then
   PKG_COMPRESSION="gzip"
fi

make_pkg_debian() {
   declare PKG_DIR="$1"
   declare PKG_NAME="$1"
   declare ARCH
   if [[ "$ARCH" == "noarch" ]] ; then
      ARCH=all
   fi
   if [[ "$PKG_POLICY" != "LSB" ]] ; then
      tprintf $"@warning{Warning!}: Package structure policy not 'LSB'.\n"
      echo $"Package may not properly comply with Debian LSB conventions"
   fi
   printf "2.0\n" > "$PKG_DIR/metadata/debian-binary"
   mkdir -p "$PKG_DIR/metadata/control"
   (cd "$PKG_DIR" && md5sum $(find * -type f)) > "$PKG_DIR/metadata/control/md5sums"
   make_debian_control "$PKG_DIR" "$PKG_DIR/metadata/control/control"
   if ! tar --owner=root --group=root -c -z -C "$PKG_DIR/metadata/control" -f "$PKG_DIR/metadata/control.tar.gz" . ; then
      tprintf $"@error{Error!}: Creating Debian control tarball failed\n"
      cleanup 1
   fi
   declare DATA="$PKG_DIR/data.tar"
   if ! tar --owner=root --group=root -c -C "$PKG_DIR" -f "$DATA" . --exclude "data.tar" --exclude "metadata" ; then
      tprintf $"@error{Error!}: Creating Debian data tarball failed\n"
      cleanup 1
   fi
   if [[ "$PKG_COMPRESSION" ]] ; then
      $PKG_COMPRESSION "$PKG_DIR/data.tar"
      DATA="$DATA".*
   fi
   declare PKG="$PKG_DEST_DIR/${PKG_NAME}_$VERSION-${BUILD}_$ARCH.deb"
   rm -f "$PKG"
   ar rc "$PKG" "$PKG_DIR/metadata/debian-binary" "$PKG_DIR/metadata/control.tar.gz" "$DATA"
   tprintf $"@summary{Package Creation} - @success{Successful!} Package location\n"
   echo "$PKG"
}

make_debian_control() {
   declare PKG_DIR="$1"
   declare CONTROL_FILE="$1"
   cat > "$CONTROL_FILE" << EOF
Package: $PKG_NAME
Version: $VERSION-$BUILD
Architecture: $ARCH
Installed-Size $(du -sk "$PKG_DIR" | cut -f 1)
Maintainer: $PACKAGER
Description: $BRIEF
EOF
   while [[ -z "$DESC" ]] ; do
      declare CHUNK=${DESC:0:79}
      if (( ${#CHUNK} == 79 )) ; then
         CHUNK=${CHUNK% *}
      fi
      echo " $CHUNK" >> "$CONTROL_FILE"
      DESC=${DESC:${#CHUNK}}
      DESC=${DESC#$ *}
   done
   if [[ -z "$HOMEPAGE" ]] ; then
      echo "Homepage: $HOMEPAGE" >> "$CONTROL_FILE"
   fi
}
