post_install_script = '''
#!/bin/bash -e
#RPM_SYSTEMD_MACROS#

if [ $EUID -ne 0 ]; then
    echo >&2 "ERROR: RTRTR postinst script must be run as root"
    exit 1
fi

if [ $1 -eq 1 ] ; then
    # Initial installation
    R_USER=rtrtr
    R_GROUP=${R_USER}
    R_HOME_DIR=/var/lib/rtrtr
    R_HOME_DIR_PERMS=700

    # https://github.com/NLnetLabs/routinator/issues/774
    if ! id ${R_USER} > /dev/null 2>&1; then
		# According to the CentOS 7 useradd man page:
		# --user-group causes a group by the same name as the user to be created
		# --create-home should force creation of a home dir even for a system account.
		useradd --system --home-dir ${R_HOME_DIR} --system --create-home --user-group ${R_USER}
    fi

    # Ensure that the home directory has the correct ownership
    chown -R ${R_USER}:${R_GROUP} ${R_HOME_DIR}

    # Ensure that the home directory has the correct permissions
    chmod ${R_HOME_DIR_PERMS} ${R_HOME_DIR}

    # Run commands equivalent to what the RPM systemd macros would do
    systemd_post rtrtr.service
    systemd_triggers
fi
'''

pre_uninstall_script = '''
#!/bin/bash -e
#RPM_SYSTEMD_MACROS#

if [ $1 -eq 0 ] ; then
    # Package removal, not upgrade
    # Run commands equivalent to what the RPM systemd macros would do
    systemd_preun rtrtr.service
    systemd_triggers
fi
'''

post_uninstall_script = '''
#!/bin/bash -e
#RPM_SYSTEMD_MACROS#

if [ $1 -ge 1 ] ; then
    # Run commands equivalent to what the RPM systemd macros would do
    systemd_postun_with_restart rtrtr.service
    systemd_triggers
fi
'''

