added new feature and scripts

This commit is contained in:
2026-03-31 16:44:26 +01:00
parent 53aa364139
commit 782aff492c
6 changed files with 897 additions and 388 deletions

27
installer/00_default.sh Normal file
View File

@@ -0,0 +1,27 @@
echo "Welcome to the Modular Bashrc script!"
echo "In order to keep more clean as possibile you file bashrc, please copy all the bashrc (everything abot the beginning of the following entries:"
echo "--------------------------------------------------------------"
echo '
# Modular Bashrc
mkdir -p ~/.bashrc.d/scripts-needed
mkdir -p ~/.bashrc.d/scripts-enabled
mkdir -p ~/.bashrc.d/scripts-available
if [ -d ~/.bashrc.d ]; then
for needed in ~/.bashrc.d/scripts-needed/*.sh; do
[ -r "$needed" ] && source "$needed"
done
unset needed
for file in ~/.bashrc.d/scripts-enabled/*.sh; do
[ -r "$file" ] && source "$file"
done
unset file
fi'
echo "---------------------------------------------------------------"
echo "in the ~/.bashrc.d/script-available/00_default.sh"
echo "(Please note that this bit has been added once you installed the Modular Bashrc!)"
echo ""
echo "This way, you .bashrc will be keeped clean and tidy!"
echo "Enjoy!"
echo "Simone Cusano!"
echo "https://sld-server.org"

View File

@@ -0,0 +1,52 @@
parse_git_branch() {
if git rev-parse --is-inside-work-tree &>/dev/null; then
status_output=$(git status --porcelain --branch 2>/dev/null)
branch=$(echo "$status_output" | head -n1 | sed -E 's/^## ([^ .]+).*/\1/')
ahead=$(echo "$status_output" | grep -o 'ahead [0-9]*' | awk '{print $2}')
behind=$(echo "$status_output" | grep -o 'behind [0-9]*' | awk '{print $2}')
staged=$(echo "$status_output" | grep -E '^[A-Z]' | wc -l)
modified=$(echo "$status_output" | grep -E '^.[A-Z]' | wc -l)
untracked=$(echo "$status_output" | grep -E '^\?\?' | wc -l)
GREEN_BOLD="\[\033[1;32m\]"
PURPLE_BOLD="\[\033[1;35m\]"
YELLOW_BOLD="\[\033[1;33m\]"
RED_BOLD="\[\033[1;31m\]"
CYAN_BOLD="\[\033[1;36m\]"
RESET="\[\033[0m\]"
git_status=""
[ "$staged" -gt 0 ] && git_status="${git_status}${GREEN_BOLD}+${staged}${RESET} "
[ "$modified" -gt 0 ] && git_status="${git_status}${YELLOW_BOLD}M:${modified}${RESET} "
[ "$untracked" -gt 0 ] && git_status="${git_status}${RED_BOLD}N:${untracked}${RESET} "
# Remote State
remote_status=""
[ -n "$ahead" ] && remote_status="${remote_status}${CYAN_BOLD}${ahead}${RESET}"
[ -n "$behind" ] && remote_status="${remote_status}${CYAN_BOLD}${behind}${RESET}"
# Repo cleaning?
if [ "$staged" -eq 0 ] && [ "$modified" -eq 0 ] && [ "$untracked" -eq 0 ]; then
branch_display="${GREEN_BOLD}${branch}${RESET}"
else
branch_display="${PURPLE_BOLD}${branch}${RESET}"
fi
# Final Building
output="(${branch_display}"
[ -n "$remote_status" ] && output="${output} ${remote_status}"
[ -n "$git_status" ] && output="${output} ${git_status}"
output=$(echo "$output" | sed 's/ $//')
output="${output})"
printf "%s" "$output"
fi
}
PROMPT_COMMAND='PS1="\[\033[1;32m\]\u\[\033[0m\]@\[\033[1;34m\]\h\[\033[0m\]:\[\033[1;33m\]\w\[\033[0m\]$(parse_git_branch)\$ "'

178
installer/02_bashboard.sh Normal file
View File

