#!/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--restore
#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--restore"

# Checking Installation
if [ ! -d $FULLPKGNAME ]; then
  if [ $SETUP_FORCE -eq 1 ]; then
    echoc YELLOW "The Package $PKGNAME is not installed on $TARGET - Forcing the Restoring"
  else
    echoc YELLOW "The Package $PKGNAME is not installed on $TARGET"
    cd "$OLD_PWD"
    if [ $SETUP_RELAXED=1 ]; then
      exit 0
    else
      sleep 1
      exit 10
    fi
  fi
fi

# ---------------------------------------------------------
echoc GREEN "Restoring package $PKGNAME on $TARGET"
# ---------------------------------------------------------

TAR_VERBOSE=""
[ $SETUP_VERBOSE -gt 0 ] && TAR_VERBOSE="-v"
# TODO: Vanno restorati solo i file NON PRESENTI
tar -zxpkf $FILENAME $TAR_VERBOSE 2>/dev/null


# Restore Incoming
# <INCOMING>
# Create files that !ONLY! does not exist
if [ -d ./incoming ]; then
  [ $SETUP_VERBOSE -gt 0 ] && echo "- Restoring Incoming Files ..."
  cat ./install/MANIFEST | grep "incoming/" | while read SOURCEFILE; do
    DESTFILE="`echo $SOURCEFILE | cut -b 10-`"
    if [ "$DESTFILE" = "" ]; then
      continue
    else
      DESTFILE="$TARGET/$DESTFILE"
    fi
    [ $SETUP_VERBOSE -gt 1 ] && echo "- checking file $DESTFILE"
    if [ -d "$SOURCEFILE" ]; then
      if [ ! -d "$DESTFILE" ]; then
        [ $SETUP_VERBOSE -gt 0 ] && echo "# Making directory $DESTFILE"
        mkdir -p "$DESTFILE"
      fi
    else
      # it's a file or symlink
      if [ ! -f "$DESTFILE" ]; then
        [ $SETUP_VERBOSE -gt 0 ] && echo "- Copying file $DESTFILE"
        cp -dipu "$SOURCEFILE" "$DESTFILE"
      fi
    fi
  done
  #
  #1.1.2: if [ "$TARGET" = "/" ]; then
  if [ $SETUP_INTERACTIVE -eq 1 ]; then
    # ldconfig when in interactive mode
    [ $SETUP_VERBOSE -gt 0 ] && echo "# Configuring Librarians ..."
    ldconfig
  fi
fi
#</INCOMING>


# Clean-up
[ $SETUP_VERBOSE -gt 1 ] && echo "- Removing temp files"
cd $TARGET
rm -f $PKGNAME $PKGNAME.tar $PKGNAME.MANIFEST
rm -fr ./incoming
rm -fr ./install
# End
[ $SETUP_VERBOSE -gt 0 ] && echo "- package succesfully restored."
cd "$OLD_PWD"
exit 0
