#! /bin/sh
# BPP Install - @1996-2002 by Antonio Gallo
# agx@linux.it - www.badpenguin.org - www.linux.it/~agx
# 2001-11-15 4.0.0

# include bpp library
if [ -f /setup/bpp-functions ]; then
  . /setup/bpp-functions
else
  . /usr/libexec/bpp-functions
fi

# init options and vars
TARGETDIR="/"
PKGFULLNAME=""
PKGSONAME=""
PKGVERSION=""
PKGBPP=""
PKGTXT=""
OPT_FORCE=0
TAR_OPTIONS=" --atime-preserve --same-owner --same-permissions --same-order "

# CHECK PARAMETERS
if [ "$1" = "" ]; then
  fn_error "need at least 1 argument !"
fi
PKGFULLNAME=`basename $1 .bpp`
PKGFULLNAME=`basename $PKGFULLNAME .txt`
PKGSONAME=`echo $PKGFULLNAME | cut -f 1 -d'_'`
PKGVERSION=`echo $PKGFULLNAME | cut -f 2 -d'_'`
PKGBPP="`dirname $1`/$PKGFULLNAME.bpp"
PKGTXT="`dirname $1`/$PKGFULLNAME.txt"
if [ "$PKGSONAME" = "" ]; then
  fn_error "incorrect package soname"
fi
if [ "$PKGVERSION" = "" ]; then
  fn_error "incorrect package version"
fi
if [ "$PKGVERSION" = "$PKGSONAME" ]; then
  fn_error "incorrect package name"
fi

# check others addittional parameters
while [ "$2" != "" ]; do
  shift
  case $1 in
    -f|--force)
      OPT_FORCE=1
      ;;
    *)
      # check if it is a target dir
      if [ "$TARGETDIR" = "/" ]; then
        if [ -d "$1" ]; then
	  TARGETDIR="$1"
	  continue
	fi
      fi
      echoc RED "option: $1 is unknown"
      exit 1
      ;;
  esac
done

# create/remove files/dirs
[ ! -d "$TARGETDIR$BPP_SPOOLDIR" ] && mkdir -p "$TARGETDIR$BPP_SPOOLDIR"
rm -fR "$TARGETDIR/tmp/bpp-*.tmp"  "$TARGETDIR/install"  "$TARGETDIR/incoming"

# we are going to install :-)
echoc CYAN  "* installing: $PKGSONAME ver. $PKGVERSION on $TARGETDIR"

# CHECK IF THE PACKAGE IS ALREADY INSTALLED
if [ -d "$TARGETDIR$BPP_SPOOLDIR/$PKGFULLNAME" ]; then
  if [ "$OPT_FORCE" = "0" ]; then
    echoc YELLOW "- $PKGFULLNAME is already installed"
    exit 1
  else
    echoc YELLOW "- $PKGFULLNAME is already installed, forcing installation"
  fi
fi 

# EXTRACT 'INFO' FROM THE .BPP IF THE .TXT IS NOT AVAILABLE
if [ ! -f "$PKGTXT" ]; then
  if [ ! -f "$PKGBPP" ]; then
    fn_error "bpp package does not exists $PKGBPP"
  fi
  echo "> extracting package info from $PKGBPP"
  PKGTXT="$TARGETDIR/tmp/bpp-pkginfo.tmp"
  tar -x -f "$PKGBPP" -z -O install/INFO  1>"$PKGTXT"
  if [ $? -ne 0 ]; then
    fn_error "unable to extract $PKGTXT"
  fi
else
  echo "- using package info file $PKGTXT"
fi


# TODO: we can now check if the architecture of the package match. Since
#       i think this is not very usefull todo because the BP distro is
#       only for x86 server i can skip this checking


# CHECK FREE DISK SPACE
grep -i -h "^DISK_USAGE" "$PKGTXT" | sort | cut -f 2 -d'"' | while read dsize ddir ; do
  #BUGFIX: it is necessary to check that the dir exists!
  if [ ! -e "$TARGETDIR/$ddir" ]; then
    mkdir -p "$TARGETDIR/$ddir"
  fi
  ddev=`df -k --no-sync "$TARGETDIR/$ddir" | tail -1 | cut -b -15`
  ddev=`basename $ddev`
  echo "- required $dsize KB on /dev/$ddev under $TARGETDIR/$ddir"
  devfile="$TARGETDIR/tmp/bpp-$ddev.du.tmp"
  if [ -f "$devfile" ]; then
    dfree=`cat $devfile`
  else
    dfree=`df -k --no-sync "$TARGETDIR/$ddir" | tail -1 | cut -b 37-50`
  fi
  dfree=$(( dfree - dsize ))
  echo $dfree > $devfile
done
#
for i in $TARGETDIR/tmp/bpp-*.du.tmp ; do
  [ ! -f $i ] && continue
  dfile="/dev/`basename $i .du.tmp | cut -b 5-`"
  dfree=`cat $i`
  if [ $dfree -lt 1024 ]; then
    fn_error "not enough disk space on $dfile: $dfree KB"
    exit 1
  elif [ $dfree -lt 10240 ]; then
    echoc YELLOW "du: low disk space on $dfile: $dfree KB"
  else
    echo "- space that will remain on $dfile: $dfree KB"
  fi
