#!/bin/bash
##############################################################################
# compress-tarball
# Compress source tarball in the current directory, 
# then put it into SRC_DIR
#
# (c) 2003, Eko M. Budi
# License: GNU GPL
#
# Parts of this script were taken from 
# - SlackBuild, commonly available in various Slackware's source packages
# - makepkg, Copyright 1994, 1998  Patrick Volkerding, Moorhead, Minnesota USA 
# - checkinstall 1.5.3, Copyright 2001 Felipe Eduardo Sanchez Diaz Duran
#
#    This program is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#
#    This program 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 this program; if not, write to the Free Software
#    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
############################################################################

usage()
{
echo "compress-tarball
Compress tarball in the current directory

Usage: export SRC_DIR=/path; compress-tarball
"
exit 0
}

if [ "$1" = "-h" ] || [ "$1" = "--help" ]; then
  usage
fi

CWD=`pwd`

echoc "Compress tarball `basename $CWD`" yellow
echo  "Directory = $CWD"

# determine the values
VERSION=0.1
COMPRESS_CMD="tar -cjf"

# Transfer global variables
# Sorry, not the same, since the were evolve :)
[ -z "$SRC_DIR" ] && SRC_DIR=$HOME/src
mkdir -p $SRC_DIR  || exit 1

# Empty the variables we expect from settings.sh
PNAME=""
PVERSION=""
PVERSION_EXTRA=""
PARCH=""
TNAME=""

# Source the settings
if [ -f settings.sh ]; then
   . settings.sh
fi


# set package name & tarball name
PNAME=${PNAME:-`basename $CWD`}
TNAME=${TNAME:-$PNAME}

# Do we have a TGZ ?
if [ -f $PNAME*.tgz ]; then
    TGZBALL=`ls -1r $PNAME*.tgz` 
    TNAME_VER_ARCH_REL=${TGZBALL%.tgz} # remove suffix
    TREL=${TNAME_VER_ARCH_REL##*-}          # Everthing after the last '-'
    TNAME_VER_ARCH=${TNAME_VER_ARCH_REL%-*} # remove REL
    TARCH=${TNAME_VER_ARCH##*-}         # Everything after the last -
    TNAME_VER=${TNAME_VER_ARCH%-*}      # remove ARCH
    TVER=${TNAME_VER#$PNAME-}           # get the version
fi

# Do we have a tarball ?
TARBALL=`ls -1r $TNAME*.tar.bz2 2>/dev/null | head -n1`
if [ "$TARBALL" ]; then
    TNAME_VER=${TARBALL%.tar.bz2}
    TVER=${TNAME_VER#${TNAME}-}
else
    TARBALL=`ls -1r $TNAME*.tar.gz 2>/dev/null | head -n1`
    if [ "$TARBALL" ]; then
	TNAME_VER=${TARBALL%.tar.gz}
	TVER=${TNAME_VER#${TNAME}-}
    fi
fi

# determine package version
PVERSION=${PVERSION:-$TVER}
if [ -z "$PVERSION" ]; then
    echoc "ERROR: Cannot determine version" red
    exit 1
fi
PVERSION=${PVERSION}${PVERSION_EXTRA}

# determine package arch, always src
PARCH="src"

# package release
PRELEASE=${PRELEASE:-$TREL}
PRELEASE=${PRELEASE:-1}

# package format
PEXT="tar.bz2"

# the proposed name + version
PNAME_VER="$PNAME-$PVERSION"
PNAME_VER_ARCH_REL="$PNAME_VER-$PARCH-$PRELEASE"

############################################################################
# here we go now

echo  "Output directory = $SRC_DIR"
CFILE=$SRC_DIR/$PNAME_VER_ARCH_REL.$PEXT

if [ ! "$OVERWRITE_PKG" = "yes" ]; then
  if [ -f "$CFILE" ]; then
     NEWER=`find . -newer $CFILE`
     if [ -z "$NEWER" ]; then
       echoc "Compressed file = $PNAME_VER_ARCH_REL.$PEXT is up to date." cyan
       exit 0
     fi
  fi
fi

cd $CWD/..
$COMPRESS_CMD $CFILE `basename $CWD` || exit 1
cd $CWD
echoc "Compressed file  = $PNAME_VER_ARCH_REL.$PEXT" green

