#!/bin/sh

#This script installs/uninstalls the Open Ldap Utilities. 

test `uname -s` = Linux && echocmd="echo -e" || echocmd="echo"

#
# Detect if we have been called from a Fixpack installation.
#
FIXPACK_INSTALL=n
if [ "$1" = "Fixpack_Install" ]; then
   FIXPACK_INSTALL=y
fi

yorn ()
{
        YN=b
        while [ "$YN" != "n" -a "$YN" != "no" -a \
                "$YN" != "N" -a "$YN" != "NO" -a \
                "$YN" != "y" -a "$YN" != "yes" -a \
                "$YN" != "Y" -a "$YN" != "YES" -a \
                "$YN" != "Yes" -a "$YN" != "No" ]; do
                $echocmd $1
                read YN
                if [ "$YN" = "y" -o "$YN" = "yes" -o "$YN" = "Yes" -o \
                     "$YN" = "Y" -o "$YN" = "YES" ]; then
                        return 1
                fi
                if [ "$YN" = "n" -o "$YN" = "no" -o "$YN" = "No" -o \
                     "$YN" = "N" -o "$YN" = "NO" ]; then
                        return 0
                fi
                $echocmd "Please enter y or n"
        done
}

cont ()
{
        yorn "Do you want to continue (y/n): \c"
        res=$?
        if [ "$res" != "1" ]; then
                exit 0
        fi
}

install_ldap()
{
  yorn "Do you wish to install the Open Ldap utilities now? (y/n): \c"
  res=$?
  if [ "$res" != "1" ]; then
    exit 0
  fi
  cd $COBDIR/lib/openldap
  ln -f * .. && cd ../../bin/openldap && ln -f * .. 
  if test $? -eq 0; then
    $echocmd "Open Ldap utilities have been installed."
  else
    $echocmd "$0: error installing Ldap utilities."
  fi
}
uninstall_ldap()
{
  yorn "Do you wish to uninstall the Open Ldap utilities now? (y/n): \c"
  res=$?
  if [ "$res" != "1" ]; then
    exit 0
  fi
  cd $COBDIR/lib/openldap
  for file in *
  do
    rm -f ../$file
  done
  res=$?
  cd ../../bin/openldap
  for file in *
  do
    rm -f ../$file
  done
  if test $? -eq 0 -a $res -eq 0; then
    $echocmd "Open Ldap utilities have been uninstalled."
  else
    $echocmd "$0: error uninstalling Open Ldap Utilities."
    exit 1
  fi
}
if test -z "$COBDIR"
then
  echo "\$COBDIR must be set."
  exit 1
fi
if test ! -w /etc/passwd
then
  echo "This script must be run as root user."
  exit 1
fi
if [ "$FIXPACK_INSTALL" = "n" ]; then
if test ! -f $COBDIR/bin/ldapadd
then
  install_ldap
else
  uninstall_ldap
fi
else
  install_ldap
fi
