#!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/slackware-version" ]] ; then
   PKG_FORMAT="slackware"
fi
if [[ -z "${PKG_POLICY:-""}" && "${PKG_FORMAT:-""}" == "slackware" ]] ; then
   PKG_POLICY="slackware"
fi
if [[ -z "${PKG_COMPRESSION:-""}" && "${PKG_FORMAT:-""}" == "slackware" ]] ; then
   if grep -q "txz" /sbin/installpkg ; then
      PKG_COMPRESSION="xz"
   elif grep -q "tlz" /sbin/installpkg ; then
      PKG_COMPRESSION="lzma"
   elif grep -q "tbz" /sbin/installpkg ; then
      PKG_COMPRESSION="bzip2"
   else
      PKG_COMPRESSION="gzip"
   fi
fi

set_pkg_policy_slackware() {
   BIN_DIR="$PRE_FIX/bin"
   SBIN_DIR="$PRE_FIX/sbin"
   LIBEXEC_DIR="$PRE_FIX/libexec"
   SYSCONF_DIR="etc"
   SHAREDSTATE_DIR="var/log"
   LOCALSTATE_DIR="var"
   LIB_DIR="$PRE_FIX/lib$LIBSUFFIX"
   INCLUDE_DIR="$PRE_FIX/include"
   DATA_DIR="$PRE_FIX/share"
   LOCALE_DIR="$DATA_DIR/locale"
   GAMESBIN_DIR="$BIN_DIR"
   GAMESDATA_DIR="$DATA_DIR"
   DOC_DIR="$PRE_FIX/doc/$NAME-$VERSION"
   MAN_DIR="$PRE_FIX/man"
   INFO_DIR="$PRE_FIX/info"
   PIXMAPS_DIR="$DATA_DIR/pixmaps"
   ICONS_DIR="$DATA_DIR/icons"
}

make_pkg_slackware() {
   declare PKG_DIR="$1"
   declare PKG_NAME="$2"
   declare TAR=""
   if which tar-1.13 > /dev/null 2>&1 ; then
      TAR="$(which tar-1.13)"
   else
      TAR="$(which tar)"
      tprintf $"@warning{Warning!}: No tar-1.13 found in the PATH.\n"
      echo $"Cannot build true Slackware-compatible packages"
   fi
   if [[ "$PKG_POLICY" != "slackware" ]] ; then
      tprintf $"@warning{Warning!}: Package structure policy not 'slackware'.\n"
      echo $"Package may not properly comply with Slackware conventions"
   fi
   mkdir -p "$PKG_DIR/metadata/install"
   if [[ -f "$SOURCES_DIR/slack-desc" ]] ; then
      cp "$SOURCES_DIR/slack-desc" "$PKG_DIR/metadata/install/slack-desc"
   else
      make_slackware_desc "$PKG_DIR" "$PKG_DIR/metadata/install/slack-desc"
   fi
   if [[ -f "$SOURCES_DIR/doinst.sh" ]] ; then
      cp "$SOURCES_DIR/doinst.sh" "$PKG_DIR/metadata/install/doinst.sh"
   else
      make_doinst "$PKG_DIR" "$PKG_DIR/metadata/install/doinst.sh"
   fi
   cp -r "$PKG_DIR/metadata/install" "$PKG_DIR"
   declare PKG="$PKG_DEST_DIR/$PKG_NAME-$VERSION-$ARCH-$BUILD$TAG"
   case "$PKG_COMPRESSION" in
      gzip)
         PKG="$PKG.tgz"
      ;;
      bzip2)
         PKG="$PKG.tbz"
      ;;
      lzma)
         PKG="$PKG.tlz"
      ;;
      xz)
         PKG="$PKG.txz"
      ;;
      *)
         tprintf $"@error{Error!}: Unsupported compression for Slackware package\n"
         cleanup 1
      ;;
   esac
   if ! "$TAR" --owner=root --group=root -c --use-compress-program "$PKG_COMPRESSION" -C "$PKG_DIR" -f "$PKG" . --exclude "metadata" ; then
      tprintf $"@error{Error!}: Creating Slackware package tarball failed\n"
      cleanup 1
   fi
   tprintf $"@summary{Package Creation} - @success{Successful!} Package location\n"
   echo "$PKG"
}

