diff --git a/install.sh b/install.sh index 7499dfd..2b08137 100755 --- a/install.sh +++ b/install.sh @@ -47,88 +47,95 @@ log_step() { printf "\n${CLR_BOLD}${CLR_CYAN}==>${CLR_RESET} ${CLR_BOLD}%s${CLR_RESET}\n" "$*" } -# If stdin is a pipe (e.g. curl ... | bash), reconnect to /dev/tty if available for interactive prompts & TUI -if [[ ! -t 0 ]]; then - if (exec < /dev/tty) 2>/dev/null; then - exec < /dev/tty - fi -fi +TMP_DIR="" -REPO_URL="${REPO_URL:-https://gitea.creative-dragonslayer.de/Scripts/Setup.git}" -REPO_BRANCH="${REPO_BRANCH:-}" - -log_step "Debian Unstable & Hyprland Setup Framework Installer" -log_info "Repository: $REPO_URL ${REPO_BRANCH:+(Branch: $REPO_BRANCH)}" - -# Ensure git is available -if ! command -v git >/dev/null 2>&1; then - log_info "Installing git..." - if [[ "$(id -u)" -eq 0 ]]; then - DEBIAN_FRONTEND=noninteractive apt-get update -y - DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends git ca-certificates - else - if ! command -v sudo >/dev/null 2>&1; then - log_error "sudo is required to install prerequisites. Please run as root or install sudo." - exit 1 - fi - sudo DEBIAN_FRONTEND=noninteractive apt-get update -y - sudo DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends git ca-certificates - fi -fi - -# Create temporary directory -TMP_DIR="$(mktemp -d /tmp/debian-setup-XXXXXX)" cleanup() { - if [[ -d "$TMP_DIR" ]]; then + if [[ -n "${TMP_DIR:-}" && -d "$TMP_DIR" ]]; then log_info "Cleaning up temporary installation files..." rm -rf "$TMP_DIR" fi } trap cleanup EXIT INT TERM -log_info "Cloning repository into temporary directory: $TMP_DIR..." -CLONE_SUCCESS=0 -if [[ -n "$REPO_BRANCH" ]]; then - if git clone --depth 1 -b "$REPO_BRANCH" "$REPO_URL" "$TMP_DIR" 2>/dev/null; then - CLONE_SUCCESS=1 - else - log_warn "Failed to clone branch '$REPO_BRANCH', falling back to default branch..." - rm -rf "${TMP_DIR:?}"/* "${TMP_DIR:?}"/.* 2>/dev/null || true +main() { + local repo_url="${REPO_URL:-https://gitea.creative-dragonslayer.de/Scripts/Setup.git}" + local repo_branch="${REPO_BRANCH:-}" + + log_step "Debian Unstable & Hyprland Setup Framework Installer" + log_info "Repository: $repo_url ${repo_branch:+(Branch: $repo_branch)}" + + # Ensure git is available + if ! command -v git >/dev/null 2>&1; then + log_info "Installing git..." + if [[ "$(id -u)" -eq 0 ]]; then + DEBIAN_FRONTEND=noninteractive apt-get update -y + DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends git ca-certificates + else + if ! command -v sudo >/dev/null 2>&1; then + log_error "sudo is required to install prerequisites. Please run as root or install sudo." + exit 1 + fi + sudo DEBIAN_FRONTEND=noninteractive apt-get update -y + sudo DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends git ca-certificates + fi fi -fi -if [[ "$CLONE_SUCCESS" -eq 0 ]]; then - if git clone --depth 1 "$REPO_URL" "$TMP_DIR"; then - CLONE_SUCCESS=1 + # Create temporary directory + TMP_DIR="$(mktemp -d /tmp/debian-setup-XXXXXX)" + + log_info "Cloning repository into temporary directory: $TMP_DIR..." + local clone_success=0 + if [[ -n "$repo_branch" ]]; then + if git clone --depth 1 -b "$repo_branch" "$repo_url" "$TMP_DIR" 2>/dev/null; then + clone_success=1 + else + log_warn "Failed to clone branch '$repo_branch', falling back to default branch..." + rm -rf "${TMP_DIR:?}"/* "${TMP_DIR:?}"/.* 2>/dev/null || true + fi fi -fi -if [[ "$CLONE_SUCCESS" -eq 0 ]]; then - log_error "Failed to clone repository from $REPO_URL" - exit 1 -fi - -cd "$TMP_DIR" -chmod +x setup.sh - -log_success "Repository cloned successfully. Starting setup orchestrator..." - -# Check if dry-run flag is requested -is_dry_run=0 -for arg in "$@"; do - if [[ "$arg" == "-d" || "$arg" == "--dry-run" ]]; then - is_dry_run=1 - break + if [[ "$clone_success" -eq 0 ]]; then + if git clone --depth 1 "$repo_url" "$TMP_DIR"; then + clone_success=1 + fi fi -done -# Execute setup.sh with root privileges (or directly if dry-run) while preserving arguments -if [[ "$(id -u)" -eq 0 ]] || [[ "$is_dry_run" -eq 1 ]]; then - ./setup.sh "$@" -else - if ! command -v sudo >/dev/null 2>&1; then - log_error "sudo is required to run setup.sh. Please install sudo or run as root." + if [[ "$clone_success" -eq 0 ]]; then + log_error "Failed to clone repository from $repo_url" exit 1 fi - sudo ./setup.sh "$@" -fi + + cd "$TMP_DIR" + chmod +x setup.sh + + log_success "Repository cloned successfully. Starting setup orchestrator..." + + # Check if dry-run flag is requested + local is_dry_run=0 + for arg in "$@"; do + if [[ "$arg" == "-d" || "$arg" == "--dry-run" ]]; then + is_dry_run=1 + break + fi + done + + # Prepare command to execute setup.sh + local -a setup_cmd=() + if [[ "$(id -u)" -ne 0 ]] && [[ "$is_dry_run" -eq 0 ]]; then + if ! command -v sudo >/dev/null 2>&1; then + log_error "sudo is required to run setup.sh. Please install sudo or run as root." + exit 1 + fi + setup_cmd+=(sudo) + fi + setup_cmd+=(./setup.sh "$@") + + # Reconnect stdin to /dev/tty if stdin is currently piped and /dev/tty is accessible (for interactive prompts/TUI) + if [[ ! -t 0 ]] && [[ -e /dev/tty ]] && ( : < /dev/tty ) 2>/dev/null; then + "${setup_cmd[@]}" < /dev/tty + else + "${setup_cmd[@]}" + fi +} + +main "$@"