pre_install() {
    :
#!/bin/bash


# Exit on error
set -e

# Exit if variable is undefined
set -u

# Move Agent 1.x configs on Linux
set +e
if [ -f /etc/openitcockpit-agent/config.cnf ]; then
    mv /etc/openitcockpit-agent/config.cnf /etc/openitcockpit-agent/config.ini
fi

if [ -f /etc/openitcockpit-agent/customchecks.cnf ]; then
    mv /etc/openitcockpit-agent/customchecks.cnf /etc/openitcockpit-agent/customchecks.ini
fi
set -e

if [ -f /usr/bin/openitcockpit-agent ]; then

    if [ -x "$(command -v systemctl)" ]; then
        set +e
        systemctl is-active --quiet openitcockpit-agent
        if [ $? = 0 ]; then
            systemctl stop openitcockpit-agent
        fi
        systemctl disable openitcockpit-agent
        set -e
    else
        set +e
        ps auxw | grep -P '/usr/bin/openitcockpit-agent' | grep -v grep >/dev/null
        if [ $? = 0 ]; then
            service openitcockpit-agent stop
        fi

        if [ -x "$(command -v update-rc.d)" ]; then
            # Debian / Ubuntu
            update-rc.d -f openitcockpit-agent remove
        fi
        if [ -x "$(command -v chkconfig)" ]; then
            # CentOS
            chkconfig openitcockpit-agent off
            chkconfig --del openitcockpit-agent
        fi
        set -e
    fi

fi

# Move Agent 1.x configs on macOS
set +e
if [ -f /Applications/openitcockpit-agent/config.cnf ]; then
    mv /Applications/openitcockpit-agent/config.cnf /Applications/openitcockpit-agent/config.ini
fi

if [ -f /Applications/openitcockpit-agent/customchecks.cnf ]; then
    mv /Applications/openitcockpit-agent/customchecks.cnf /Applications/openitcockpit-agent/customchecks.ini
fi
set -e


if [ -f /Applications/openitcockpit-agent/openitcockpit-agent ]; then

    if [ -f /Applications/openitcockpit-agent/com.it-novum.openitcockpit.agent.plist ]; then
        /bin/launchctl stop com.it-novum.openitcockpit.agent
        /bin/launchctl unload -F /Applications/openitcockpit-agent/com.it-novum.openitcockpit.agent.plist
    fi

    # Keep configs on Updates
    if [ -f /Applications/openitcockpit-agent/config.ini ]; then
        cp /Applications/openitcockpit-agent/config.ini /Applications/openitcockpit-agent/config.ini.old
    fi

    if [ -f /Applications/openitcockpit-agent/customchecks.ini ]; then
        cp /Applications/openitcockpit-agent/customchecks.ini /Applications/openitcockpit-agent/customchecks.ini.old
    fi

    if [ -f /Applications/openitcockpit-agent/prometheus_exporters.ini ]; then
        cp /Applications/openitcockpit-agent/prometheus_exporters.ini /Applications/openitcockpit-agent/prometheus_exporters.ini.old
    fi
    
fi

}
post_install() {
    :
#!/bin/bash


# Exit on error
set -e

# Exit if variable is undefined
set -u


if [ -f /usr/bin/openitcockpit-agent ]; then

    if [ -x "$(command -v systemctl)" ]; then
        if [ -d /lib/systemd/system/ ]; then
            # Debian / Ubuntu / Arch
            if [ ! -f /lib/systemd/system/openitcockpit-agent.service ]; then
                ln -s /etc/openitcockpit-agent/init/openitcockpit-agent.service /lib/systemd/system/openitcockpit-agent.service
            fi
        elif [ -d /usr/lib/systemd/system/ ]; then
            # RedHat / Suse
            if [ ! -f /usr/lib/systemd/system/openitcockpit-agent.service ]; then
                ln -s /etc/openitcockpit-agent/init/openitcockpit-agent.service /usr/lib/systemd/system/openitcockpit-agent.service
            fi
        fi

        systemctl daemon-reload
        systemctl start openitcockpit-agent
        systemctl enable openitcockpit-agent
    else

        enableConfig="0"
        if [ ! -f /etc/init.d/openitcockpit-agent ]; then
            enableConfig="1"
            ln -s /etc/openitcockpit-agent/init/openitcockpit-agent.init /etc/init.d/openitcockpit-agent
        fi

        if [ "$enableConfig" == "1" ]; then
            if [ -x "$(command -v update-rc.d)" ]; then
                # Debian / Ubuntu
                update-rc.d -f openitcockpit-agent defaults
            fi
            if [ -x "$(command -v chkconfig)" ]; then
                # CentOS
                chkconfig openitcockpit-agent on
            fi
        fi

        service openitcockpit-agent start
    fi

fi

if [ -f /Applications/openitcockpit-agent/openitcockpit-agent ]; then
    if [ -f /Applications/openitcockpit-agent/com.it-novum.openitcockpit.agent.plist ]; then
        if [ -d /Library/LaunchDaemons/ ] && [ ! -f /Library/LaunchDaemons/com.it-novum.openitcockpit.agent.plist ]; then
            ln -s /Applications/openitcockpit-agent/com.it-novum.openitcockpit.agent.plist /Library/LaunchDaemons/com.it-novum.openitcockpit.agent.plist
        fi
    fi

    enableConfig="0"
    set +e
    /bin/launchctl list | grep com.it-novum.openitcockpit.agent
    RC=$?
    if [ "$RC" -eq 1 ]; then
        enableConfig="1"
    fi
    set -e

    # Keep configs on Updates
    if [ -f /Applications/openitcockpit-agent/config.ini.old ]; then
        cp /Applications/openitcockpit-agent/config.ini.old /Applications/openitcockpit-agent/config.ini
    fi

    if [ -f /Applications/openitcockpit-agent/customchecks.ini.old ]; then
        cp /Applications/openitcockpit-agent/customchecks.ini.old /Applications/openitcockpit-agent/customchecks.ini
    fi

    if [ -f /Applications/openitcockpit-agent/prometheus_exporters.ini.old ]; then
        cp /Applications/openitcockpit-agent/prometheus_exporters.ini.old /Applications/openitcockpit-agent/prometheus_exporters.ini
    fi

    if [ "$enableConfig" == "1" ]; then
        /bin/launchctl load /Library/LaunchDaemons/com.it-novum.openitcockpit.agent.plist
    fi

    if [ ! -d "/Library/Logs/openitcockpit-agent" ]; then
        mkdir -p /Library/Logs/openitcockpit-agent
    fi

    /bin/launchctl start com.it-novum.openitcockpit.agent

fi
}
pre_remove() {
    :
#!/bin/bash


# Exit on error
set -e

# Exit if variable is undefined
set -u


if [ -f /usr/bin/openitcockpit-agent ]; then

    set +e
    if [ -x "$(command -v systemctl)" ]; then
        /bin/systemctl -a | grep openitcockpit-agent >/dev/null
        RC=$?
        if [ "$RC" -eq 0 ]; then

            if [ "$1" = "0" ] || [ "$1" = "purge" ] || [ "$1" = "remove" ] ; then
                # Uninstall on CentOS / Debian / Ubuntu
                /bin/systemctl stop openitcockpit-agent
                /bin/systemctl disable openitcockpit-agent
            fi

        fi      
    else
        service openitcockpit-agent stop
        if [ -x "$(command -v update-rc.d)" ]; then
            # Debian / Ubuntu
            update-rc.d -f openitcockpit-agent remove
        fi
        if [ -x "$(command -v chkconfig)" ]; then
            # CentOS
            chkconfig openitcockpit-agent off
            chkconfig --del openitcockpit-agent
        fi
        
    fi
    
    if [ "$1" = "0" ]; then
        # Uninstall on CentOS
        # https://docs.fedoraproject.org/en-US/packaging-guidelines/Scriptlets/#_syntax
        # https://en.opensuse.org/openSUSE:Packaging_scriptlet_snippets
        rm -f /etc/init.d/openitcockpit-agent /lib/systemd/system/openitcockpit-agent.service /usr/lib/systemd/system/openitcockpit-agent.service /var/log/openitcockpit-agent
    fi

    if [ "$1" = "purge" ] || [ "$1" = "remove" ] ; then
        # Uninstall on Debian / Ubuntu
        # https://www.debian.org/doc/debian-policy/ch-maintainerscripts.html
        rm -f /etc/init.d/openitcockpit-agent /lib/systemd/system/openitcockpit-agent.service /usr/lib/systemd/system/openitcockpit-agent.service /var/log/openitcockpit-agent
    fi

    set -e

fi

if [ -f /Applications/openitcockpit-agent/openitcockpit-agent ]; then

    touch /Applications/openitcockpit-agent/tmp_runrm

    set +e
    /bin/launchctl list | grep com.it-novum.openitcockpit.agent >/dev/null
    RC=$?
    if [ "$RC" -eq 0 ]; then
        /bin/launchctl stop com.it-novum.openitcockpit.agent
        /bin/launchctl unload -F /Applications/openitcockpit-agent/com.it-novum.openitcockpit.agent.plist
    fi
    set -e
    
    if [ -d "/Library/Logs/openitcockpit-agent" ]; then
        rm -rf /Library/Logs/openitcockpit-agent
    fi

    rm -rf /Applications/openitcockpit-agent/com.it-novum.openitcockpit.agent.plist /Library/LaunchDaemons/com.it-novum.openitcockpit.agent.plist /Applications/openitcockpit-agent/config.ini /Applications/openitcockpit-agent/customchecks.ini /Applications/openitcockpit-agent/prometheus_exporters.ini /Applications/openitcockpit-agent /private/etc/openitcockpit-agent
fi

}
