#!/bin/sh

DESTDIR=${DESTDIR:="/"}

BINDIR=$DESTDIR/usr/bin
DOCDIR=$DESTDIR/usr/share/doc/collectl
SHRDIR=$DESTDIR/usr/share/collectl
MANDIR=$DESTDIR/usr/share/man/man1
SYSDDIR=$DESTDIR/usr/lib/systemd/system
ETCDIR=$DESTDIR/etc
INITDIR=$ETCDIR/init.d

mkdir -p $BINDIR
mkdir -p $DOCDIR
mkdir -p $SHRDIR
mkdir -p $ETCDIR
mkdir -p $MANDIR
mkdir -p $INITDIR
mkdir -p $SHRDIR/util
mkdir -p $DESTDIR/var/log/collectl

cp collectl colmux       $BINDIR
cp collectl.conf         $ETCDIR
cp man1/*                $MANDIR
cp initd/*               $INITDIR

cp docs/*                $DOCDIR
cp GPL ARTISTIC COPYING  $DOCDIR
cp RELEASE-collectl      $DOCDIR

cp UNINSTALL             $SHRDIR
cp formatit.ph           $SHRDIR
cp lexpr.ph              $SHRDIR
cp gexpr.ph misc.ph      $SHRDIR
cp hello.ph graphite.ph  $SHRDIR
cp envrules.std          $SHRDIR
cp statsd.ph             $SHRDIR
cp vmstat.ph             $SHRDIR
cp vnet.ph vmsum.ph      $SHRDIR
cp client.pl             $SHRDIR/util
cp col2tlviz.pl          $SHRDIR/util

# Force in case redoing the install and files already zipped
gzip -f $MANDIR/collectl*

chmod 755 $INITDIR/collectl*
chmod 444 $ETCDIR/collectl.conf
chmod 755 $BINDIR/collectl
chmod 444 $DOCDIR/ARTISTIC $DOCDIR/COPYING $DOCDIR/GPL
chmod 444 $SHRDIR/*ph
chmod 755 $SHRDIR/util/*

# remove any stale versions in case the names/numbers used have changed.
# on new ROCKS installion 'rm' isn't there yet!  [thanks roy]
if [ -x /bin/rm ] ; then
  /bin/rm -f $INITDIR/rc*.d/*collectl
  /bin/rm -f $ETCDIR/rc.d/rc*.d/*collectl
fi

# only if systemd is supported
if [ -d $SYSDDIR ]; then
  cp service/collectl.service $SYSDDIR
fi

# Try and decide which distro this is based on distro specific files.
distro=1
if [ -f /sbin/yast ]; then
    distro=2
    mv -f $INITDIR/collectl-suse $INITDIR/collectl
    rm -f $INITDIR/collectl-debian
    rm -f $INITDIR/collectl-generic
fi

# debian
if [ -f /usr/sbin/update-rc.d ]; then
    distro=3
    mv -f $INITDIR/collectl-debian $INITDIR/collectl
    rm -f $INITDIR/collectl-suse
    rm -f $INITDIR/collectl-generic

    # only if we're installing under /
    [ "$DESTDIR" = "/" ] && update-rc.d collectl defaults
fi

# redhat
if [ -f /etc/redhat-release ]; then
    distro=4
    rm -f $INITDIR/collectl-suse
    rm -f $INITDIR/collectl-debian
    rm -f $INITDIR/collectl-generic
    [ "$DESTDIR" = "/" ] && chkconfig --add collectl
fi

# gentoo
if [ -f $ETCDIR/gentoo-release ]; then
    distro=5
    mv -f $INITDIR/collectl-generic $INITDIR/collectl
    rm -f $INITDIR/collectl-suse
    rm -f $INITDIR/collectl-debian
    [ "$DESTDIR" = "/" ] && rc-update -a collectl default
fi

# Generic Distros
# If /etc/init.d doesn't exist and/or there's no way to use chkconfig or 
# rc-update you're going to have to add some custom code below...
if [ ${distro} = 1 ]; then

    mv -f $INITDIR/collectl-generic $INITDIR/collectl
    rm -f $INITDIR/collectl-suse
    rm -f $INITDIR/collectl-debian

    # If in not installing under / there's nothing extra do
    [ $DESTDIR != "/" ] && exit 0

    # figure out how to handle reboots
    if [ -f /sbin/chkconfig ]; then
        chkconfig --add collectl
    elif [ -f /sbin/rc-update ]; then
        rc-update -a collectl default
    else
        echo "could not figure out how to enable restarting across reboots"
    fi
fi
