#!/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 fullScriptName=template.sh shortScriptName=`echo $fullScriptName | sed -e 's|.*/||g' | cut -f1 -d.` destPath=/usr/local/bin/ configFile=$shortScriptName.conf configFilePath=/etc/$shortScriptName/ logRotateFile=$shortScriptName logRotateFilePath=/etc/logrotate.d/$logRotateFile # check if the effective user ID is 0 (root) if [[ $EUID -ne 0 ]]; then echo "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 "Erreur : No supported package managers (apt, dnf) were found." exit 1 fi echo "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)" 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)" 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)" else echo "[SUCCESS] - The script $logRotateFile.logrotate to $logRotateFilePath was copied successfully." fi echo "Installation completed."