@@ -0,0 +1,178 @@
############# DEPENDENCIES #############
# - Debian Based OS (for now)
# - curl
# - lm-sensors
# apt update && apt install curl lm-sensors
############################################
colorize() {
local value=$1
local green_limit=$2
local yellow_limit=$3
if [ "$value" -lt "$green_limit" ]; then
echo -e "${GREEN}${value}${NC}"
elif [ "$value" -lt "$yellow_limit" ]; then
echo -e "${YELLOW}${value}${NC}"
else
echo -e "${RED}${value}${NC}"
fi
}
function dashboard_fast() {
#### CHANGABLE OPTIONS ####
ISTHISVM=0 # Change to 1 if this is a VM. This will remove the temperature bit.
MIN_CHECK_IP=30 # Change this value to set the timing cache for checking the public IP
MIN_CHECK_UPDATE=120 # Change this value to set the timing cache for checking updates.
#### Colors ####
GREEN="\e[32m"
YELLOW="\e[33m"
RED="\e[31m"
CYAN="\e[36m"
BOLD="\e[1m"
NC="\e[0m" # reset
#### Cache ####
CACHE_DIR="/tmp/.dashboard_cache"
mkdir -p "$CACHE_DIR"
#### Overview ####
echo -e "${CYAN}========================================${NC}"
echo -e "${BOLD} 🖥️ SYSTEM OVERVIEW${NC}"
echo -e "${CYAN}========================================${NC}"
#### OS Informations Include ####
source /etc/os-release
#### OS info ###
echo -e "${CYAN}DISTRO:${NC} $PRETTY_NAME"
echo -e "${CYAN}ID:${NC} $ID"
echo -e "${CYAN}VERSION:${NC} $VERSION"
echo -e "${CYAN}URL:${NC} $HOME_URL"
echo -e "${CYAN}BUGS_URL:${NC} $BUG_REPORT_URL"
echo -e "${CYAN}========================================${NC}"
#### Hostname ####
echo "📛 $(hostname)"
#### Local IP ####
LOCAL_IPS=$(hostname -I 2>/dev/null | xargs)
PRIMARY_IP=$(echo "$LOCAL_IPS" | awk '{print $1}')
if [ -z "$LOCAL_IPS" ]; then
echo "🏠 Local IP: NOT AVAILABLE (no network?)"
echo "🌐 Web UI: NOT AVAILABLE"
else
echo "🏠 Local IP: $LOCAL_IPS"
echo "🌐 Web UI: http://$PRIMARY_IP:9090"
fi
#### Public IP ####
if [ -f "$CACHE_DIR/public_ip" ] && find "$CACHE_DIR/public_ip" -mmin -$MIN_CHECK_IP | grep -q .; then
PUBLIC_IP=$(cat "$CACHE_DIR/public_ip")
else
PUBLIC_IP=$(timeout 2 curl -s ifconfig.me 2>/dev/null)
if [ -n "$PUBLIC_IP" ]; then
echo "$PUBLIC_IP" > "$CACHE_DIR/public_ip"
fi
fi
if [ -z "$PUBLIC_IP" ]; then
echo "🌍 Public IP: NOT AVAILABLE (no internet?)"
else
echo "🌍 Public IP: $PUBLIC_IP"
fi
#echo ""
echo -e "${CYAN} .................................. ${NC}"
#### Cpu ####
LOAD=$(awk '{print $1}' /proc/loadavg)
CPU_PCT=$(awk -v l="$LOAD" -v n="$(nproc)" 'BEGIN {printf "%d", (l*100/n)}')
CPU_COLOR=$(colorize "$CPU_PCT" 50 80)
echo -e "🧠 CPU: ${CPU_COLOR}%"
#### Ram ####
read MEM_TOTAL MEM_USED <<< $(free -m | awk '/Mem:/ {print $2, $3}')
MEM_PCT=$((MEM_USED * 100 / MEM_TOTAL))
MEM_COLOR=$(colorize "$MEM_PCT" 50 80)
echo -e "💾 RAM: ${MEM_COLOR}% (${MEM_USED}/${MEM_TOTAL}MB)"
#### Diskspace ####
DISK_PCT=$(df / | awk 'NR==2 {gsub("%",""); print $5}')
DISK_COLOR=$(colorize "$DISK_PCT" 50 80)
DISK=$(df -h / | awk 'NR==2 {print $3 "/" $2}')
echo -e "📦 Disk: ${DISK} (${DISK_COLOR}%)"
#### Uptime ####
echo "⏱️ Uptime: $(awk '{printf "%dd %dh %dm", $1/86400, ($1%86400)/3600, ($1%3600)/60}' /proc/uptime)"
#### Load Avarage ####
echo "📊 Load avg: $(awk '{print $1, $2, $3}' /proc/loadavg)"
#### Updates ####
if command -v apt >/dev/null 2>&1; then
if [ -f "$CACHE_DIR/updates" ] && find "$CACHE_DIR/updates" -mmin -$MIN_CHECK_UPDATE | grep -q .; then
UPDATES=$(cat "$CACHE_DIR/updates")
else
UPDATES=$(apt list --upgradable 2>/dev/null | wc -l)
UPDATES=$((UPDATES - 1))
echo "$UPDATES" > "$CACHE_DIR/updates"
fi
## 🎨 Dinamic Color ##
if [ "$UPDATES" -eq 0 ]; then
UPD_COLOR="${GREEN}${UPDATES}${NC}"
elif [ "$UPDATES" -le 20 ]; then
UPD_COLOR="${YELLOW}${UPDATES}${NC}"
else
UPD_COLOR="${RED}${UPDATES}${NC}"
fi
echo -e "📦 Updates: $UPD_COLOR"
else
echo "📦 Updates: N/A"
fi
#### Temperature ####
if [ $ISTHISVM -ne 1 ]; then
if [ -f /sys/class/thermal/thermal_zone0/temp ]; then
TEMP_RAW=$(cat /sys/class/thermal/thermal_zone0/temp)
TEMP=$((TEMP_RAW/1000))
if [ "$TEMP" -lt 50 ]; then
TEMP_COLOR="${GREEN}${TEMP}°C${NC}"
elif [ "$TEMP" -lt 70 ]; then
TEMP_COLOR="${YELLOW}${TEMP}°C${NC}"
else
TEMP_COLOR="${RED}${TEMP}°C${NC}"
fi
echo -e "🌡️ Temp: $TEMP_COLOR"
fi
fi
echo -e "${CYAN}========================================${NC}"
}
dashboard_fast

