#!/bin/bash
# This script checks converts .deb packages to .tgz.
# Copyright (C) 2007 Matthew Bruenig <matthewbruenig@gmail.com>
#
# This file is free software; the copyright holder gives unlimited
# permission to copy and/or distribute it, with or without
# modifications, as long as this notice is preserved.
#
# This file is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY, to the extent permitted by law; without
# even the implied warranty of MERCHANTABILITY or FITNESS FOR A
# PARTICULAR PURPOSE.

#Initialize Variables
INFO="0"
CONVERT="0"
MAKEPKGOPTS="-l y -c n"
TMPDIR="/tmp/deb2tgz"
CONTROL="control.tar.gz"
DATA="data.tar.gz"
STARTDIR="$PWD"
DEBFILE=

#Some Functions
err_msg() {
  echo "$1"
  exit 1
}

usage() {
  echo "deb2tgz converts debian packages to tgz packages"
  echo
  echo "Usage: deb2tgz [options] [deb package]"
  echo
  echo "Options:"
  echo
  echo "  -i, --info              - only retreive meta-info from deb package"
  echo
  echo "  -m, --makepkg [options] - add specific makepkg options for the .tgz"
  echo "                            script assumes \"-l y -c n\" if not provided"
  echo "                            be sure to quote the [options]"
  echo
  echo "  -h, --help              - display this message"
  echo
  exit
}

#Are there any parameters?
[ "$#" -eq "0" ] && err_msg "Incorrect usage, see deb2tgz --help."

#Parse Parameters
while [ "$#" -gt "0" ]; do
  param="$1"
  if [ "${param:0:2}" = "--" ]; then
    case "${param:2}" in
      makepkg) MAKEPKGOPTS="$2" ; shift ;;
      info)    INFO="1"                 ;;
      help)    usage                    ;;
      *) err_msg "Option \`$param' is not recognized, see deb2tgz --help." ;;
    esac
  elif [ "${param:0:1}" = "-" ]; then
    shortparam="$(sed 's/./& /g' <<<${param:1})"
    for character in $shortparam; do
      case $character in
        m) MAKEPKGOPTS="$2" ; shift ;;
        i) INFO="1"                 ;;
        h) usage                    ;;
        *) err_msg "Option \`$character' is not recognized, see deb2tgz --help." ;;
      esac
    done
  elif [ -f "$param" ]; then
    DEBFILE="$param"
  else
    err_msg "Error parsing \`$param', see deb2tgz --help."
  fi
  shift
done

#Some checks
[ -z "$DEBFILE" ]              && err_msg "No deb package was specified."
[ ! "${DEBFILE##*.}" = "deb" ] && err_msg "File \`$DEBFILE' does not have a .deb extension."
[ "$INFO" -ne "1" ]            && CONVERT="1"

#Get dependencies option
if [ "$INFO" -eq "1" ]; then
  [ ! -d "$TMPDIR" ] && mkdir -p "$TMPDIR"
  cp "$DEBFILE" "$TMPDIR"
  cd "$TMPDIR"
  ar x "$DEBFILE" "$CONTROL"
  tar xf "$CONTROL"
  cat control
  rm -rf "$TMPDIR"
fi

if [ "$CONVERT" -eq "1" ]; then
  [ ! -d "$TMPDIR" ] && mkdir -p "$TMPDIR"
  PKGNAME="${DEBFILE%.*}"
  PKG="$TMPDIR/$PKGNAME"
  cp "$DEBFILE" "$TMPDIR"
  cd "$TMPDIR"
  ar x "$DEBFILE" "$CONTROL" "$DATA"
  tar xf "$CONTROL"
 
  #Collect some variables from control file
  name="$(awk '/^Package: / {print $2}' <control)"
  desc="$(awk -F": " '/^Description: / {print $2}' <control)"
 
  #Create pkg directory and slack-desc
  mkdir -p $PKG/install
  echo "$name: $name - $desc" > "$PKG/install/slack-desc"
  for ((i=0; i<10; i++)); do
    echo "$name:" >> "$PKG/install/slack-desc"
  done

  #Extract binaries into place
  tar xf "$DATA" -C "$PKG"

  #Create the package
  cd "$PKG"
  makepkg $MAKEPKGOPTS "$STARTDIR/$PKGNAME.tgz" &>/dev/null
  rm -rf "$TMPDIR"
fi 
