#!/bin/sh
# compress-groups
# Compress tarballs in a directory tree
#
# (c) 2003, Eko M. Budi
#
#    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-groupt
Compress several tarballs and put it in the source tree

Usage: compress-group <group> < --all | package [ package [...] ] >
"
exit 0
}

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

START_DIR=`pwd`
# Check environment variables
TAR_BASE=${TAR_BASE:-$START_DIR/tar}
SRC_BASE=${SRC_BASE:-$START_DIR/src}
TGZ_BASE=${TGZ_BASE:-$START_DIR/tgz}
PEXT=${PKGFORMAT:-tgz}

PGROUP=$1
shift

# variables needed by compress tarball
export SRC_DIR="$SRC_BASE/$PGROUP"


clean_exit()
{
   cd $START_DIR
   exit $1
}

do_compress()
{
    echo
    echo "-----------------------------------------------------------"
    cd $1
    compress-tarball || clean_exit 1
}

cd $START_DIR
if [ "$1" = "--all" ]; then
   echo "==============================================================="
   echoc "COMPRESS ALL GROUP $PGROUP" magenta
   # compress all tarballs
   for DIR in $TAR_BASE/$PGROUP/*; do
      if [ -d $DIR ] && [ "${DIR##*.}" != "bak" ]; then
         do_compress $DIR
      fi   
   done
else
   echo "==============================================================="
   echoc "COMPRESS GROUP $PGROUP" magenta
   while [ "$1" ]; do
      if [ -d $TAR_BASE/$PGROPU/$1 ]; then
	 do_compress $TAR_BASE/$PGROUP/$1
      else
	 echoc "ERROR: Could not find $DIR tarbal." red
	 clean_exit 1
      fi
      shift  
   done
fi
cd $START_DIR
