#!/bin/sh
#
#   install-config
#   einit
#
#   Created by Magnus Deininger on 30/05/2007.
#   Copyright 2007 Magnus Deininger. All rights reserved.
#
# Copyright (c) 2007, Magnus Deininger
# All rights reserved.

# Redistribution and use in source and binary forms, with or without modification,
# are permitted provided that the following conditions are met:
# 
#     * Redistributions of source code must retain the above copyright notice,
# 	  this list of conditions and the following disclaimer.
#     * Redistributions in binary form must reproduce the above copyright notice,
# 	  this list of conditions and the following disclaimer in the documentation
# 	  and/or other materials provided with the distribution.
#     * Neither the name of the project nor the names of its contributors may be
# 	  used to endorse or promote products derived from this software without
# 	  specific prior written permission.
# 
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
# ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
# ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#

if [ "$(readlink /bin/sh)" != "dash" ]; then
alias echo="echo -e"
fi

. $(dirname $0)/configuration

file=$1
fileshort=$(basename $1)
subsystem=$2
method=$3

echo "installing $file to $DESTDIR/$ETCDIR/${subsystem}/${fileshort}";

if [ "${method}" = "ebuild" ]; then
# gentoo's ebuilds do internal handling for configuration updates, no need to
# mess with diff and patch ourselves
 if [ -e "$ETCDIR/${subsystem}/${fileshort}" ]; then
  if [ -n "$(diff3 -e "$ETCDIR/${subsystem}/${fileshort}" "$LIBDIR/defaults/${subsystem}/${fileshort}" ${file})" ]; then
   cp -f ${file} "$DESTDIR/$ETCDIR/${subsystem}/${fileshort}";
  else
   cp -f "$ETCDIR/${subsystem}/${fileshort}" "$DESTDIR/$ETCDIR/${subsystem}/${fileshort}";
  fi
 else
  cp -f ${file} "$DESTDIR/$ETCDIR/${subsystem}/${fileshort}";
 fi
 cp -f ${file} "$DESTDIR/$LIBDIR/defaults/${subsystem}/${fileshort}";
else
# non-ebuild methods need patching, so we do that here
 if [ -e "$ETCDIR/${subsystem}/${fileshort}" ]; then
  if [ -n "$(diff3 -e "$ETCDIR/${subsystem}/${fileshort}" "$DESTDIR/$LIBDIR/defaults/${subsystem}/${fileshort}" ${file})" ]; then
   echo " ** file needs update, merging and creating backup in $DESTDIR/$ETCDIR/backups/${subsystem}/${fileshort}";

   mkdir -p "$DESTDIR/$ETCDIR/backups/${subsystem}";
   cp -f "$ETCDIR/${subsystem}/${fileshort}" "$DESTDIR/$ETCDIR/backups/${subsystem}/${fileshort}";

#   diff --unified "$DESTDIR/$LIBDIR/defaults/${subsystem}/${fileshort}" ${file};

   diff --unified "$DESTDIR/$LIBDIR/defaults/${subsystem}/${fileshort}" ${file} | patch -u "$DESTDIR/$ETCDIR/backups/${subsystem}/${fileshort}" -o "$DESTDIR/$ETCDIR/${subsystem}/${fileshort}";
  fi;
 else
#  cp -f "$ETCDIR/${subsystem}/${fileshort}" "$DESTDIR/$ETCDIR/${subsystem}/${fileshort}";
  cp -f "${file}" "$DESTDIR/$ETCDIR/${subsystem}/${fileshort}";
 fi;

# defaults-file must always be the most recent
 cp -f ${file} "$DESTDIR/$LIBDIR/defaults/${subsystem}/${fileshort}";
fi;
