renew-cert.sh/install.sh
2025-09-11 20:29:10 +02:00

134 lines
4.1 KiB
Bash
Executable File

#!/bin/bash
############################################################
# Decription: Install script
# Author: Gilles Mouchet (gilles.mouchet@gmail.com)
# Creation Date: 06-Sep-2025
# Version: 1.0
#
# Changelog:
# V1.0.0 - 06-Sep-2025 - GMo
# Added
# - Creation of script from scratch
#
############################################################
#-----------------------------------------------------------
# FUNCTIONS
#-----------------------------------------------------------
# Function installPacakege if needed
installPackage() {
packageName="$1"
echo "Installation of $packageName..."
case "$packageManager" in
apt)
sudo apt update && sudo apt install -y "$packageName"
;;
dnf)
sudo dnf install -y "$packageName"
;;
esac
if [ $? -eq 0 ]; then
echo "$packageName installed successfully"
else
echo "Error installing $packageName"
exit 1
fi
}
#-----------------------------------------------------------
# variables
user=gilles
group=gilles
fullScriptName=renew-cert.sh
shortScriptName=`echo $fullScriptName | sed -e 's|.*/||g' | cut -f1 -d.`
destPath=/usr/local/bin/gmotools
configFile=$shortScriptName.conf
configFilePath=/etc/$shortScriptName/
logRotateFile=$shortScriptName
logRotateFilePath=/etc/logrotate.d/$logRotateFile
cronFile=$shortScriptName.cron
# check if the effective user ID is 0 (root)
if [[ $EUID -ne 0 ]]; then
echo "[ERROR] - this script must be run as root or with sudo."
exit 1
fi
# select packet manager if need to install package during install
packageManager=""
if command -v apt &> /dev/null; then
packageManager="apt"
elif command -v dnf &> /dev/null; then
packageManager="dnf"
else
echo "[ERROR] - no supported package managers (apt, dnf) were found."
exit 1
fi
echo "[INFO] - package manager detected: $packageManager"
# check if logPath exist
if [ ! -d $configFilePath ]; then
mkdir $configFilePath &> /dev/null
rc=$?
if [ "$rc" != "0" ];then
echo "[ERROR] - an error occurred while creating $configFilePath ($rc)"
else echo "[SUCCESS] - the folder $configFilePath was created successfully."
fi
fi
# install package (example)
installPackage mutt &> /dev/null
# install script
cp $fullScriptName $destPath/. &> /dev/null
rc=$?
if [ "$rc" != "0" ];then
echo "[ERROR] - an error occurred while copying $fullScriptName to $destPath ($rc)"
exit 1
else echo "[SUCCESS] - the script ${fullScriptName} to $destPath was copied successfully."
fi
# copy config file
cp $configFile $configFilePath &> /dev/null
rc=$?
if [ "$rc" != "0" ];then
echo "[ERROR] - an error occurred while copying $configFile to $configFilePath ($rc)"
exit 1
else echo "[SUCCESS] - the script $configFile to $configFilepath was copied successfully."
fi
cp $logRotateFile.logrotate $logRotateFilePath &> /dev/null
rc=$?
if [ "$rc" != "0" ];then
echo "[ERROR] - an error occurred while copying $logRotateFile.logrotate to $logRotateFilePath ($rc)"
exit 1
else echo "[SUCCESS] - the script $logRotateFile.logrotate to $logRotateFilePath was copied successfully."
fi
cp $shortScriptName.cron /etc/cron.d/$shortScriptName &> /dev/null
rc=$?
if [ "$rc" != "0" ];then
echo "[ERROR] - n error occurred while copying $shortScriptName.cron to /etc/cron.d/$shortScriptName ($rc)"
exit 1
else echo "[SUCCESS] - $shortScriptName.cron to /etc/cron.d/$shortScriptName was copied successfully."
fi
# create log file if not exist
if [ ! -d /var/log/$shortScriptName ]; then
mkdir /var/log/$shortScriptName &> /dev/null
rc=$?
if [ "$rc" != "0" ];then
echo "[ERROR] - An error occurred while creating /var/log/$shortScriptName ($rc)"
exit 1
else echo "[SUCCESS] - /var/log/$shortScriptName was created successfully."
fi
fi
sudo chown $user:$group /var/log/$shortScriptName
rc=$?
if [ "$rc" != "0" ];then
echo "[ERROR] - An error occurred while applying user ($user) and group ($group) owner /var/log/$shortScriptName ($rc)"
exit 1
else echo "[SUCCESS] - User ($user) and group ($group) owner on /var/log/$shortScriptName was applied successfully."
fi
echo "Installation completed"