Install script for Gentoo

They are almost the same everywhere. Only name and owner are changed. Only one warning - I don’t have systemd so I did not test that section

post-install.sh

#!/usr/bin/env bash

NAME=loraserver
BIN_DIR=/usr/bin
#SCRIPT_DIR=/usr/lib/${NAME}/scripts
SCRIPT_DIR=./
LOG_DIR=/var/log/${NAME}
USER=loraserver
GROUP=loraserver

function install_init {
cp -f ${SCRIPT_DIR}/${NAME} /etc/init.d/${NAME}
chmod +x /etc/init.d/${NAME}
rc-update add ${NAME} default

add defaults file, if it doesn’t exist

if [[ ! -f /etc/conf.d/${NAME} ]]; then
cp ${SCRIPT_DIR}/default /etc/conf.d/${NAME}
chown ${USER}:${GROUP} /etc/conf.d/${NAME}
chmod 640 /etc/conf.d/${NAME}
echo -e “\n\n\n”
echo “---------------------------------------------------------------------------------”
echo “A sample configuration file has been copied to: /etc/conf.d/${NAME}”
echo “After setting the correct values, run the following command to start LoRa Server:”
echo “$ /etc/init.d/${NAME} start”
echo “---------------------------------------------------------------------------------”
echo -e “\n\n\n”
fi

}

function install_systemd {
cp -f ${SCRIPT_DIR}/${NAME}.service /lib/systemd/system/${NAME}.service
systemctl daemon-reload
systemctl enable ${NAME}

add defaults file, if it doesn’t exist

if [[ ! -f /etc/default/${NAME} ]]; then
cp ${SCRIPT_DIR}/default /etc/default/${NAME}
chown ${USER}:${GROUP} /etc/default/${NAME}
chmod 640 /etc/default/${NAME}
echo -e “\n\n\n”
echo “---------------------------------------------------------------------------------”
echo “A sample configuration file has been copied to: /etc/default/${NAME}”
echo “After setting the correct values, run the following command to start LoRa Server:”
echo “$ systemctl start ${NAME}”
echo “---------------------------------------------------------------------------------”
echo -e “\n\n\n”
fi

}

function restart_server {
echo “Restarting LoRa Server”
which systemctl &>/dev/null
if [[ $? -eq 0 ]]; then
systemctl daemon-reload
systemctl restart ${NAME}
else
/etc/init.d/${NAME} restart || true
fi
}

create loraserver user

id ${USER} &>/dev/null
if [[ $? -ne 0 ]]; then
useradd --system -U ${USER} -d /dev/null -s /sbin/nologin
fi

mkdir -p “${LOG_DIR}”
chown ${USER}:${GROUP} “${LOG_DIR}”

add start script

which systemctl &>/dev/null
if [[ $? -eq 0 ]]; then
install_systemd
else
install_init
fi

if [[ -n $2 ]]; then
restart_server
fi


post-uninstall.sh


#!/usr/bin/env bash

NAME=loraserver

function remove_systemd {
systemctl stop ${NAME}
systemctl disable ${NAME}
rm -f /lib/systemd/system/${NAME}.service
}

function remove_initd {
/etc/init.d/${NAME} stop
rc-update del ${NAME}
rm -f /etc/init.d/${NAME}
}

which systemctl &>/dev/null
if [[ $? -eq 0 ]]; then
remove_systemd
else
remove_initd
fi