#!/bin/sh -e
# rkinstall: a KerGIS tools script to install files on a host, following
# a describes policy about the actions, the owners, the permissions...
#
# $Id: rkinstall,v 1.3 2007/03/22 16:51:32 tlaronde Exp $
#
#  Copyright 2004 Thierry LARONDE <tlaronde@polynum.com>
#  All rights reserved. 
#  
#  This work is under the KerGIS Public Licence v1.0
# 
#  See the COPYRIGHT file at the root of the source directory or see
#  http://www.kergis.org/licence.txt for complete information.
# 
# !!!THIS SOFTWARE IS PROVIDED ``AS IS'' WITHOUT ANY WARRANTIES!!! 
#                      USE IT AT YOUR OWN RISK 


#========== LIBRARY and INIT

. install_bin/libsh

#========== GLOBALS

prog_version='$Id: rkinstall,v 1.3 2007/03/22 16:51:32 tlaronde Exp $'

usage="
	$PROGRAM [-d] [-h] [-V] 
Install a set of files according to a .rkcomp/map policy.
This program must be run from the directory where the files exist.
It is normally included in the tarball

Options:
 -d	set Debug on
 -h	display this Help and exit
 -V	display Version information and exit
"

version="$prog_version
Written by Thierry Laronde.

Copyright (c) 2004 Thierry Laronde <tlaronde@polynum.org>

All rights reserved. KerGIS Public Licence v1.0. NO WARRANTIES!."


#========== SUBFUNCTIONS

error ()
{
	case $1 in
	  option) debug E "This option is unknown!";
		debug USAGE "$usage";;
	esac

	clean_tmp
	exit 1
}

#=========== PROCESSING

debug_mode=NO

# options to pass to the {pre|post}-install programs
#
prog_options=
while [ $# -gt 0 ]; do
	case "$1" in
	  -d) set -x; prog_options="-d"; debug_mode=YES;;
	  -h|--help) echo "$usage"; exit 0;;
	  -V|--version) echo "$version"; exit 0;;
	  *) error option ;;
	esac
shift	
done

#========== INITIAL CHECKS
# Allow a more useful message to user about the need to set
# LD_LIBRARY_PATH.
#
SET_RPATH=
[ ! -f install_data/rkinstall.cf ] || . install_data/rkinstall.cf

if [ -f install_bin/pre-install ]; then
	debug L "Running the provided pre-install program"
	sh ./install_bin/pre-install $prog_options
fi

debug L "Installing the required stuff"
tar -cf - -C ./installed . | tar xvf - -C /

debug L "Setting ownership and permissions"
while read type pathname owner mods; do
	chown_flags=
	[ "$type" != "l" ] || chown_flags="-h"
	[ "$owner" = '*' ] || chown $chown_flags $owner "$pathname"
	[ "$mods" = '*' ] || chmod $mods "$pathname"
done < installed.list

debug L "Handling the preserved files"
for file in `sed 's/ /|/g' preserved.list`; do
	type=$(echo $file | cut -d '|' -f 1)
	pathname=$(echo $file | cut -d '|' -f 2)
	owner=$(echo $file | cut -d '|' -f 3)
	mods=$(echo $file | cut -d '|' -f 4)
	ans=
	if [ -e "$pathname" ]; then
	  # if just the same, skip
	  ! cmp preserved$pathname $pathname 1>/dev/null 2>&1 \
		|| { debug L "$path hasn't changed. Skipping..."; continue;}

	  debug L "===================================================="
	  debug L "$pathname already exists. What do you want me to do?"
	  while [ "x$ans" != "xS" ]; do
	  debug L ""
	  debug L "  S	Skip this file"
	  debug L "  D	show Differences between files"
	  debug L "  I	Install this new version"
	  debug L ""
	  echo "Your choice: " | tr -d '\n' 1>&2
	  read ans
	    case $ans in
		  D) case $type in
		    d) debug L "Infos about existing dir $pathname"
		      ls -l "$pathname"
		      debug L "Version to be installed:"
		      ls -l preserved/."$pathname";;
		    f) if diff -v 1>/dev/null 2>&1; then 
			    diff -u "$pathname" preserved"$pathname" || :
			  else
			    debug L "Program \`diff' not found on the host"
			  fi;;
		    *) debug L "Unimplemented diff for type $type";;
		    esac;;
		  I|S) break;; 
	      *) ans=;;
	    esac
	  done
 	else
 	  debug L "$pathname not existing, installing"
	fi
	[ "x$ans" != "xS" ] || continue
	case $type in
	  d) mkdir -p "$pathname" \
	   || { debug E "Unable to mkdir $pathname!  Skipping..."; continue; } ;;
	  f) cp preserved"$pathname" "$pathname" \
	   || { debug E "Unable to move $pathname to its place! Skipping..."; continue;};;
	esac
	[ "$owner" = "*" ] || chown $owner "$pathname"
	[ "$mods" = "*" ] || chmod $mods "$pathname"
done 

paths_needed=
debug L "Verifying the presence of required third parties files"
while read type pathname owner mods; do
	if [ -e "$pathname" ]; then
	  paths_needed="$path_needed:$(echo $pathname | sed -n 's@^\(.*\)/lib[^/]*$@\1@p'):"
	  debug L "$pathname OK found"
	else
	  debug W "$pathname not found!"
	fi
done < verified.list

# Inform user about LD_LIBRARY_PATH if needed.
#
if [ "x$SET_RPATH" != "xYES" ]; then
	# suppress /lib and /usr/lib
	paths_needed=$(echo $paths_needed | sed -e 's@:/lib:@@' -e 's@:/usr/lib:@@')
	paths_needed=$(echo $paths_needed | tr ':' ' ' | sed 's/^ *//')
	[ "x$paths_needed" = "x" ] \
	  || {
	  debug L ""
	  debug L "No information was available to encode rpath in the objects"
	  debug L "Hence you probably need to add the following paths to LD_LIBRARY_PATH:"
	  for path in $paths_needed; do
		debug L "	$path"
	  done
	  }
fi

if [ -f install_bin/post-install ]; then
	debug L "Running the provided post-install program"
	sh ./install_bin/post-install $prog_options
fi

#------------------POST PROCESSING

clean_tmp

debug L "Finished processing"

exit 0
