#! /bin/bash

# $Id$
#*********************************************************************
#
# fai-divert -- add or remove a diversion for fai
#
# This script is part of FAI (Fully Automatic Installation)
# (c) 2002-2007 by Thomas Lange, lange@informatik.uni-koeln.de
# Universitaet zu Koeln
#
#*********************************************************************
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
# 
# A copy of the GNU General Public License is available as
# `/usr/share/common-licences/GPL' in the Debian GNU/Linux distribution
# or on the World Wide Web at http://www.gnu.org/copyleft/gpl.html.  You
# can also obtain it by writing to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
#*********************************************************************

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
mkdivert() {

    # make a diversion of a file
    [ "$debug" ] || local divertquiet=--quiet
    $ROOTCMD dpkg-divert $divertquiet --package fai --rename --add $1 &&
    cat > $FAI_ROOT/$1 <<-EOF
	#! /bin/sh
	# diversion of $1 created by FAI
	exit 0
EOF
    chmod a+rx $FAI_ROOT/$1
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
adddivert() {

    # add an executable to the list of diversions
    local item
    for item in "$@"; do
	mkdivert $item
    done
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
rmdivert() {

    # remove diversion
    [ "$debug" ] || local divertquiet=--quiet
    rm -f $FAI_ROOT/$1
    $ROOTCMD dpkg-divert $divertquiet --package fai --rename --remove $1
    # when a diversion was made before the file exists
    [ -f $FAI_ROOT/$1.distrib.dpkg-new ] && mv $FAI_ROOT/$1.distrib.dpkg-new $FAI_ROOT/$1
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
rmalldivert() {

    # remove all diversions made by fai
    local item
    for item in $($ROOTCMD dpkg-divert --list fai | awk '{ print $3 }'); do
	rmdivert $item
    done
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
usage() {

    cat <<EOF
fai-divert, add or remove a diversion for fai.

   Copyright (C) 2002-2007 by Thomas Lange

Usage: fai-divert [OPTION] ... FILE

   -a FILE ...          Replace each file by a dummy script.
   -R			Remove all diversion made by fai.
   -r FILE              Remove a diversion.
   -v                   Be verbose.

Report bugs to <lange@informatik.uni-koeln.de>.
EOF
exit 0
}

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

[ "$1" ] || usage

# do not execute if command is not available
[ -f $FAI_ROOT/usr/sbin/dpkg-divert ] || {
    echo "$FAI_ROOT/usr/sbin/dpkg-divert not available. Skipping."
    exit 1
}

while getopts arRhv opt ; do
        case "$opt" in
        v) verbose=1 ;;
        R) rmalldivert ;;
        r) shift ; rmdivert $1 ;;
        a) shift ; adddivert $@ ;;
        h) usage ;;
        esac
done

