#!/bin/sh
# <BEGIN COPYRIGHT>
# This file is part of the "Bad Penguin" GNU/Linux distribution
# Copyright (C) 1997-98-99 by Antonio GALLO
# Author of this file: Antonio GALLO aka AGX
# Home Page at ......: http://www.linux.it/~agx/
# Home Page at ......: http://www.geocities.com/SiliconValley/Bay/7305/
# e-mail contact ....: mailto:agx@linux.it	mailto:agx@geocities.com
# This program is distributed under the GNU General Public License
# You are not allowed to remove the copyright notice
# <END COPYRIGHT>
# 1.0.0 by agx@linux.it 

#setup--request, this is called by setup--install
#This script is part of the package management project of the
#Bad Penguin distribution.
#19990505 - Version 1.1.0 by AGX
#19990629 - Version 1.1.3 by AGX

# --------------------------------------------------------

#1.1.3
if [ "$SETUP_MODE" = "" ]; then
    echoc RED "This script cannot be launched interactively, use setup --help"
    exit 100
fi

[ $SETUP_VERBOSE -gt 1 ] && echo "# entering setup--request"

# Initialize mode
if [ "$SETUP_MODE" = "INSTALL" ]; then
  DEPENDFILE="./install/REQUEST"
  DEPENDLOCK="./install/REQUEST"
else
  DEPENDFILE="$SPOOL_QUEUE/DEPENDENCIES"
  DEPENDLOCK="$SPOOL_QUEUE/.DEPENDENCIES.LOCKED"
fi

# Check if there is a REQUEST file
if [ -f "$DEPENDFILE" ]; then

  if [ ! "$SETUP_MODE" = "INSTALL" ]; then
    # In finalize mode we should lock the deps file
    echo "# checking system dependencies"
    mv "$DEPENDFILE" "$DEPENDLOCK"
    DEPENDFILE="$DEPENDLOCK"
  fi

  [ $SETUP_VERBOSE -gt 1 ] && echo "- Reading the REQUEST file"
  #Initialize vars
  R_CAT=""
  R_PKG=""
  R_LIBRARY=""
  #Parse the request file
  while read R_CAT R_PKG R_LIBRARY; do

    #Ignore comments
    case "$R_CAT" in
	"#*")
	    R_CAT=""
	    continue
	    ;;
	\#*)
	    R_CAT=""
	    ;;	    
    esac

    if [ ! "$R_CAT" = "" ]; then
  
      # Check if the needed package is already installed
      if [ ! "$R_PKG" = "" ]; then
        [ $SETUP_VERBOSE -gt 1 ] && echo "- depends from the package $R_LIBRARY, checking."
	for d in $SPOOL_PKG/$R_LIBRARY*; do
          if [ -f $d ]; then
            [ $SETUP_VERBOSE -gt 0 ] && echo "- depends from the package $R_LIBRARY, resolved using `basename $f`"
            continue
          fi
	done
      fi

      # Check if the needed library are already installed
      # if it is found into the default path then it skip to the next
      if [ ! "$R_LIBRARY" = "" ]; then
        [ $SETUP_VERBOSE -gt 1 ] && echo "- depends from the library $R_LIBRARY, checking"
	  if [ -f lib/$R_LIBRARY -o -f usr/lib/$R_LIBRARY -o -f usr/local/lib/$RLIBRARY -o -f usr/X11R6/lib/$R_LIBRARY ]; then
          [ $SETUP_VERBOSE -gt 0 ] && echo "- depends from the library $R_LIBRARY, resolved"
	    continue;
	  fi
      fi

      # Schedule or Finalize
      if [ "$SETUP_MODE" = "INSTALL" ]; then
        # Schedule the dependencie
        [ $SETUP_VERBOSE -gt 0 ] && echo "# dependencies not resolved ( $R_PKG $R_LIBRARY ), caching."
        echo "$R_CAT	$R_PKG	$R_LIBRARY" >> "$SPOOL_QUEUE/DEPENDENCIES"
      else
        # Schedule package for installation
        [ $SETUP_VERBOSE -gt 0 ] && echo "# dependencies not resolved ( $R_PKG $R_LIBRARY ), scheduling."
        mkdir -p $SPOOL_QUEUE/$R_CAT/
        echo > $SPOOL_QUEUE/$R_CAT/$R_PKG.install
      fi
      #
    fi
  done < $DEPENDFILE
else
  [ $SETUP_VERBOSE -gt 0 ] && echoc GREEN "- no dependencies requested."
fi

exit 0
