Files
esn-support-bash-snippets/.update_backup_20250926_091039/install.sh
T

518 lines
16 KiB
Bash
Executable File

#!/usr/bin/env bash
# install.sh - Installationsscript für easyname Support Bash-Snippets
# Maintainer: ljp
# Modular erweiterbar für neue Tools und Kategorien
set -euo pipefail
# Farben für Output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
PURPLE='\033[0;35m'
NC='\033[0m' # No Color
# Script-Verzeichnis ermitteln
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
# Dry-run Modus
DRY_RUN=false
# Logging-Funktionen
log_info() {
echo -e "${BLUE}[INFO]${NC} $1"
}
log_success() {
echo -e "${GREEN}[SUCCESS]${NC} $1"
}
log_warning() {
echo -e "${YELLOW}[WARNING]${NC} $1"
}
log_error() {
echo -e "${RED}[ERROR]${NC} $1"
}
log_dry_run() {
echo -e "${YELLOW}[DRY-RUN]${NC} $1"
}
# Auto-Discovery Funktionen
discover_scripts() {
local category="$1"
local scripts=()
if [[ ! -d "$SCRIPT_DIR/$category" ]]; then
echo "0"
return
fi
# Finde alle .sh Dateien in Unterverzeichnissen
while IFS= read -r -d '' script; do
# Extrahiere Scriptname und Verzeichnis
local script_dir="$(dirname "$script")"
local script_name="$(basename "$script_dir")"
local script_file="$(basename "$script")"
# Überspringe wenn Script-Name nicht dem Verzeichnis entspricht
if [[ "$script_file" == "$script_name.sh" ]]; then
scripts+=("$script_name:$script")
fi
done < <(find "$SCRIPT_DIR/$category" -name "*.sh" -type f -print0 2>/dev/null)
# Rückgabe: Anzahl:script1:path1:script2:path2...
local result="${#scripts[@]}"
for script_info in "${scripts[@]}"; do
result="$result:$script_info"
done
echo "$result"
}
get_script_description() {
local category="$1"
local script_name="$2"
# Versuche Beschreibung aus README zu extrahieren
local readme="$SCRIPT_DIR/$category/$script_name/README.md"
if [[ -f "$readme" ]]; then
# Erste Zeile nach "# scriptname"
grep -A 2 "^# $script_name" "$readme" 2>/dev/null | tail -n 1 | sed 's/^[[:space:]]*//'
else
# Fallback: Erste Kommentarzeile aus Script
local script_file="$SCRIPT_DIR/$category/$script_name/$script_name.sh"
grep -m 1 "^# " "$script_file" 2>/dev/null | sed 's/^# //' || echo "Keine Beschreibung verfügbar"
fi
}
# Hilfsfunktionen
backup_file() {
local file="$1"
if [[ -f "$file" ]]; then
local backup="${file}.backup.$(date +%Y%m%d_%H%M%S)"
if [[ "$DRY_RUN" == "true" ]]; then
log_dry_run "Würde Backup erstellen: $backup"
else
cp "$file" "$backup"
log_info "Backup erstellt: $backup"
fi
fi
}
create_symlink() {
local src="$1"
local dest="$2"
local name="$3"
if [[ -L "$dest" ]]; then
log_warning "$name bereits als Symlink vorhanden, überspringe"
return 0
fi
if [[ -f "$dest" ]]; then
backup_file "$dest"
fi
if [[ "$DRY_RUN" == "true" ]]; then
log_dry_run "Würde Symlink erstellen: $dest -> $src"
else
ln -sf "$src" "$dest"
log_success "$name symlink erstellt: $dest -> $src"
fi
}
make_executable() {
local file="$1"
if [[ "$DRY_RUN" == "true" ]]; then
log_dry_run "Würde ausführbar machen: $file"
else
chmod +x "$file"
log_info "$file ausführbar gemacht"
fi
}
# Installation: Bash-Profile
install_bashprofiles() {
log_info "=== Bash-Profile Installation ==="
echo -e "\n${PURPLE}Verfügbare Profile:${NC}"
echo "1) h0tznpl0tz (pl) - Optimiertes Basis-Profil"
echo "0) Überspringen"
read -p "Profil auswählen [0-1]: " profile_choice
case "$profile_choice" in
1)
log_info "Installiere h0tznpl0tz Bash-Profil..."
backup_file "$HOME/.bashrc"
if [[ "$DRY_RUN" == "true" ]]; then
log_dry_run "Würde kopieren: $SCRIPT_DIR/bashprofiles/pl/.bashrc -> $HOME/.bashrc"
log_dry_run "Würde erstellen: ~/.bash_aliases (falls nicht vorhanden)"
log_dry_run "Würde erstellen: ~/.bash_env (falls nicht vorhanden)"
log_dry_run "Würde erstellen: ~/.bash_paths (falls nicht vorhanden)"
else
cp "$SCRIPT_DIR/bashprofiles/pl/.bashrc" "$HOME/.bashrc"
# Modulare Dateien erstellen falls nicht vorhanden
[[ ! -f "$HOME/.bash_aliases" ]] && touch "$HOME/.bash_aliases"
[[ ! -f "$HOME/.bash_env" ]] && touch "$HOME/.bash_env"
[[ ! -f "$HOME/.bash_paths" ]] && touch "$HOME/.bash_paths"
log_success "Bash-Profil installiert"
fi
log_info "Hinweis: Setze LUSER in ~/.bash_env für go-Tool"
;;
0)
log_info "Bash-Profile übersprungen"
;;
*)
log_error "Ungültige Auswahl"
;;
esac
}
# Generische Script-Installation
install_script() {
local category="$1"
local script_name="$2"
local script_path="$3"
log_info "Installiere $script_name..."
# Spezielle Behandlung für go-Tool (LUSER check)
if [[ "$script_name" == "go" ]]; then
if [[ -z "${LUSER:-}" ]]; then
log_warning "LUSER Umgebungsvariable nicht gesetzt"
if [[ "$DRY_RUN" == "true" ]]; then
log_dry_run "Würde nach LUSER fragen und in ~/.bash_env schreiben"
log_dry_run "Beispiel: export LUSER=\"username\" >> ~/.bash_env"
else
read -p "LUSER eingeben (für Kerberos): " user_input
if [[ -n "$user_input" ]]; then
echo "export LUSER=\"$user_input\"" >> "$HOME/.bash_env"
export LUSER="$user_input"
log_success "LUSER in ~/.bash_env gesetzt"
fi
fi
else
log_info "LUSER bereits gesetzt: $LUSER"
fi
fi
# Script ausführbar machen
make_executable "$script_path"
# Symlink erstellen
ensure_bin_dir
create_symlink "$script_path" "$HOME/bin/$script_name" "$script_name-Tool"
log_success "$script_name installiert"
}
ensure_bin_dir() {
if [[ ! -d "$HOME/bin" ]]; then
if [[ "$DRY_RUN" == "true" ]]; then
log_dry_run "Würde Verzeichnis erstellen: ~/bin"
log_dry_run "Würde PATH erweitern: export PATH=\"\$HOME/bin:\$PATH\" >> ~/.bash_paths"
else
log_info "~/bin existiert nicht. Erstelle Verzeichnis..."
mkdir -p "$HOME/bin"
log_info "Füge ~/bin zu PATH hinzu (in ~/.bash_paths)"
echo 'export PATH="$HOME/bin:$PATH"' >> "$HOME/.bash_paths"
fi
fi
}
# Installation: Connect Tools
install_connect() {
log_info "=== Connect Tools Installation ==="
# Auto-discover verfügbare Scripts
local discovery_result
discovery_result=$(discover_scripts "connect")
local script_count
script_count=$(echo "$discovery_result" | cut -d':' -f1)
if [[ "$script_count" -eq 0 ]]; then
log_info "Keine Connect Tools gefunden"
return
fi
echo -e "\n${PURPLE}Verfügbare Tools:${NC}"
# Parse discovery result und zeige Menu
local scripts=()
local counter=1
IFS=':' read -ra PARTS <<< "$discovery_result"
for ((i=1; i<${#PARTS[@]}; i+=2)); do
local script_name="${PARTS[i]}"
local script_path="${PARTS[i+1]}"
local description
description=$(get_script_description "connect" "$script_name")
echo "$counter) $script_name - $description"
scripts+=("$script_name:$script_path")
((counter++))
done
echo "$counter) Alle Connect Tools"
echo "0) Überspringen"
read -p "Tools auswählen [0-$counter]: " connect_choice
if [[ "$connect_choice" -eq 0 ]]; then
log_info "Connect Tools übersprungen"
elif [[ "$connect_choice" -eq "$counter" ]]; then
# Alle installieren
for script_info in "${scripts[@]}"; do
IFS=':' read -r script_name script_path <<< "$script_info"
install_script "connect" "$script_name" "$script_path"
done
elif [[ "$connect_choice" -ge 1 && "$connect_choice" -lt "$counter" ]]; then
# Einzelnes Tool installieren
local selected_script="${scripts[$((connect_choice-1))]}"
IFS=':' read -r script_name script_path <<< "$selected_script"
install_script "connect" "$script_name" "$script_path"
else
log_error "Ungültige Auswahl"
fi
}
# Installation: Legacy Tools
install_legacy() {
log_info "=== Legacy Tools Installation ==="
# Auto-discover verfügbare Scripts
local discovery_result
discovery_result=$(discover_scripts "legacy")
local script_count
script_count=$(echo "$discovery_result" | cut -d':' -f1)
if [[ "$script_count" -eq 0 ]]; then
log_info "Keine Legacy Tools gefunden"
return
fi
echo -e "\n${PURPLE}Verfügbare Tools:${NC}"
# Parse discovery result und zeige Menu
local scripts=()
local counter=1
IFS=':' read -ra PARTS <<< "$discovery_result"
for ((i=1; i<${#PARTS[@]}; i+=2)); do
local script_name="${PARTS[i]}"
local script_path="${PARTS[i+1]}"
local description
description=$(get_script_description "legacy" "$script_name")
echo "$counter) $script_name - $description"
scripts+=("$script_name:$script_path")
((counter++))
done
echo "$counter) Alle Legacy Tools"
echo "0) Überspringen"
read -p "Tools auswählen [0-$counter]: " legacy_choice
if [[ "$legacy_choice" -eq 0 ]]; then
log_info "Legacy Tools übersprungen"
elif [[ "$legacy_choice" -eq "$counter" ]]; then
# Alle installieren
for script_info in "${scripts[@]}"; do
IFS=':' read -r script_name script_path <<< "$script_info"
install_legacy_script "$script_name" "$script_path"
done
elif [[ "$legacy_choice" -ge 1 && "$legacy_choice" -lt "$counter" ]]; then
# Einzelnes Tool installieren
local selected_script="${scripts[$((legacy_choice-1))]}"
IFS=':' read -r script_name script_path <<< "$selected_script"
install_legacy_script "$script_name" "$script_path"
else
log_error "Ungültige Auswahl"
fi
}
# Spezielle Legacy Script Installation (für quicknav Behandlung)
install_legacy_script() {
local script_name="$1"
local script_path="$2"
log_info "Installiere $script_name..."
if [[ "$script_name" == "quicknav" ]]; then
# quicknav ist eine Bash-Funktion, keine Binary
log_warning "quicknav ist eine Bash-Funktion und muss manuell in .bashrc eingebunden werden"
if [[ "$DRY_RUN" == "true" ]]; then
log_dry_run "Würde Hinweis anzeigen: source \"$script_path\" zu ~/.bashrc hinzufügen"
else
log_info "Füge folgende Zeile zu ~/.bashrc hinzu:"
echo -e "${YELLOW}source \"$script_path\"${NC}"
fi
else
# Normale Script Installation
install_script "legacy" "$script_name" "$script_path"
fi
}
# Installation: WH20 Tools
install_wh20() {
log_info "=== WH20 Tools Installation ==="
# Auto-discover verfügbare Scripts
local discovery_result
discovery_result=$(discover_scripts "wh20")
local script_count
script_count=$(echo "$discovery_result" | cut -d':' -f1)
if [[ "$script_count" -eq 0 ]]; then
log_info "Noch keine WH20 Tools verfügbar"
return
fi
echo -e "\n${PURPLE}Verfügbare Tools:${NC}"
# Parse discovery result und zeige Menu
local scripts=()
local counter=1
IFS=':' read -ra PARTS <<< "$discovery_result"
for ((i=1; i<${#PARTS[@]}; i+=2)); do
local script_name="${PARTS[i]}"
local script_path="${PARTS[i+1]}"
local description
description=$(get_script_description "wh20" "$script_name")
echo "$counter) $script_name - $description"
scripts+=("$script_name:$script_path")
((counter++))
done
echo "$counter) Alle WH20 Tools"
echo "0) Überspringen"
read -p "Tools auswählen [0-$counter]: " wh20_choice
if [[ "$wh20_choice" -eq 0 ]]; then
log_info "WH20 Tools übersprungen"
elif [[ "$wh20_choice" -eq "$counter" ]]; then
# Alle installieren
for script_info in "${scripts[@]}"; do
IFS=':' read -r script_name script_path <<< "$script_info"
install_script "wh20" "$script_name" "$script_path"
done
elif [[ "$wh20_choice" -ge 1 && "$wh20_choice" -lt "$counter" ]]; then
# Einzelnes Tool installieren
local selected_script="${scripts[$((wh20_choice-1))]}"
IFS=':' read -r script_name script_path <<< "$selected_script"
install_script "wh20" "$script_name" "$script_path"
else
log_error "Ungültige Auswahl"
fi
}
# Hilfsfunktion
show_help() {
cat <<HELP
Usage: $0 [OPTIONS]
Interaktiver Installer für easyname Support Bash-Snippets
Optionen:
--dry-run, -n Zeigt nur was gemacht würde, ohne Dateien zu verändern
--help, -h Zeigt diese Hilfe an
Beispiele:
$0 Startet den interaktiven Installer
$0 --dry-run Testet den Installer ohne Veränderungen
HELP
}
# Hauptmenü
show_menu() {
echo -e "\n${GREEN}=== easyname Support Bash-Snippets Installer ===${NC}"
if [[ "$DRY_RUN" == "true" ]]; then
echo -e "${YELLOW}(DRY-RUN MODUS)${NC}"
fi
echo -e "${BLUE}Verfügbare Installationskategorien:${NC}\n"
echo "1) Bash-Profile - Optimierte .bashrc Konfigurationen"
echo "2) Connect Tools - SSH-Shortcuts und Verbindungstools"
echo "3) Legacy Tools - Tools für Legacy-Infrastruktur"
echo "4) WH20 Tools - Tools für WH20-Infrastruktur"
echo "5) Alles installieren"
echo "0) Beenden"
echo ""
}
# Hauptinstallation
main() {
# Argumente parsen
while [[ $# -gt 0 ]]; do
case $1 in
--dry-run|-n)
DRY_RUN=true
shift
;;
--help|-h)
show_help
exit 0
;;
*)
log_error "Unbekannte Option: $1"
show_help
exit 1
;;
esac
done
if [[ "$DRY_RUN" == "true" ]]; then
log_info "=== DRY-RUN MODUS AKTIVIERT ==="
log_info "Keine Dateien werden verändert!"
echo ""
fi
log_info "Installation gestartet in: $SCRIPT_DIR"
# Prüfe Abhängigkeiten
if ! command -v git >/dev/null 2>&1; then
log_error "git ist erforderlich aber nicht installiert"
exit 1
fi
while true; do
show_menu
read -p "Auswahl treffen [0-5]: " choice
case "$choice" in
1) install_bashprofiles ;;
2) install_connect ;;
3) install_legacy ;;
4) install_wh20 ;;
5)
install_bashprofiles
install_connect
install_legacy
install_wh20
;;
0)
log_success "Installation beendet"
echo -e "\n${YELLOW}Hinweis: Starte eine neue Shell oder führe 'source ~/.bashrc' aus${NC}"
exit 0
;;
*)
log_error "Ungültige Auswahl"
;;
esac
echo -e "\n${BLUE}Drücke Enter für Hauptmenü...${NC}"
read -r
done
}
# Script starten
main "$@"