#!/bin/sh

ILOGDB="/etc/ilog"

errormsg_groupadd_failed_abort(){
cat <<EOTXT
Adding a group "ilog" to your system failed. Aborting ilog installation.

Please create a group "ilog" manually (e.g., using groupadd)"
and run "dpkg --pending --configure" to restart the ilog configuration.

EOTXT
exit 1
}

echo -e "Now performing post-install configuration of the" \
        "ilog system documentaion tool.\n"

if [ -n "$(perl -e 'print scalar getgrnam("ilog")')" ] ; then
    echo "Group \"ilog\" already exists. good."
else
    echo "Group \"ilog\" does not exist, trying to make one..."
    GID=51;
    while [ -n "$(perl -e "print scalar getgrgid($GID)")" ] ; do
        GID=$[$GID+1]
    done
    if [ $GID -gt 99 ] ; then
        echo "No more system GID (<100) free!. Aborting."
	errormsg_groupadd_failed_abort
    fi
    echo "GID #$GID seems to be unused. taking this one."
    groupadd -g $GID ilog || errormsg_groupadd_failed_abort
fi

# in theory, we should have our ilog group by now. but let's check again...
if [ -z "$(perl -e 'print scalar getgrnam("ilog")')" ] ; then
    errormsg_groupadd_failed_abort
fi


if [ -e "$ILOGDB" ] ; then
    if [ -d "$ILOGDB" ] ; then
        echo "Directory $ILOGDB already exists. good."
    else
        echo "$ILOGDB already exits, but is not a directory."
	echo -n "Please remove/rename $ILOGDB and run "
	echo "\"dpkg --pending --configure\" to restart the configuration."
	echo "Aborting the ilog configuration."
	exit 1
    fi
else
    mkdir -p "$ILOGDB" && \
    chmod 0750 "$ILOGDB" && \
    ( chown root:ilog "$ILOGDB" || \
		( echo "Making a Directory $ILOGDB with ownership root:ilog" ; \
		  echo "and permissions 0750 failed. Please check why. Abort." ; \
		  exit 1 ) ) && \
	cp /dev/null "$ILOGDB/installed.all" && \
    chmod 0640 "$ILOGDB/installed.all" && \
    ( chown root:ilog "$ILOGDB/installed.all" || \
		( echo "Making a File $ILOGDB/installed.all with ownership root:ilog" ; \
		  echo "and permissions 0640 failed. Please check why. Abort." ; \
		  exit 1 ) )
fi

echo "ilog post-install configuration successful."


