#!/usr/bin/env bash
#
# Copyright (C) 2003 VA Linux Systems Japan, K.K. All rights reserved.
#
# LICENSE NOTICE
#
#  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.
#

# Usage:
#    update-ultrapossum [-f] <configure|remove|sanity|reconfigure>
#

# $Id: update-ultrapossum,v 1.20 2004/10/07 07:18:46 taru Exp $

: <<POD
=head1 NAME

update-ultrapossum - configure/remove ultrapossum plugins

=head1 SYNOPSIS

B<update-ultrapossum> [-f] <configure|reconfigure|sanity|remove>

=head1 DESCRIPTION

B<update-ultrapossum> manages UltraPossum plugins. There are 4
arguments. B<configure> configures all the plugins. B<reconfigure>
reconfigures the plugins again which have been already configured.
B<sanity> checks whether your configuration is right and B<remove>
removes the plugins from the system management.

B<configure> tries to configure all the plugins which are unpacked
in your system, while B<reconfigure> doesn't. So, you can use
B<configure> argument to install new plugin. B<remove> removes them
only from the management. So every files have been located yet. You
can remove these files successfully after calling B<remove>.

=head1 OPTIONS

=over 4

=item B<-f>

forcibly configures UltraPossum plugins again even if all plugins
are up-to-date.

=cut
POD

set -e

eval `ultrapossum-config init`
trap "eval `ultrapossum-config term`" 0

configure_module() {
  for module in $@
  do
    v="ULTRAPOSSUM_MODULE_`echo $module | tr a-z- A-Z_`"
    if test "x$reconfig" = "x" || test "${!v}" = "installed"; then
      $UPDATEDIR/update-$module configure
    fi
  done
}

remove_module() {
  ultrapossum-config module | while read f
  do
    $UPDATEDIR/update-$f remove
  done
}

sanity_module() {
  for f in $@
  do
    v="ULTRAPOSSUM_MODULE_`echo $f | tr a-z- A-Z_`"
    if test "${!v}" = "installed"; then
      $UPDATEDIR/update-$f sanity
    fi
  done
}

up_to_date() {

  if ! test -f "$CONFSTATUS"; then
    echo "status change detected" 1>&2
    return 1
  fi
  if test "x$@" = "x"; then
    if test "$SLAPDCONF" -nt "$CONFSTATUS"; then
      echo "$SLAPDCONF is newer than current status" 1>&2
      return 1
    fi
    find $SYSCONFDIR | while read f
    do
      if test "$f" -nt "$CONFSTATUS"; then
        echo "$f is newer than current status" 1>&2
        return 1
      fi
    done || return 1
  fi

  return 0
}

check_configuring_module() {
  modules=`ultrapossum-config module`
  if ! up_to_date; then
    echo $modules
  else
    for module in $modules
    do
      getvalues $conftemp "`ultrapossum-config variable $module`" > $conftemp.$$
      getvalues $CONFSTATUS "`ultrapossum-config variable $module`" > $conftemp.$$2 
      if diff -c $conftemp.$$ $conftemp.$$2 | egrep "^\! [^+]" | cut -d' ' -f2- | cut -d= -f1 | sort | uniq | egrep . > /dev/null; then
        echo " $module"
      fi
      /bin/rm -f $conftemp.$$ $conftemp.$$2
    done
  fi
}

configure() {
  conftemp=`tempfile`
  chmod 640 $conftemp
  # little evil
  getconfig | grep -v SLAPROOTPW | grep -v CHROOTFILES > $conftemp
  module=`check_configuring_module`
  if test "x$module" != "x"; then
    sanity_module $module
    install -d $CONFDIR

    configure_module $module

    /bin/cp -p $conftemp $CONFSTATUS
    cat $conftemp > $CONFSTATUS
  else
    echo "UltraPossum already configured" 1>&2
  fi
  /bin/rm -f $conftemp

}

remove() {
  remove_module
  $SHAREDIR/sshman unauth
}

if test "x$1" = "x-f"; then
  /bin/rm -f $CONFSTATUS
  shift
fi

case "x$1" in
	xconfigure)
		configure
	;;
	xreconfigure)
		reconfig=1
		configure
	;;
	xsanity)
		sanity_module
	;;
	xremove)
		remove
	;;
	x)
		echo "Usage: $0 <configure|reconfigure|sanity|remove>" 1>&2
		exit 1
	;;
	x*)
		echo "Unknown argument: $1" 1>&2
		exit 1
	;;
esac

