#!/bin/sh

install -d ${DESTDIR}${ETCDIR}
install -d ${DESTDIR}${LOGDIR}

# Start by detecting if systemd is used
(which systemctl && systemctl list-units) >/dev/null 2>&1
if [ $? = 0 ]; then
    install -d ${DESTDIR}${ETCDIR}/systemd/system/
        sed -e "s;%prefix%;${PREFIX};g" \
            -e "s;%etcdir%;${ETCDIR};g" \
            -e "s;%bindir%;${BINDIR};g" \
        systemd/yaws.service > ${DESTDIR}${ETCDIR}/systemd/system/yaws.service
    exit 0
fi

# Else, looking at distro-specific files
os=$( uname -s )
which lsb_release >/dev/null 2>&1
if [ $? = 0 ]; then
    os=$( lsb_release -si )
elif [ -f /etc/os-release ]; then
    . /etc/os-release
    os=${ID}
elif [ -f /etc/arch-release ]; then
    os="arch"
elif [ -f /etc/gentoo-release ]; then
    os="gentoo"
elif [ -f /etc/fedora-release ]; then
    os="fedora"
elif [ -f /etc/centos-release ]; then
    os="centos"
elif [ -f /etc/redhat-release ]; then
    os="redhat"
elif [ -f /etc/debian_version ]; then
    os="debian"
elif [ -f /etc/SuSE-release ]; then
    os="suse"
elif [ $os = "Darwin" -a $(id -u) = 0 ]; then
    os="darwin"
elif [ $os = "FreeBSD" ]; then
    os="freebsd"
elif [ $os = "NetBSD" ]; then
    os="netbsd"
else
    os="Unknown"
fi

case $(printf $os | tr '[:upper:]' '[:lower:]') in
    debian | ubuntu)
        install -d ${DESTDIR}${ETCDIR}/init.d
        sed -e "s;%prefix%;${PREFIX};g" \
            -e "s;%etcdir%;${ETCDIR};g" \
            -e "s;%bindir%;${BINDIR};g" \
            debian/yaws.init >  ${DESTDIR}${ETCDIR}/init.d/yaws
        chmod +x ${DESTDIR}${ETCDIR}/init.d/yaws
        ;;

    gentoo)
        # seems gentoo don't like to be installed in /usr/local/etc since #
        # /sbin/runscript still reads /etc/conf.d
        install -d ${DESTDIR}${ETCDIR}/init.d
        install -d ${DESTDIR}${ETCDIR}/conf.d
        sed -e "s;%prefix%;${PREFIX};g" \
            -e "s;%etcdir%;${ETCDIR};g" \
            -e "s;%bindir%;${BINDIR};g" \
            gentoo/yaws.init > ${DESTDIR}${ETCDIR}/init.d/yaws
        chmod +x ${DESTDIR}${ETCDIR}/init.d/yaws
        ;;

    redhat | fedora | centos)
        install -d ${DESTDIR}${ETCDIR}/init.d
        sed -e "s;%prefix%;${PREFIX};g" \
            -e "s;%etcdir%;${ETCDIR};g" \
            -e "s;%bindir%;${BINDIR};g" \
            redhat/yaws.init > ${DESTDIR}${ETCDIR}/init.d/yaws
        chmod +x ${DESTDIR}${ETCDIR}/init.d/yaws
        ;;

    *suse*)
        install -d ${DESTDIR}${ETCDIR}/init.d
        sed -e "s;%prefix%;${PREFIX};g" \
            -e "s;%etcdir%;${ETCDIR};g" \
            -e "s;%bindir%;${BINDIR};g" \
            suse/yaws.init > ${DESTDIR}${ETCDIR}/init.d/yaws
        chmod +x ${DESTDIR}${ETCDIR}/init.d/yaws
        ;;


    darwin)
        startupdir="/Library/StartupItems/Yaws"
        if [ ! -e ${startupdir} ]; then
            mkdir ${startupdir};
        elif [ ! -d ${startupdir} ]; then
            echo "${startupdir} exists but is not a directory, bailing out ..."
            exit 1
        fi
        sed -e "s;%prefix%;${PREFIX};g" \
            -e "s;%etcdir%;${ETCDIR};g" \
            -e "s;%bindir%;${BINDIR};g" \
            darwin/Yaws.StartupItem > ${startupdir}/Yaws
        chmod +x ${startupdir}/Yaws
        cp darwin/Yaws.plist ${startupdir}/StartupParameters.plist
        # MacOS is particular about the ownership of startup items.
        chown -R root:wheel ${startupdir}
        ;;

    freebsd)
        install -d ${DESTDIR}${ETCDIR}/rc.d
        sed -e "s;%prefix%;${PREFIX};g" \
            -e "s;%etcdir%;${ETCDIR};g" \
            -e "s;%bindir%;${BINDIR};g" \
            freebsd/yaws > ${DESTDIR}${ETCDIR}/rc.d/yaws
        chmod +x ${DESTDIR}${ETCDIR}/rc.d/yaws
        ;;

    netbsd)
        sed -e "s;%prefix%;${PREFIX};g" \
            -e "s;%etcdir%;${ETCDIR};g" \
            -e "s;%bindir%;${BINDIR};g" \
            netbsd/yaws.sh > ${DESTDIR}${ETCDIR}/rc.d/yaws
        chmod +x /etc/rc.d/yaws
        ;;
    *)
        install -d ${DESTDIR}${ETCDIR}
        echo "Don't know how to make /etc/init scripts for this system"
        echo "possibly add ${PREFIX}/bin/yaws --daemon --heart to your /etc/rc.local manually"
        ;;
esac


exit 0
