#!/bin/sh

# Code shared by the RPM post-install script and by agent upgrade.
# (Linux version.)

# Determine OS platform
UNAME=$(uname | tr "[:upper:]" "[:lower:]")
# If Linux, try to determine specific distribution
if [ "$UNAME" = "linux" ]; then
    # If available, use LSB to identify distribution
    if [ -f /etc/lsb-release -o -d /etc/lsb-release.d ]; then
        export DISTRO=$(lsb_release -i | cut -d: -f2 | sed s/'^\t'//)
    # Otherwise, use release info file
    else
        export DISTRO=$(ls -d /etc/[A-Za-z]*[_-][rv]e[lr]* | grep -v "lsb" | cut -d'/' -f3 | cut -d'-' -f1 | cut -d'_' -f1)
    fi
fi
# For everything else (or if above failed), just use generic identifier
[ "$DISTRO" = "" ] && export DISTRO=$UNAME
unset UNAME

# Optional positional argument: the agent root directory; default is
# $AVROOT or if that's not set /opt/sigma/agent
#
case $1 in
"") : ${AVROOT:=/opt/sigma/agent};;
*) AVROOT=$1;;
esac

# Where the scripts are
#
SCRIPTS=$AVROOT/active/scripts

# some vendors have peculiar ideas about where init.d lives
case `uname` in
AIX)	INITBASE=/etc/rc.d;;
HP-UX)	INITBASE=/sbin;;
*)	INITBASE=/etc;;
esac

# Create /etc/sigma if needed
#
mkdir -p /etc/sigma

# Create /etc/sigma/agent.conf
#
SRC=$SCRIPTS/etc/sigma/agent.conf.tpl
DST=/etc/sigma/agent.conf
sed -e "s|^AVROOT=.*|AVROOT=$AVROOT|" <$SRC >$DST

# Create /etc/init.d/sigma_agent
#
SRC=$SCRIPTS/etc/init.d/sigma_agent
DST=$INITBASE/init.d/sigma_agent
mkdir -p "$INITBASE/init.d"   # systemd-only distros (e.g. EL9) ship no /etc/init.d
cp $SRC $DST
chmod a+rx $DST

# Create symbolic links to /etc/init.d/sigma_agent
#
case `uname` in
Linux)
    # All supported distros (Ubuntu 22.04+, EL9, Amazon Linux 2023) run systemd:
    # install the NATIVE unit (Type=simple, scheduler runs foreground/no-fork) so
    # `systemctl status` tracks the agent correctly -- it shadows the generated
    # LSB unit. Fall back to update-rc.d/chkconfig only on non-systemd hosts.
    if [ -d /run/systemd/system ] && command -v systemctl >/dev/null 2>&1; then
        chmod a+rx "$SCRIPTS/etc/sigma-agent-launch"
        install -m 0644 "$SCRIPTS/etc/sigma_agent.service" \
            /lib/systemd/system/sigma_agent.service
        systemctl daemon-reload
        systemctl enable sigma_agent >/dev/null 2>&1 || true
    elif command -v update-rc.d >/dev/null 2>&1; then
        update-rc.d sigma_agent defaults
    elif command -v chkconfig >/dev/null 2>&1; then
        chkconfig --add sigma_agent
    else
        echo "No systemd/update-rc.d/chkconfig found; register sigma_agent manually." >&2
    fi
    ;;
SunOS)
    ln -fs $INITBASE/init.d/sigma_agent $INITBASE/rc3.d/S99sigma_agent
    ln -fs $INITBASE/init.d/sigma_agent $INITBASE/rcS.d/K01sigma_agent
    ;;
HP-UX)
    ln -fs $INITBASE/init.d/sigma_agent $INITBASE/rc3.d/S99sigma_agent
    ;;
Darwin)
    mkdir -p /Library/StartupItems
    cp -rf $SCRIPTS/Library/StartupItems/SigmaAgent /Library/StartupItems
    chmod 755 /Library/StartupItems/SigmaAgent/SigmaAgent
    chown root:wheel /Library/StartupItems/SigmaAgent/SigmaAgent
    chmod 644 /Library/StartupItems/SigmaAgent/StartupParameters.plist
    chown root:wheel /Library/StartupItems/SigmaAgent/StartupParameters.plist
    ;;
AIX)
# runlevels are totally arbitrary on AIX so we can't say what's right
#    ln -fs $INITBASE/init.d/sigma_agent $INITBASE/rc3.d/Ssigma_agent
#    ln -fs $INITBASE/init.d/sigma_agent $INITBASE/rc3.d/Ksigma_agent
    ;;
*)  echo "Don't know how to create service symlinks on `uname`.";;
esac

# Expose the reconfigure tool on PATH: `sudo sigma-agent-setup`.
if [ -f "$SCRIPTS/sigma-agent-setup" ]; then
    chmod a+rx "$SCRIPTS/sigma-agent-setup"
    [ -d /usr/sbin ] && ln -sf "$SCRIPTS/sigma-agent-setup" /usr/sbin/sigma-agent-setup
fi
[ -f "$SCRIPTS/sigma-agent-svc" ] && chmod a+rx "$SCRIPTS/sigma-agent-svc"

# Unload the poliwall kernel module, if one was loaded previously
#
$AVROOT/active/lib/modules/pw_unload

exit 0
