#!/usr/bin/env bash set -euo pipefail [ "$EUID" -ne 0 ] && { echo "Exécuter en root"; exit 1; } export TERM="${TERM:-xterm-256color}" export NCURSES_NO_UTF8_ACS=1 . /etc/os-release OS_INFO="$NAME $VERSION_ID" # dialog check + install if ! command -v dialog >/dev/null 2>&1; then echo "dialog non installé." read -rp "Installer dialog maintenant ? [O/n] " r r=${r:-O} if [[ "$r" =~ ^[Oo]$ ]]; then apt update && apt install -y dialog else exit 1 fi fi # Taille plein écran H=$(tput lines); W=$(tput cols) H=$((H - 4)); W=$((W - 4)) LISTH=$((H - 9)) [ "$LISTH" -lt 8 ] && LISTH=8 # Détection init if pidof systemd >/dev/null 2>&1; then INIT=systemd else INIT=sysv fi header_text() { cat </dev/null 2>&1; then ON_UNITS+=("$svc") else OFF_UNITS+=("$svc") fi done < <(for s in /etc/init.d/*; do [ -x "$s" ] && basename "$s"; done | sort) SERVICES=( "${ON_UNITS[@]}" "${OFF_UNITS[@]}" ) ITEMS=() for svc in "${ON_UNITS[@]}"; do ITEMS+=("$svc" "\Z4ON\Zn " "on") done for svc in "${OFF_UNITS[@]}"; do ITEMS+=("$svc" "\Z3OFF\Zn" "off") done } apply_selection_systemd() { local sel="$1" unit for unit in "${SERVICES[@]}"; do if [[ " $sel " =~ \"${unit}\" ]]; then systemctl enable "$unit" >/dev/null 2>&1 || true else systemctl disable "$unit" >/dev/null 2>&1 || true fi done } apply_selection_sysv() { local sel="$1" svc for svc in "${SERVICES[@]}"; do if [[ " $sel " =~ \"${svc}\" ]]; then ls /etc/rc?.d/S*"$svc" >/dev/null 2>&1 || update-rc.d "$svc" defaults >/dev/null else ls /etc/rc?.d/S*"$svc" >/dev/null 2>&1 && update-rc.d -f "$svc" remove >/dev/null fi done } # ===== Loop : applique + refresh (live) ===== while true; do SERVICES=(); ITEMS=() if [ "$INIT" = "systemd" ]; then build_items_systemd else build_items_sysv fi exec 3>&1 SEL=$(dialog \ --colors \ --clear \ --backtitle " rcconf hybride " \ --title " Gestion des services " \ --checklist "$(header_text)" \ "$H" "$W" "$LISTH" \ "${ITEMS[@]}" \ 2>&1 1>&3) || { exec 3>&-; clear; exit 0; } exec 3>&- if [ "$INIT" = "systemd" ]; then apply_selection_systemd "$SEL" else apply_selection_sysv "$SEL" fi # refresh auto (ordre ON/OFF recalculé) -> loop continue done