#!/bin/sh

# Open-MX
# Copyright © inria 2007-2010 (see AUTHORS file)
#
# The development of this software has been funded by Myricom, Inc.
#
# 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 in COPYING.GPL for more details.

prefix=/usr/open-mx
OMX_PREFIX=/usr/open-mx
OMX_CONFIG_DIR=${prefix}/etc/open-mx
OMX_WIDE_CONFIG_DIR=/etc/open-mx
OMX_INIT_SCRIPT_DIR=/etc/rc.d/init.d

OMX_INIT_SCRIPT_STUB=${OMX_PREFIX}/sbin/omx_init
OMX_INIT_SCRIPT_DEST=${OMX_INIT_SCRIPT_DIR}/open-mx

OMX_CONFIG_STUB=${OMX_CONFIG_DIR}/open-mx.conf
OMX_CONFIG_DEST=${OMX_WIDE_CONFIG_DIR}/open-mx.conf

UDEV=1
UDEV_RULES_DIR=/etc/udev/rules.d
UDEV_RULES_FILE=${OMX_CONFIG_DIR}/10-open-mx.rules
UDEV_RULES_DEST=${UDEV_RULES_DIR}/10-open-mx.rules

set -e

if test x"${DESTDIR}" != x; then
    echo "Using ${DESTDIR} as the file-system root"
fi

if test ! -d ${DESTDIR}${OMX_PREFIX}; then
    echo "Something bad happened with the install script"
    echo "OMX_PREFIX isn't pointing to a valid directory"
    exit 1
fi

DATE=$(date +%Y%m%d.%H%M%S)

cmp_mv_old_and_install_new() {
    src="$1" ; shift
    dest="$1"; shift
    installname="$1" ; shift

    if test -f "${DESTDIR}$dest" ; then
	# if no difference, do nothing
	if cmp -s "${DESTDIR}$dest" "${DESTDIR}$src" ; then
	    echo "  File $dest is unchanged"
	    return
	fi

	# move the old file away
	echo "  Saving the old $dest into ${dest}.${DATE}..."
	mv -f "${DESTDIR}$dest" "${DESTDIR}${dest}.${DATE}"
    fi

    echo "  Installing $installname $dest..."
    cp -f "${DESTDIR}$src" "${DESTDIR}$dest"
}

cmp_mv_old_and_remove() {
    src="$1" ; shift
    dest="$1"; shift
    installname="$1" ; shift

    if test ! -f "${DESTDIR}$dest" ; then
	echo "  File $dest not found"
	return
    fi

    # if no difference, just remove
    if cmp -s "${DESTDIR}$dest" "${DESTDIR}$src" ; then
	echo "  File $dest is unchanged, removing..."
	rm -f "${DESTDIR}$dest"
	return
    fi

    # move the old file away
    echo "  Saving the old $dest into ${dest}.${DATE}..."
    mv -f "${DESTDIR}$dest" "${DESTDIR}${dest}.${DATE}"
}

if test x"$1" != "x--uninstall"; then
    # installing

    echo "Installing startup script..."
    if test ! -d ${DESTDIR}${OMX_INIT_SCRIPT_DIR} ; then
	echo "  Creating ${OMX_INIT_SCRIPT_DIR} directory..."
	mkdir -p ${DESTDIR}${OMX_INIT_SCRIPT_DIR}
    fi
    cmp_mv_old_and_install_new "${OMX_INIT_SCRIPT_STUB}" "${OMX_INIT_SCRIPT_DEST}" "startup script"

    echo "Installing config file..."
    if test ! -d ${DESTDIR}${OMX_WIDE_CONFIG_DIR} ; then
	echo "  Creating ${OMX_WIDE_CONFIG_DIR} directory..."
	mkdir -p ${DESTDIR}${OMX_WIDE_CONFIG_DIR}
    fi
    cmp_mv_old_and_install_new "${OMX_CONFIG_STUB}" "${OMX_CONFIG_DEST}" "stub config"

    if test x$UDEV = x1; then
	echo "Installing udev rules file..."
	if test -d ${DESTDIR}${UDEV_RULES_DIR} ; then
	    cmp_mv_old_and_install_new "${UDEV_RULES_FILE}" "${UDEV_RULES_DEST}" "udev rules"
	else
	    echo "  Udev rules directory not found (UDEV_RULES_DIR=$UDEV_RULES_DIR)."
	fi
    else
	echo "Udev support disabled, not installing rules file."
    fi

else
    # uninstalling

    echo "Uninstalling startup script..."
    cmp_mv_old_and_remove "${OMX_INIT_SCRIPT_STUB}" "${OMX_INIT_SCRIPT_DEST}" "startup script"

    echo "Uninstalling config file..."
    cmp_mv_old_and_remove "${OMX_CONFIG_STUB}" "${OMX_CONFIG_DEST}" "config"
    test -d "${DESTDIR}${OMX_WIDE_CONFIG_DIR}" && ( rmdir "${DESTDIR}${OMX_WIDE_CONFIG_DIR}" 2> /dev/null || echo "  ${OMX_WIDE_CONFIG_DIR} not empty, not removing" )

    if test x$UDEV = x1; then
	echo "Uninstalling udev rules file..."
	cmp_mv_old_and_remove "${UDEV_RULES_FILE}" "${UDEV_RULES_DEST}" "udev rules"
    else
	echo "Udev support disabled, not removing rules file."
    fi
fi
