jolly

jolly / SDIO Clean

Last active 3 weeks ago

Like 0

Tool for deleting old SDIO driver packs

jolly revised this gist 3 weeks ago · 31da3f5

1 file changed, 93 insertions

sdio-clean.sh (file created)
@@ -0,0 +1,93 @@
1 + #!/usr/bin/env bash
2 +
3 + # === [ Config ] === #
4 + DRIVERS_DIR="drivers"
5 + # (relative paths are resolved from this script's folder)
6 +
7 + if [[ -t 1 ]]; then B=$'\e[1m'; BL=$'\e[1;34m'; RD=$'\e[1;31m'; GR=$'\e[1;32m'; R=$'\e[0m'
8 + else B=; BL=; RD=; GR=; R=; fi
9 +
10 + has() { command -v "$1" >/dev/null 2>&1; }
11 +
12 + deps=(readlink:coreutils stat:coreutils numfmt:coreutils sort:coreutils
13 + rm:coreutils dirname:coreutils sed:sed)
14 + report="${BL}::${R} ${B}Checking dependencies...${R}"$'\n'
15 + missing=0
16 +
17 + if (( BASH_VERSINFO[0] >= 4 )); then
18 + report+=" $(printf '%-10s' 'bash >= 4') ${GR}ok${R} (${BASH_VERSION%%[( ]*})"$'\n'
19 + else
20 + report+=" $(printf '%-10s' 'bash >= 4') ${RD}missing${R} (found ${BASH_VERSION%%[( ]*})"$'\n'
21 + missing=$((missing + 1))
22 + fi
23 + for 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
31 + done
32 +
33 + printf '%s\n' "$report"
34 + if (( 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
38 + fi
39 +
40 + script_dir="$(cd "$(dirname "$(readlink -f "${BASH_SOURCE[0]}")")" && pwd)"
41 + DRIVERS_DIR="${DRIVERS_DIR/#\~/$HOME}"
42 + [[ "$DRIVERS_DIR" == /* ]] && dir="$DRIVERS_DIR" || dir="$script_dir/$DRIVERS_DIR"
43 + yes=0
44 + for a in "$@"; do [[ "$a" == "-y" ]] && yes=1 || dir="$a"; done
45 + cd "$dir" || { echo "error: can't open $dir"; exit 1; }
46 + shopt -s nullglob
47 +
48 + human() { numfmt --to=iec-i --suffix=B --format=%.1f "$1"; }
49 +
50 + old=(); keep=(); sizes=()
51 + for 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}")
55 + done
56 +
57 + if (( ${#old[@]} == 0 )); then echo " there is nothing to do"; exit 0; fi
58 +
59 + total=0; echo "${B}Old driver packs (${#old[@]}):${R}"
60 + echo
61 + for f in "${old[@]}"; do
62 + s=$(stat -c %s -- "$f"); sizes+=("$s"); total=$((total + s))
63 + printf ' %-45s %12s\n' "$f" "$(human "$s")"
64 + done
65 + echo
66 + echo "${B}Total Removed Size:${R} $(human "$total")"
67 + echo "${B}Newest packs kept:${R} ${#keep[@]}"
68 + echo
69 +
70 + if (( ! yes )); then
71 + read -r -p "${BL}::${R} ${B}Proceed with removal? [y/N] ${R}" ans
72 + [[ "${ans,,}" == y || "${ans,,}" == yes ]] || exit 1
73 + fi
74 +
75 + echo "${BL}::${R} ${B}Removing old driver packs...${R}"
76 + n=${#old[@]}; i=0; fail=0; freed=0
77 + for 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
85 + done
86 +
87 + echo
88 + if (( 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
92 + fi
93 + echo "${BL}::${R} ${B}Removed $n files, freed $(human "$freed")${R}"