make_slackware_desc() {
   declare PKG_DIR="$1"
   declare DESC_FILE="$2"
   declare LINE=""
   if [[ -z "$BRIEF" ]] ; then
      LINE="$PKG_NAME: $PKG_NAME"
   else
      LINE="$PKG_NAME: $PKG_NAME ($BRIEF)"
      if [[ ${#LINE} -gt 80 ]] ; then
         tprintf $"@warning{Warning!}: BRIEF too long, line in description exceeds 80 characters\n"
      fi
   fi
   echo "$LINE" > "$DESC_FILE"
   echo "$PKG_NAME:" >> "$DESC_FILE"
   declare LINE_LIMIT=8
   if [[ -z "$HOMEPAGE" ]] ; then
      (( ++LINE_LIMIT ))
   fi
   declare WIDTH=$((78-${#PKG_NAME}))
   while [[ "$DESC" != "" ]] ; do
      if [[ $LINE_LIMIT -eq 0 ]] ; then
         tprintf $"@warning{Warning!}: DESC too long, truncating\n"
         break
      fi
      declare CHUNK="${DESC:0:$WIDTH}"
      if (( ${#CHUNK} == $WIDTH )) ; then
         CHUNK=${CHUNK% *}
      fi
      echo "$PKG_NAME: $CHUNK" >> "$DESC_FILE"
      (( --LINE_LIMIT ))
      DESC="${DESC:${#CHUNK}}"
      DESC="${DESC# *}"
   done
   while [[ $LINE_LIMIT -gt 2 ]] ; do
      echo "$PKG_NAME:" >> "$DESC_FILE"
      (( --LINE_LIMIT ))
   done
   echo "$PKG_NAME:" >> "$DESC_FILE"
   if [[ "$HOMEPAGE" ]] ; then
      LINE="$PKG_NAME: Homepage: $HOMEPAGE"
      if [[ ${#LINE} -gt 80 ]] ; then
         tprintf $"@warning{Warning!}: HOMEPAGE too long, line exceeds 80 characters\n"
      fi
      echo "$LINE" >> "$DESC_FILE"
   fi
   LINE="$PKG_NAME: Packaged by $PACKAGER"
   if [[ ${#LINE} -gt 80 ]] ; then
      tprintf $"@warning{Warning!}: PACKAGER too long, line exceeds 80 characters\n"
   fi
   echo "$LINE" >> "$DESC_FILE"
}

make_doinst() {
   declare PKG_DIR="$1"
   declare DOINST_FILE="$2"
   echo "#!/bin/sh" > "$DOINST_FILE"
   if [[ -e "$SOURCES_DIR/doinst.prepend" ]] ; then
      cat "$SOURCES_DIR/doinst.prepend" >> "$DOINST_FILE"
   fi
   if [[ -d "$PKG_DIR/$SYSCONF_DIR" ]] ; then
      cat >> "$DOINST_FILE" << "EOF"
config() {
   NEW="$1"
   OLD="`dirname $NEW`/`basename $NEW .new`"
   if [ ! -r "$OLD" ] ; then
      mv "$NEW" "$OLD"
   elif [ "`md5sum < $OLD`" = "`md5sum < $NEW`" ] ; then
      rm "$NEW"
   fi
}
EOF
      declare CONF=""
      for CONF in $(find "$PKG_DIR/$SYSCONF_DIR" -type f); do
         cat >> "$DOINST_FILE" << EOF
config "${CONF#$PKG_DIR/}.new"
EOF
         mv "$CONF" "$CONF.new"
      done
   fi
   declare LINK=""
   for LINK in $(find "$PKG_DIR" -type l) ; do
      cat >> "$DOINST_FILE" << EOF
( cd "$(dirname ${LINK#$PKG_DIR/})" ; rm -rf "${LINK##*/}" )
( cd "$(dirname ${LINK#$PKG_DIR/})" ; ln -sf "$(readlink $LINK)" "${LINK##*/}" )
EOF
      rm -f "$LINK"
   done
   if [[ -f "$PKG_DIR/$SYSCONF_DIR/rc.d/rc.$NAME.new" ]] ; then
      cat >> "$DOINST_FILE" << EOF
if grep -q rc.$NAME "/$SYSCONF_DIR/rc.d/rc.local" ; then
   echo '# Start $NAME service' >> "/$SYSCONF_DIR/rc.d/rc.local"
   echo 'if [ -x "/$SYSCONF_DIR/rc.d/rc.$NAME" ] ; then' >> "/$SYSCONF_DIR/rc.d/rc.local"
   echo '   . "/$SYSCONF_DIR/rc.d/rc.$NAME" start' >> "/$SYSCONF_DIR/rc.d/rc.local"
   echo 'fi' >> "/$SYSCONF_DIR/rc.d/rc.local"
fi
EOF
   fi
   if [[ -d "$PKG_DIR/$INFO_DIR" ]] ; then
      cat >> "$DOINST_FILE" << EOF
if [ -x "/$BIN_DIR/install-info" ] ; then
EOF
      declare INFO
      for INFO in $(ls -1 "$PKG_DIR/$INFO_DIR") ; do
         cat >> "$DOINST_FILE" << EOF
   install-info --info-dir "/$INFO_DIR" "/$INFO_DIR/$INFO" 2> /dev/null
EOF
      done
      cat >> "$DOINST_FILE" << EOF
fi
EOF
   fi
   if [[ -d "$PKG_DIR/$DATA_DIR/applications" && $(find "$PKG_DIR/$DATA_DIR/applications" -type f) ]] ; then
      cat >> "$DOINST_FILE" << EOF
if [ -x "/$BIN_DIR/update-desktop-database" ] ; then
   /$BIN_DIR/update-desktop-database "/$DATA_DIR/applications" > /dev/null 2>&1
fi
EOF
   fi
   if [[ -d "$PKG_DIR/$ICONS_DIR" ]] ; then
      cat >> "$DOINST_FILE" << EOF
if [ -x "/$BIN_DIR/gtk-icon-cache" ] ; then
   /$BIN_DIR/gtk-icon-cache --quiet "/$ICONS_DIR"
fi
EOF
   fi
   if [[ -d "$PKG_DIR/$DATA_DIR/mime" ]] ; then
      cat >> "$DOINST_FILE" << EOF
if [ -x \"/$BIN_DIR/update-mime-database\" ] ; then
   /$BIN_DIR/update-mime-database \"/$DATA_DIR/mime\"
fi
EOF
   fi
   if [[ -e "$SOURCES_DIR/doinst.append" ]] ; then
      cat "$SOURCES_DIR/doinst.append" >> "$DOINST_FILE"
   fi
   if [[ "$(cat "$DOINST_FILE")" == "#!/bin/sh" ]] ; then
      rm "$DOINST_FILE"
   else
      chmod +x "$DOINST_FILE"
   fi
}
