#!/bin/sh

# Create directories
mkdir -p @CMAKE_INSTALL_PREFIX@/@MAXSCALE_LIBDIR@
mkdir -p @CMAKE_INSTALL_PREFIX@/@MAXSCALE_BINDIR@
mkdir -p @CMAKE_INSTALL_PREFIX@/@MAXSCALE_SHAREDIR@
mkdir -p @CMAKE_INSTALL_PREFIX@/@MAXSCALE_DOCDIR@

# MAXSCALE_VARDIR is an absolute path to /var by default
mkdir -p @MAXSCALE_VARDIR@/log/maxscale
mkdir -p @MAXSCALE_VARDIR@/lib/maxscale
mkdir -p @MAXSCALE_VARDIR@/cache/maxscale
mkdir -p @MAXSCALE_VARDIR@/run/maxscale

# Create MaxScale user
if [ -f "/etc/passwd" ] && [ "$(grep -c 'maxscale' /etc/passwd)" -eq 0 ]
then
    groupadd -r maxscale
    useradd -r -s /bin/false -g maxscale maxscale
fi

# Change the owner of the directories to maxscale:maxscale
chown -R maxscale:maxscale @MAXSCALE_VARDIR@/log/maxscale
chown -R maxscale:maxscale @MAXSCALE_VARDIR@/lib/maxscale
chown -R maxscale:maxscale @MAXSCALE_VARDIR@/cache/maxscale
chown -R maxscale:maxscale @MAXSCALE_VARDIR@/run/maxscale
chmod 0755 @MAXSCALE_VARDIR@/log/maxscale
chmod 0755 @MAXSCALE_VARDIR@/lib/maxscale
chmod 0755 @MAXSCALE_VARDIR@/cache/maxscale
chmod 0755 @MAXSCALE_VARDIR@/run/maxscale

# Create the module configuration directory (default: /etc/maxscale.modules.d/)
mkdir -p @DEFAULT_MODULE_CONFIGDIR@

# Install ldconfig files
if [ -f "@CMAKE_INSTALL_PREFIX@/@MAXSCALE_SHAREDIR@/maxscale.conf" ]
then
    cp @CMAKE_INSTALL_PREFIX@/@MAXSCALE_SHAREDIR@/maxscale.conf /etc/ld.so.conf.d/
else
    echo "Could not find ldconfig file: @CMAKE_INSTALL_PREFIX@/@MAXSCALE_SHAREDIR@/maxscale.conf" >& 2
fi

# Only copy the service files if the systemd folder and systemctl executable are found
if [ -f @CMAKE_INSTALL_PREFIX@/@MAXSCALE_SHAREDIR@/maxscale.service ] && command -v systemctl >/dev/null
then
    if [ -d "/lib/systemd/system"  ]
    then
        cp @CMAKE_INSTALL_PREFIX@/@MAXSCALE_SHAREDIR@/maxscale.service /lib/systemd/system
        systemctl daemon-reload
    elif [ -d "/usr/lib/systemd/system"  ]
    then
        cp @CMAKE_INSTALL_PREFIX@/@MAXSCALE_SHAREDIR@/maxscale.service /usr/lib/systemd/system
        systemctl daemon-reload
    fi
else
    if [ -d "/etc/init/" ] && [ -f "@CMAKE_INSTALL_PREFIX@/@MAXSCALE_SHAREDIR@/upstart/maxscale.conf" ]
    then
        cp @CMAKE_INSTALL_PREFIX@/@MAXSCALE_SHAREDIR@/upstart/maxscale.conf /etc/init/
    fi

    # If systemd is not present, use init.d scripts
    if [ -f "@CMAKE_INSTALL_PREFIX@/@MAXSCALE_SHAREDIR@/maxscale" ]
    then
        cp @CMAKE_INSTALL_PREFIX@/@MAXSCALE_SHAREDIR@/maxscale /etc/init.d/
    else
        echo "Could not find init script: @CMAKE_INSTALL_PREFIX@/@MAXSCALE_SHAREDIR@/maxscale" >& 2
    fi
fi

# If no maxscale.cnf file is found in /etc, copy the template file there
if [ ! -f "@MAXSCALE_CONFDIR@/maxscale.cnf" ]
then
    cp @MAXSCALE_CONFDIR@/maxscale.cnf.template @MAXSCALE_CONFDIR@/maxscale.cnf
fi

# This will allow us to install the Perl scripts as executable files
# without adding the Perl dependencies
find @CMAKE_INSTALL_PREFIX@/@MAXSCALE_SHAREDIR@/plugins/nagios -name '*.pl' -exec chmod ug+x {} \;

/sbin/ldconfig
