#!/bin/bash

function add_groups() {
    # Add the DT groups.
    for GROUP in dtadmin dtmon; do
        if [ -z "$(/usr/bin/getent group "$GROUP")" ]; then
            /usr/sbin/groupadd "$GROUP"
        fi
    done

    # Make root the default dtadmin
    if [ -n "$(/usr/bin/getent passwd root)" ]; then
        if [ -f "/etc/redhat-release" ]; then
            /usr/sbin/usermod -a -G dtadmin root
        elif [ -f "/etc/debian_version" ]; then
            /usr/sbin/usermod -a -G dtadmin root
        elif grep -q "VERSION = 9" /etc/SuSE-release; then
            /usr/sbin/groupmod -A root dtadmin
        else
            /usr/sbin/usermod -A dtadmin root
        fi
    fi
}

function del_groups() {
    for GROUP in dtadmin dtmon; do
        if [ -n "$(/usr/bin/getent group "$GROUP")" ]; then
            /usr/sbin/groupdel "$GROUP"
        fi
    done
}

function install() {
    # Create OEM symlinks.
    if [ -e /usr/share/groff/site-tmac/HP-branding.tmac ]; then
        ln -sf /usr/share/groff/site-tmac/HP-branding.tmac \
            /usr/share/groff/site-tmac/DT-branding.tmac
    fi
    ln -sf /usr/lib/${oem_install_directory}/libRSResource.so \
        $libdir/libRSResource.so
    ln -sf /etc/${oem_install_directory}/bin/DTBrand.sh /etc/DTBrand.sh

    # Could we do more of this in the rpm itself?
    # Creating symlinks for files at the original location
    ln -s -f /etc/${oem_install_directory}/bin/DTRegisterFS /usr/bin/DTRegisterFS
    ln -s -f /etc/${oem_install_directory}/bin/DTRepWait /usr/bin/DTRepWait
    ln -s -f /etc/${oem_install_directory}/bin/DTPivotRoot /usr/bin/DTPivotRoot
    ln -s -f /etc/${oem_install_directory}/bin/DTInit.sh /usr/bin/DTInit.sh
    cp /usr/bin/diff /etc/${oem_install_directory}/bin
    cp /usr/bin/head /etc/${oem_install_directory}/bin

    # Register the service.
    if [ -f "/etc/redhat-release" ]; then
        /sbin/chkconfig --add ${servicename}Mount
        /sbin/chkconfig --add ${servicename}
    else
        update-rc.d DTMount defaults 80 20
        update-rc.d DT defaults 80 20
    fi

    add_groups

    mkdir -p /var/lock/subsys
}

function remove() {
    /etc/init.d/${servicename} stop >/dev/null 2>&1
    /etc/init.d/${servicename}Mount stop >/dev/null 2>&1

    # Scrub the kernel directories of copies of the drivers
    rm -f `find /lib/modules -name "dtfs.ko" -print | grep -v "${oem_install_directory}"`
    rm -f `find /lib/modules -name "dtrep.ko" -print | grep -v "${oem_install_directory}"`
    rm -f `find /lib/modules -name "dtloop.ko" -print | grep -v "${oem_install_directory}"`

    # Unregister the service.
    if [ -f "/etc/redhat-release" ]; then
        /sbin/chkconfig --del ${servicename}Mount
        /sbin/chkconfig --del ${servicename}
        if chkconfig --list | grep -q boot.dtfs; then
            /sbin/chkconfig --del boot.dtfs
        fi
    else
        update-rc.d -f DTMount remove
        update-rc.d -f DT remove
    fi

    del_groups

    # Remove OEM symlinks.
    rm -f /usr/share/groff/site-tmac/DT-branding.tmac
    rm -f $libdir/libRSResource.so
    rm -f /etc/DTBrand.sh

    # Remove the SoftLinks file created
    /bin/rm -f /usr/bin/DT*

    # Update module dependencies to reflect removal
    # of dtfs, dtrep, and dtloop.
    /sbin/depmod -a

    rm -rf /var/log/subsys
}

RPM_INSTALL_PREFIX=
export RPM_INSTALL_PREFIX
servicename=DT
oem_install_directory=DT
libdir=/usr/lib
PROC_MOUNTS="/proc/mounts"

op="$1"

if [ -z "$op" ]; then
    echo >&2 "usage: $0 [install|remove]"
    exit 1
fi


case "$op" in
    install)
        install
        ;;
    remove)
        remove
        ;;
esac

