#!/usr/bin/env bash export TERM="${TERM:-xterm}" set -Eeuo pipefail clear || true # ───────────────────────────────────────────────────────────── # PinCabOS - 99-clean-depot.sh # Nettoyage ressources temporaires installateur # Branding: Karots Sugarpie # ───────────────────────────────────────────────────────────── ORANGE=$'\033[38;5;208m' RED=$'\033[1;31m' CYAN=$'\033[38;5;51m' GREEN=$'\033[1;32m' YELLOW=$'\033[1;33m' WHITE=$'\033[1;37m' GRAY=$'\033[38;5;245m' RESET=$'\033[0m' SCRIPT_NAME="$(basename "$0")" TS="$(date +%Y%m%d-%H%M%S)" INSTALL_DIR="/opt/pincabos/install" LOG_DIR="/opt/pincabos/logs" CONFIG_DIR="/opt/pincabos/config" STATE_DIR="/opt/pincabos/state" DOWNLOAD_DIR="/opt/pincabos/download" ESSENTIALS_DIR="/opt/pincabos/essentials" LOG="${LOG_DIR}/99-clean-depot-${TS}.log" mkdir -p "$LOG_DIR" "$CONFIG_DIR" "$STATE_DIR" "$DOWNLOAD_DIR" "$ESSENTIALS_DIR" exec > >(tee -a "$LOG") 2>&1 PCO_CURRENT_STEP="" PCO_STEPS=() PCO_STATUS=() PCO_FINAL_SUMMARY_DONE=0 pco_line() { printf "${CYAN}────────────────────────────────────────────────────────────────${RESET}\n" } pco_split_step() { local raw="${1:-}" PCO_STEP_NUM="**" PCO_STEP_TITLE="$raw" if [[ "$raw" =~ ^([0-9]+)\)\ (.*)$ ]]; then PCO_STEP_NUM="${BASH_REMATCH[1]}" PCO_STEP_TITLE="${BASH_REMATCH[2]}" fi } pco_title() { local raw="$1" pco_split_step "$raw" printf "\n${CYAN}─[%s]─►${ORANGE} %s ${CYAN}◄────${RESET}\n\n" "$PCO_STEP_NUM" "$PCO_STEP_TITLE" } pco_step() { local step="$1" PCO_CURRENT_STEP="$step" PCO_STEPS+=("$step") PCO_STATUS+=("RUNNING") pco_title "$step" } pco_check() { local step="$1" local i pco_split_step "$step" for i in "${!PCO_STEPS[@]}"; do if [ "${PCO_STEPS[$i]}" = "$step" ]; then PCO_STATUS[$i]="OK" fi done printf "${GREEN}─[%s]─► %s ◄──── Check [√]${RESET}\n" "$PCO_STEP_NUM" "$PCO_STEP_TITLE" } pco_fail_current() { local i if [ -n "${PCO_CURRENT_STEP:-}" ]; then for i in "${!PCO_STEPS[@]}"; do if [ "${PCO_STEPS[$i]}" = "$PCO_CURRENT_STEP" ]; then PCO_STATUS[$i]="FAIL" fi done fi } pco_warn() { printf "${YELLOW}WARN:${RESET} %s\n" "$1" } pco_disk_used() { df -h / | awk 'NR==2 {print $3}' } pco_disk_free() { df -h / | awk 'NR==2 {print $4}' } pco_disk_percent() { df -h / | awk 'NR==2 {print $5}' } pco_dir_size() { local path="$1" if [ -e "$path" ]; then du -sh "$path" 2>/dev/null | awk '{print $1}' else echo "0" fi } pco_final_summary() { local exit_code="${1:-0}" local i local failed=0 if [ "${PCO_FINAL_SUMMARY_DONE:-0}" -eq 1 ]; then return 0 fi PCO_FINAL_SUMMARY_DONE=1 echo pco_line printf "${ORANGE}Résumé complet des checks PinCabOS - %s${RESET}\n" "$SCRIPT_NAME" pco_line for i in "${!PCO_STEPS[@]}"; do pco_split_step "${PCO_STEPS[$i]}" case "${PCO_STATUS[$i]}" in OK) printf "${GREEN}─[%s]─► %s ◄──── Check [√]${RESET}\n" "$PCO_STEP_NUM" "$PCO_STEP_TITLE" ;; FAIL|RUNNING) failed=1 printf "${RED}─[%s]─► %s ◄──NOGOOD [X]${RESET}\n" "$PCO_STEP_NUM" "$PCO_STEP_TITLE" ;; *) failed=1 printf "${RED}─[%s]─► %s ◄──NOGOOD [X]${RESET}\n" "$PCO_STEP_NUM" "$PCO_STEP_TITLE" ;; esac done pco_line if [ "$failed" -eq 0 ] && [ "$exit_code" -eq 0 ]; then printf "${GREEN}Tous les checks sont réussis. Cleanup dépôt PinCabOS OK.${RESET}\n" else printf "${RED}Un ou plusieurs checks ont échoué. Code retour: %s${RESET}\n" "$exit_code" fi pco_line printf "${ORANGE}Espace utilisé système :${RESET} %s\n" "$(pco_disk_used)" printf "${ORANGE}Espace disponible :${RESET} %s\n" "$(pco_disk_free)" printf "${ORANGE}Utilisation système :${RESET} %s\n" "$(pco_disk_percent)" printf "${ORANGE}Rapport local :${RESET} %s\n" "$LOG" pco_line echo } pco_on_error() { local exit_code="$?" pco_fail_current pco_final_summary "$exit_code" exit "$exit_code" } trap pco_on_error ERR trap 'pco_final_summary "$?"' EXIT if [ "$(id -u)" -ne 0 ]; then echo "${RED}ERREUR: ce script doit être lancé en root.${RESET}" exit 1 fi pco_line printf "${ORANGE}PinCabOS - 99-clean-depot.sh${RESET}\n" pco_line echo "Date : $(date)" echo "Host : $(hostname)" echo "Script : $SCRIPT_NAME" echo "Log : $LOG" echo printf "${ORANGE}Espace utilisé avant cleanup :${RESET} %s\n" "$(pco_disk_used)" printf "${ORANGE}Espace restant avant cleanup :${RESET} %s\n" "$(pco_disk_free)" printf "${ORANGE}Utilisation avant cleanup :${RESET} %s\n" "$(pco_disk_percent)" echo pco_step "1) Préparation audit cleanup" mkdir -p "$LOG_DIR" "$CONFIG_DIR" "$STATE_DIR" "$DOWNLOAD_DIR" "$ESSENTIALS_DIR" pco_check "1) Préparation audit cleanup" pco_step "2) Analyse espace disque avant cleanup" echo "Root filesystem:" df -h / echo echo "Taille dossiers PinCabOS avant cleanup:" for d in \ "$INSTALL_DIR" \ "$LOG_DIR" \ "$CONFIG_DIR" \ "$STATE_DIR" \ "$DOWNLOAD_DIR" \ "$ESSENTIALS_DIR" \ "/tmp" do printf "%-45s %s\n" "$d" "$(pco_dir_size "$d")" done pco_check "2) Analyse espace disque avant cleanup" pco_step "3) Inventaire ressources install temporaires" echo "Ressources temporaires détectées:" find "$CONFIG_DIR" -maxdepth 1 -type d \ \( -name 'update-assets-*' -o -name 'engine-assets-*' \) \ -print 2>/dev/null || true find "$DOWNLOAD_DIR" -maxdepth 1 -type f \ \( -name '*.tmp' -o -name '*.part' -o -name '*.download' \) \ -print 2>/dev/null || true find /tmp -maxdepth 1 -type f \ \( -name 'pincabos-spin-*' -o -name 'pincabos-wifi-ssids.txt' \) \ -print 2>/dev/null || true pco_check "3) Inventaire ressources install temporaires" pco_step "4) Suppression update-assets temporaires" find "$CONFIG_DIR" -maxdepth 1 -type d -name 'update-assets-*' -print -exec rm -rf {} + 2>/dev/null || true pco_check "4) Suppression update-assets temporaires" pco_step "5) Suppression engine-assets temporaires" find "$CONFIG_DIR" -maxdepth 1 -type d -name 'engine-assets-*' -print -exec rm -rf {} + 2>/dev/null || true pco_check "5) Suppression engine-assets temporaires" pco_step "6) Nettoyage fichiers temporaires download" find "$DOWNLOAD_DIR" -maxdepth 1 -type f \ \( -name '*.tmp' -o -name '*.part' -o -name '*.download' \) \ -print -delete 2>/dev/null || true pco_check "6) Nettoyage fichiers temporaires download" pco_step "7) Nettoyage fichiers temporaires /tmp PinCabOS" find /tmp -maxdepth 1 -type f \ \( -name 'pincabos-spin-*' -o -name 'pincabos-wifi-ssids.txt' \) \ -print -delete 2>/dev/null || true find /tmp -maxdepth 1 -type d \ \( -name 'pincabos-*tmp*' -o -name 'pincabos-extract-*' \) \ -print -exec rm -rf {} + 2>/dev/null || true pco_check "7) Nettoyage fichiers temporaires /tmp PinCabOS" pco_step "8) Nettoyage cache apt" if command -v apt-get >/dev/null 2>&1; then apt-get clean || true apt-get autoclean || true fi pco_check "8) Nettoyage cache apt" pco_step "9) Validation scripts install conservés" for f in \ "$INSTALL_DIR/go" \ "$INSTALL_DIR/go-pincabos.sh" \ "$INSTALL_DIR/00-install-admin.sh" \ "$INSTALL_DIR/01-install-system.sh" \ "$INSTALL_DIR/02-install-engine.sh" \ "$INSTALL_DIR/03-install-check.sh" \ "$INSTALL_DIR/99-clean-depot.sh" do if [ -s "$f" ]; then chmod +x "$f" 2>/dev/null || true bash -n "$f" echo "Check [√] conservé et valide: $f" else pco_warn "Absent ou vide: $f" fi done pco_check "9) Validation scripts install conservés" pco_step "10) Analyse espace disque après cleanup" echo "Root filesystem:" df -h / echo echo "Taille dossiers PinCabOS après cleanup:" for d in \ "$INSTALL_DIR" \ "$LOG_DIR" \ "$CONFIG_DIR" \ "$STATE_DIR" \ "$DOWNLOAD_DIR" \ "$ESSENTIALS_DIR" \ "/tmp" do printf "%-45s %s\n" "$d" "$(pco_dir_size "$d")" done pco_check "10) Analyse espace disque après cleanup" pco_step "11) Résumé espace final" printf "${ORANGE}Espace utilisé final :${RESET} %s\n" "$(pco_disk_used)" printf "${ORANGE}Espace restant final :${RESET} %s\n" "$(pco_disk_free)" printf "${ORANGE}Utilisation finale :${RESET} %s\n" "$(pco_disk_percent)" pco_check "11) Résumé espace final" exit 0