done

# CHECK PREREQUISITE FILES
ERRORFILE="$TARGETDIR/tmp/bpp-errorflag.tmp"
rm -f $ERRORFILE
grep -i "^REQUIRE_FILE" "$PKGTXT" | cut -f 2 -d'"' | while read rline ; do
  FOUND=0
  rfile=`echo $rline | cut -f 1 -d':'`
  rpkg=`echo $rline | cut -f 2 -d':'`
  for i in "" /bin /sbin /usr/bin /usr/sbin /lib /usr/lib /usr/local/bin /usr/local/sbin /usr/local/lib ; do
    if [ -e "$TARGETDIR$i/$rfile" ]; then
      FOUND=1
      break
    fi
  done
  if [ $FOUND -eq 0 ]; then
    echoc YELLOW "- required file: $rfile not found, you need to install the package $rpkg first!"
    touch $ERRORFILE
  else
    echo "- required file: $rfile has been found in $i"
  fi
done

# CHECK PREREQUISITE PACKAGES
grep -i "^REQUIRE_BPP" "$PKGTXT" | cut -f 2 -d'"' | while read rpkg ; do
  npkg=`find "$TARGETDIR$BPP_SPOOLDIR" -iname "$rpkg*" | tail -1`
  if [ -d "$npkg" ]; then
    echo "- required package: $rpkg is already installed"
  else
    echoc YELLOW "- required package: $rpkg is NOT installed"
    touch $ERRORFILE
  fi
done

# abort if any error about the deps
if [ -f "$ERRORFILE" ]; then
  echoc RED "! Please install the missing package to continue the installation"
  rm -f "$ERRORFILE"
  exit 1
fi


# TODO: we should remove any package wich named conflicts here. Package
#       which are marked 'vital' cannot be removed if we are installing
#	under the '/' directory


# EXTRACTING INFO & SCRIPTS
echo "- finalizing installation of $PKGFULLNAME"
cd "$TARGETDIR"

echo "> extracting infoes"
tar -x -z -U $TAR_OPTIONS -f "$PKGBPP" install/
if [ $? -ne 0 ]; then
  fn_error "error while extracting info files from the tarball"
fi
if [ -x ./install/PRE-INSTALL ]; then
  echo "> running PRE-INSTALL script..."
  ./install/PRE-INSTALL
  if [ $? -ne 0 ]; then
    echoc YELLOW "press ENTER to continue with errors"
    read x
  fi
fi

# UNPACKING FILES
echo "> extracting all files..."
cd "$TARGETDIR"
tar -x -z -U $TAR_OPTIONS -f "$PKGBPP" 
if [ $? -ne 0 ]; then
  fn_error "error while extracting all files from tarball"
fi

# EXTRACT MISSED "INCOMING" FILES
cd "$TARGETDIR"
if [ -d incoming ]; then
  (cd incoming ; tar -O $TAR_OPTIONS -c * ) | ( cd "$TARGETDIR" ; tar -x -k $TAR_OPTIONS ) 2>&1 | grep -v "File exists" | grep -v "delayed" >"$ERRORFILE"
  if [ -s "$ERRORFILE" ]; then
    fn_error "Error while extracting incoming files"
  fi
fi

# RUN "INSTALL" SCRIPT
cd "$TARGETDIR"
if [ -x ./install/INSTALL ]; then
  echo "> running INSTALL script..."
  ./install/INSTALL
  if [ $? -ne 0 ]; then
    echoc YELLOW "press ENTER to continue with errors"
    read x
  fi
fi

# RUN "POST-INSTALL" SCRIPT
cd "$TARGETDIR"
if [ -f ./install/POST-INSTALL ]; then
  echo "> queuing post-install script"
  mv ./install/POST-INSTALL  "$TARGETDIR$BPP_SPOOLDIR/$PKGFULLNAME.post-install"
  [ $? -ne 0 ] && fn_error "Error while queuing the post-install script"
fi
if [ -f ./install/X-POST-INSTALL ]; then
  echo "> queuing x-post-install script"
  mv ./install/X-POST-INSTALL  "$TARGETDIR$BPP_SPOOLDIR/$PKGFULLNAME.x-post-install"
  [ $? -ne 0 ] && fn_error "Error while queuing the x-post-install script"
fi

# REGISTER THE INSTALLED PACKAGE INTO THE DATABASE
cd "$TARGETDIR"
if [ -d ./install ]; then
  echo "> registering package"
  PDIR="$TARGETDIR$BPP_SPOOLDIR/$PKGFULLNAME"
  rm -fR "$PDIR"
  mv ./install "$PDIR"
  [ $? -ne 0 ] && fn_error "Error while rtegistering the package"
fi

# REMOVE ANY TEMPORARY FILES
rm -fR "$TARGETDIR/tmp/bpp-*.tmp"  "$TARGETDIR/install"  "$TARGETDIR/incoming"
echoc GREEN "- package successfully installed"

# IF WE ARE INSTALLING LOCALLY THEN RUN THE POSTINSTALL
if [ "$TARGETDIR" = "/" ]; then
  echo "- running post-install scripts if there"
  bpp-postinstall
fi

exit 0
