#!/bin/sh
# install-groups
# Build tarballs in a directory tree
# Install it and also produce Vector Linux (slackware) package
#
# (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 "install-group
Build and install several tarballs 

Usage: install-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}
PKG_BASE=${PKG_BASE:-$START_DIR/pkg}
SRC_BASE=${SRC_BASE:-$START_DIR/src}
TGZ_BASE=${TGZ_BASE:-$START_DIR/tgz}
MAKE_BASE=${MAKE_BASE:-$START_DIR/tmp/make}
DEST_BASE=${DEST_BASE:-$START_DIR/tmp/dest}
PEXT=${PKGFORMAT:-tgz}

PGROUP=$1
shift

echo
echo "==============================================================="
echoc "INSTALL GROUP $PGROUP" magenta

# read group settings
SET_DIR=$PGROUP
SET_DIR1="$TAR_BASE"
while [ "$SET_DIR" ]; do
   SET_DIR1="$SET_DIR1/`echo $SET_DIR | cut -f1 -d/`"
   if [ -f $SET_DIR1/settings.sh ]; then
	echoc "Reading $SET_DIR1/settings.h" blue
	. $SET_DIR1/settings.sh
	cat $SET_DIR1/settings.sh
   fi
   if ! echo $SET_DIR | grep -q "/"; then
      break
   fi
   SET_DIR=`echo $SET_DIR | cut -f2- -d/`
done
unset SET_DIR SET_DIR1

clean_exit()
{
   cd $DEST_BASE
   rm -rf $PGROUP
   rmdir -p $PGROUP
   cd $START_DIR
   unset START_DIR
   exit $1
}

# Find the package under current dir
find_pkg()
{
   for EXT in $*; do
     ls -1 $1*.$EXT 2>/dev/null | while read LINE; do
       if [ "`pkgname $LINE`" = "$1" ]; then
         echo $LINE
         return 0
      fi
    done
  done
  return 1
}

delete_old()
{
    for POLD in $2/$1-*; do
	BOLD=`basename $POLD`
	if [ "`pkgname $BOLD`" = "$1" ]; then
	    rm -vf $POLD
	fi
    done
}

do_install_tgz()
{
   echo
   echo "-----------------------------------------------------------------"
   echoc "INSTALL TGZ $1" yellow
   if [ "$REINSTALL_PKG" = "yes" ]; then
       PARAM="-o"
   else
       PARAM="-u"
   fi
   if [ "$TARGET" ]; then
      if [ "$INSTALL_HOST" = "yes" ]; then
         pkg $PARAM $1 && ldconfig
      fi
      if [ "$INSTALL_PKG" = "yes" ]; then
         ROOT=$TARGET pkg $PARAM $1
      fi
   else
      if [ "$INSTALL_HOST" = "yes" ] || [ "$INSTALL_PKG" = "yes" ]; then
         pkg $PARAM $1 && ldconfig
      fi
   fi
   if [ ! $? = 0 ]; then
     echoc "ERROR: Could not install $1. Aborting" red
     clean_exit 1
   fi
   echoc "Premade package $1 has been installed" green    
   if [ "$COPY_MEDIA" = "yes" ] && [ "$MEDIA_PACKAGES" ]; then
     mkdir -p $MEDIA_PACKAGES/$PGROUP
     delete_old "`pkgname $1`" $MEDIA_PACKAGES/$PGROUP
     cp -u $1 $MEDIA_PACKAGES/$PGROUP
     cp -u ${1%.*}.txt $MEDIA_PACKAGES/$PGROUP
     echoc "Premade $1 has been copied to $MEDIA_PACKAGES" green
   fi
}

do_install()
{
  # If there is a tarball-dir, use install-tarball
  if [ -d $TAR_BASE/$PGROUP/$1 ]; then
    echo
    echo "-----------------------------------------------------------------"
    echoc "INSTALL TARBALL $1" yellow
    cd $TAR_BASE/$PGROUP/$1
    install-tarball
    if [ ! $? = 0 ]; then
      echoc "ERROR: install-tarball $PGROUP/$1 was failed. Aborting." red
      clean_exit 1
    fi
    if [ "$COPY_MEDIA" = "yes" ] && [ "$MEDIA_PACKAGES" ]; then
       cd $PKG_BASE/$PGROUP
       TGZ=`find_pkg $1 $PEXT`
       if [ "$TGZ" ]; then
    	  mkdir -p $MEDIA_PACKAGES/$PGROUP
          delete_old "`pkgname $TGZ`" $MEDIA_PACKAGES/$PGROUP
          cp -u $TGZ $MEDIA_PACKAGES/$PGROUP
          cp -u ${TGZ%.*}.txt $MEDIA_PACKAGES/$PGROUP
          echoc "Package $TGZ has been copied to $MEDIA_PACKAGES" green
       else
          echoc "ERROR: Could not find compiled $PGROUP/$1." red
          clean_exit 1
       fi
    fi
    cd $START_DIR
    return 0
  fi
  # try premade tgz
  if [ -d $TGZ_BASE/$PGROUP ]; then
    cd $TGZ_BASE/$PGROUP
    TGZ=`find_pkg $1 tgz tbz`
    if [ "$TGZ" ]; then
      do_install_tgz $TGZ
      return $?
    fi
  fi
  # no luck
  echoc "ERROR: Could not find $PGROUP/$1 tarbal nor package." red
  clean_exit 1
}

####### MAIN
export PKG_DIR="$PKG_BASE/$PGROUP"
export MAKE_DIR="$MAKE_BASE/$PGROUP"
export DEST_DIR="$DEST_BASE/$PGROUP"

cd $START_DIR
if [ "$1" = "--all" ]; then
   # install all prebuilt packages
   if [ "$INSTALL_PKG" = "yes" ] && [ -d $TGZ_BASE/$PGROUP ]; then
     cd $TGZ_BASE/$PGROUP
     for TGZ in *.tgz; do
       do_install_tgz $TGZ
     done
     for TGZ in *.tbz; do
       do_install_tgz $TGZ
     done
   fi
   # install all tarballs
   cd $START_DIR
   for DIR in $TAR_BASE/$PGROUP/*; do
      if [ -d $DIR ] && [ "${DIR##*.}" != "bak" ]; then
         do_install `basename $DIR`
      fi   
   done
else
   while [ "$1" ]; do
      do_install $1
      shift  
   done
fi
cd $START_DIR
