jolly

jolly / SDIO Clean

Last active 3 weeks ago

Like 0

Tool for deleting old SDIO driver packs

Revision 31da3f5ec7330ea44470b788198baeaaab852d42

sdio-clean.sh Raw
1#!/usr/bin/env bash
2
3# === [ Config ] === #
4DRIVERS_DIR="drivers"
5# (relative paths are resolved from this script's folder)
6
7if [[ -t 1 ]]; then B=$'\e[1m'; BL=$'\e[1;34m'; RD=$'\e[1;31m'; GR=$'\e[1;32m'; R=$'\e[0m'
8else B=; BL=; RD=; GR=; R=; fi
9
10has() { command -v "$1" >/dev/null 2>&1; }
11
12deps=(readlink:coreutils stat:coreutils numfmt:coreutils sort:coreutils
13 rm:coreutils dirname:coreutils sed:sed)
14report="${BL}::${R} ${B}Checking dependencies...${R}"$'\n'
15missing=0
16
17if (( BASH_VERSINFO[0] >= 4 )); then
18 report+=" $(printf '%-10s' 'bash >= 4') ${GR}ok${R} (${BASH_VERSION%%[( ]*})"$'\n'
19else
20 report+=" $(printf '%-10s' 'bash >= 4') ${RD}missing${R} (found ${BASH_VERSION%%[( ]*})"$'\n'
21 missing=$((missing + 1))
22fi
23for d in "${deps[@]}"; do
24 bin="${d%%:*}"; pkg="${d##*:}"
25 if has "$bin"; then
26 report+=" $(printf '%-10s' "$bin") ${GR}ok${R}"$'\n'
27 else
28 report+=" $(printf '%-10s' "$bin") ${RD}missing${R} (package: $pkg)"$'\n'
29 missing=$((missing + 1))
30 fi
31done
32
33printf '%s\n' "$report"
34if (( missing > 0 )); then
35 echo "${RD}error:${R} $missing required dependenc$( ((missing==1)) && echo y || echo ies ) missing."
36 echo "This script can't run without them. Install them and try again."
37 exit 1
38fi
39
40script_dir="$(cd "$(dirname "$(readlink -f "${BASH_SOURCE[0]}")")" && pwd)"
41DRIVERS_DIR="${DRIVERS_DIR/#\~/$HOME}"
42[[ "$DRIVERS_DIR" == /* ]] && dir="$DRIVERS_DIR" || dir="$script_dir/$DRIVERS_DIR"
43yes=0
44for a in "$@"; do [[ "$a" == "-y" ]] && yes=1 || dir="$a"; done
45cd "$dir" || { echo "error: can't open $dir"; exit 1; }
46shopt -s nullglob
47
48human() { numfmt --to=iec-i --suffix=B --format=%.1f "$1"; }
49
50old=(); keep=(); sizes=()
51for base in $(printf '%s\n' DP_*_[0-9]*.7z | sed 's/_[0-9]*\.7z$//' | sort -u); do
52 mapfile -t files < <(printf '%s\n' "${base}"_[0-9]*.7z | sort -V)
53 keep+=("${files[-1]}")
54 old+=("${files[@]:0:${#files[@]}-1}")
55done
56
57if (( ${#old[@]} == 0 )); then echo " there is nothing to do"; exit 0; fi
58
59total=0; echo "${B}Old driver packs (${#old[@]}):${R}"
60echo
61for f in "${old[@]}"; do
62 s=$(stat -c %s -- "$f"); sizes+=("$s"); total=$((total + s))
63 printf ' %-45s %12s\n' "$f" "$(human "$s")"
64done
65echo
66echo "${B}Total Removed Size:${R} $(human "$total")"
67echo "${B}Newest packs kept:${R} ${#keep[@]}"
68echo
69
70if (( ! yes )); then
71 read -r -p "${BL}::${R} ${B}Proceed with removal? [y/N] ${R}" ans
72 [[ "${ans,,}" == y || "${ans,,}" == yes ]] || exit 1
73fi
74
75echo "${BL}::${R} ${B}Removing old driver packs...${R}"
76n=${#old[@]}; i=0; fail=0; freed=0
77for f in "${old[@]}"; do
78 i=$((i + 1))
79 printf '(%*d/%d) removing %-42s ' "${#n}" "$i" "$n" "$f"
80 if rm -- "$f" 2>/dev/null; then
81 freed=$((freed + sizes[i-1])); echo "${GR}[done]${R}"
82 else
83 fail=$((fail + 1)); echo "${RD}[fail]${R}"
84 fi
85done
86
87echo
88if (( fail > 0 )); then
89 echo "${RD}error:${R} failed to remove $fail of $n files"
90 echo "${BL}::${R} ${B}Removed $((n - fail)) files, freed $(human "$freed")${R}"
91 exit 1
92fi
93echo "${BL}::${R} ${B}Removed $n files, freed $(human "$freed")${R}"
94