#! /bin/ksh
USAGE='USAGE: logiso_copy [ -d config_prefix ]
   Get the iso log and install the logged files on the hard disk.

USAGE: logiso_preview  [ -d config_prefix ]
   List the files that would have been installed by logiso_copy.

If -d is specified, then the config file, ${drive_or_mirror_prefix}config
is used instead of config.  This allows a set of config files to be defined
one for each unique combination of cd-rom mount point and mirror directory
for users with multiple cd-rom drives or who what to populate different
mirror directories, e.g. to mirror different cd-rom volumes.
'
# (C) Copyright 1995 by Michael Coulter.  All rights reserved.

# Process parameters

      COMMAND="$(basename "$0")"
      CONFIG_PREFIX=""
      if [ $# -eq 2 -a "$1" = "-d" ]
      then
         shift  # done with -d
         CONFIG_PREFIX="$1"; shift
      fi
      if [ $# -ne 0 ]
      then
         echo "$USAGE" >&2
         echo "Expected zero or 2 arguments, got $#" >&2
         exit 1
      fi

# Set variables

   ISOFS_UTIL_DIR="${ISOFS_UTIL_DIR:-/usr/src/linux/fs/isofs/Utils}"

# Source in user definitions
# MOUNT_PATH
# MAP_TO_PATH
# CD_FILE

   CONFIG_FILE="${ISOFS_UTIL_DIR}/${CONFIG_PREFIX}config"
   if [ ! -r "$CONFIG_FILE" ]
   then
      echo "Unable to find config file, $CONFIG_FILE" >&2
      exit 1
   fi
   . "${ISOFS_UTIL_DIR}/${CONFIG_PREFIX}config"
   export ISOFS_UTIL_DIR

# Set up temp files

    INODE_LIST="/tmp/logisoina$$"
    INSTALL_LIST="/tmp/logisoinb$$"
    trap "rm -f $INODE_LIST $INSTALL_LIST" 0 1 2 3 15

# Define standard functions

   . "$ISOFS_UTIL_DIR/ksh_fns"

# Check for root if doing copy

   if [ "$COMMAND" = "logiso_copy" ]
   then
      if [ "$(id -u)" -ne 0 ]
      then
	 echo "$USAGE" >&2
	 echo "You are not root. Do you want to continue (type y for yes)?" >&2
	 read RESPONSE
	 if [ "$RESPONSE" != "y" ]
	 then
	    exit 1
	 fi
      fi
   fi


# Do it

   logiso_get "$MOUNT_PATH" | cut -f1 | sort -n | uniq >  "$INODE_LIST"
   rm -f "$ISOFS_UTIL_DIR/last_inodes"  2> /dev/null
   cp "$INODE_LIST" "$ISOFS_UTIL_DIR/last_inodes"

# If CD_FILES does not exist, create it.

   USE_GZIP="FALSE"
   if [ -f "${CD_FILES}.gz" -a ! -f "$CD_FILES" ]
   then
      USE_GZIP="TRUE"
      echo "Uncompressing $CD_FILES"
      gunzip < "${CD_FILES}.gz" > "$CD_FILES"
   fi
   if [ ! -f "$CD_FILES" ]
   then
      echo "$CD_FILES does not exist."
      echo "Creating it will take a while."
      find  "$MOUNT_PATH" 2> /dev/null | xargs ls -i -d 2> /dev/null 	\
	 | sort -n > "$CD_FILES" 2> /dev/null
      check_return 0 1 "Error making $CD_FILE"
      echo "You should probably clear the log and try again." >&2
      exit 1
   fi

# Compare logged files against cd_files.

   echo "Comparing logged inodes against list in $CD_FILES"
   process_lists "$INODE_LIST" "$CD_FILES" > "$INSTALL_LIST"

   if [ "$COMMAND" = "logiso_copy" ]
   then
      ##echo logiso_copy: do install
      cp "$INSTALL_LIST" "$ISOFS_UTIL_DIR/last_install"
      ## echo logiso_copy: install_list -d "$CONFIG_PREFIX" "$INSTALL_LIST" "$MOUNT_PATH" "$MAP_TO_PATH"
      install_list -d "$CONFIG_PREFIX" "$INSTALL_LIST" "$MOUNT_PATH" "$MAP_TO_PATH"
   else
      ##echo logiso_copy: do preview
      cat "$INSTALL_LIST"
   fi   

# If we uncompressed the cd_files, file, remove the uncompressed copy.

   if [ "$USE_GZIP" = "TRUE" ]
   then
      rm -f "$CD_FILES"
   fi