View File

@@ -1,136 +1,172 @@
#!/bin/bash
clear
user=$(whoami) # Remove the extra space
follow() {
read -p "Press 'Enter' to continue"
clear
current_user="$(whoami)"
selected_user=""
pause() {
read -rp "Press Enter to continue..."
}
loop2=0
while [ $loop2 -lt 1 ]; do # Corrected the integer comparison
select_user() {
while true; do
echo "----------------------------"
echo "Available users:"
echo "----------------------------"
grep -E ':/home/' /etc/passwd | cut -d: -f1
echo "q = exit"
echo "----------------------------"
read -rp "Type the user: " choice
if [[ "$choice" == "q" ]]; then
echo "Exiting..."
exit 1
elif [[ -d "/home/$choice" ]]; then
read -rp "Confirm user '$choice'? (y/N): " confirm
if [[ "$confirm" == "y" ]]; then
selected_user="$choice"
return
fi
else
echo "User '$choice' not valid!"
pause
fi
done
}
# ========================
# USER SELECTION
# ========================
while true; do
echo "### brc-script installer ###"
echo "---------------------------------------------"
echo "Do you want to install for the current user ($user)?"
echo "Install for current user ($current_user)?"
echo "---------------------------------------------"
echo " y - yes"
echo " n - no"
echo " q - exit"
echo " n - choose another user"
echo " q - exit"
echo " default - yes"
echo "---------------------------------------------"
read choice
if [ "$choice" == "n" ]; then
loop2=1
loop1=0
clear
while [ $loop1 -lt 1 ]; do # Corrected the integer comparison
echo "----------------------------"
echo "List of the available users:"
echo "----------------------------"
for i in $(grep -E ':/home/' /etc/passwd | cut -d: -f1); do echo $i; done
echo "q = exit"
echo "----------------------------"
echo "Type the user:"
read choice2
if [ -d "/home/$choice2" ]; then
user=$choice2
echo "Selected user '$choice2'!"
echo "-------------------"
echo "Can you confirm it?"
echo "-------------------"
echo " y = yes "
echo " n = no "
echo " default = no "
echo "-------------------"
read confirm # Changed 'input' to 'read'
if [ "$confirm" == "y" ]; then
user=$choice2
echo "Using user '$choice2'"
loop1=1
else
echo "Wrong selection, for user '$choice2' "
follow
loop1=0
fi
elif [ "$choice2" == "q" ]; then
echo "Exit from the installer script"
exit 1
else
echo "Home folder for '$choice2' does not exist!"
echo "Try with another one!"
read -rp "Choice: " choice
case "$choice" in
n)
select_user
break
;;
q)
echo "Exiting..."
exit 1
;;
*)
read -rp "Confirm current user '$current_user'? (y/N): " confirm
if [[ "$confirm" == "y" ]]; then
selected_user="$current_user"
break
fi
done
elif [ "$choice" == "q" ]; then
echo "[ Exit from the installer script! ]"
exit 1
;;
esac
clear
done
# ========================
# VARIABLES
# ========================
home="/home/$selected_user"
bashrc="$home/.bashrc"
mainfolder="$home/.bashrc.d"
neededfolder="$mainfolder/scripts-needed"
availablefolder="$mainfolder/scripts-available"
enabledfolder="$mainfolder/scripts-enabled"
removedfolder="$mainfolder/scripts-removed"
script_dir="$(cd "$(dirname "$0")" && pwd)"
# ========================
# CREATE DIRECTORIES
# ========================
echo "[ Creating folders... ]"
mkdir -p "$neededfolder"
mkdir -p "$availablefolder"
mkdir -p "$enabledfolder"
mkdir -p "$removedfolder"
# ========================
# BACKUP .bashrc
# ========================
if [[ -f "$bashrc" ]]; then
cp "$bashrc" "$home/bashrc-backup-$(date +%F)"
echo "[ Backup created ]"
else
echo "[ WARNING: .bashrc not found, creating a new one ]"
touch "$bashrc"
fi
# ========================
# MODIFY .bashrc
# ========================
if [[ -f "$script_dir/NEEDED-FOR-INSTALLER" ]]; then
cat "$script_dir/NEEDED-FOR-INSTALLER" >> "$bashrc"
echo "[ Updated .bashrc ]"
else
echo "[ ERROR: NEEDED-FOR-INSTALLER missing ]"
fi
# ========================
# COPY MAIN STRUCTURE
# ========================
cp -r "$script_dir/../." "$mainfolder"
echo "[ Main folder installed ]"
# ========================
# COPY DEFAULT SCRIPTS
# ========================
echo "[ Installing default scripts... ]"
default_scripts=(
"00_default.sh"
"01_git-cli-highlitgh.sh"
"02_bashboard.sh"
)
for script in "${default_scripts[@]}"; do
if [[ -f "$script_dir/$script" ]]; then
cp "$script_dir/$script" "$availablefolder/"
echo " -> Copied $script"
else
clear
loop3=0
while [ $loop3 -eq 0 ]; do
echo "--------------------------------"
echo "Do you want to use '$user' user?"
echo "--------------------------------"
echo " y = yes"
echo " n = no "
echo "--------------------------------"
read choice3
if [ "$choice3" == "y" ]; then
echo "Selected the current user '$user'"
loop2=1
loop3=1
follow
elif [ "$choice3" == "n" ]; then # Corrected from 'no' to 'n'
echo "Not selected the current user '$user'"
loop3=1
follow
clear
else
echo "Pressed wrong button!"
follow
clear
clear
fi
done
echo " -> WARNING: $script not found"
fi
done
#### NEEDED VARIABLES ####
location="$(pwd)/../"
home="/home/$user"
bashrc="$home/.bashrc"
mainfolder="$home/.bashrc.d/"
neededfold="${mainfolder}scripts-needed"
avfolder="${mainfolder}scripts-available"
enfolder="${mainfolder}scripts-enabled"
rmfolder="${mainfolder}scripts-removed"
# ========================
# APPLY CHANGES
# ========================
# shellcheck disable=SC1090
source "$bashrc"
echo "[ Bashrc reloaded ]"
#### INSTALLATION ####
cp "$bashrc" "bashrc-backup-`date +%F`"
echo "[ Created a backup ]"
cat NEEDED-FOR-INSTALLER >> $bashrc
echo "[ Added info in the .bashrc file ]"
cp -r $location $mainfolder
echo "[ Installed Main Folder ]"
echo $mainfolder
source $bashrc
echo "[ Refreshed bashrc ]"
echo ""
echo "Intallation Completed!"
follow
# ========================
# FINAL MESSAGE
# ========================
clear
## EXECUTION ##
echo "##################################"
echo " Small Introduction "
echo " Installation Done "
echo "##################################"
echo ""
echo "You can handle your script by adding them in the $avfolder."
echo "Make sure that all the scripts you add are within a function, otherwise they will be loaded each time you open the bash CLI."
echo "You can start managing scripts by using the command 'brc-script', and refresh bash with 'refresh-brc'."
echo "You can create your own script using 'brc-script -c'."
echo "To enable an available script (after copying or creating one in the scripts-available), use 'brc-script -e' and provide the index."
echo "You can also modify existing scripts using 'brc-script -m'."
echo ""
echo "Scripts available in:"
echo " $availablefolder"
echo ""
echo "Use commands:"
echo " brc-script -> manage scripts"
echo " refresh-brc -> reload config"
echo ""
echo "##################################"
echo " Thanks for using this script. "
echo " Thanks for using this script! "
echo "##################################"
echo "Visit my website www.simolinuxdesign.org to discover more plugins I created!"
pause