22
33set -e
44
5- SHOW_HELP=false
65REMOVE_LATEST=false
76
8- # --- Usage function ---
97usage () {
108cat << EOF
11- Usage: $( basename " $0 " ) [OPTIONS] <directory>
9+ Usage: $( basename " $0 " ) [OPTIONS]
1210
13- Clean all contents of a directory except the latest versioned "subvortex-X.Y.Z-[alpha|rc].W" subdirectory.
11+ Clean all contents under /var/tmp/subvortex,
12+ preserving only the latest versioned directory and all non-versioned ones.
13+ With --remove, remove everything.
1414
1515Options:
16- -r, --remove Remove the latest version directory as well
16+ -r, --remove Remove all versioned and non-versioned directories
1717 -h, --help Show this help message and exit
1818
1919Examples:
@@ -22,8 +22,20 @@ Examples:
2222EOF
2323}
2424
25- # --- Parse arguments ---
26- ARGS=()
25+ # Determine compatible version sort command
26+ version_sort () {
27+ if command -v sort > /dev/null && sort -V < /dev/null & > /dev/null; then
28+ sort -V
29+ elif command -v gsort > /dev/null; then
30+ gsort -V
31+ else
32+ echo " ❌ Error: version sort (sort -V or gsort -V) not supported on this system." >&2
33+ echo " 👉 On macOS, run: brew install coreutils" >&2
34+ exit 1
35+ fi
36+ }
37+
38+ # Parse arguments
2739while [[ $# -gt 0 ]]; do
2840 case " $1 " in
2941 -r|--remove)
@@ -40,64 +52,65 @@ while [[ $# -gt 0 ]]; do
4052 exit 1
4153 ;;
4254 * )
43- ARGS+=(" $1 " )
44- shift
55+ echo " ❌ Unexpected argument: $1 "
56+ usage
57+ exit 1
4558 ;;
4659 esac
4760done
4861
49- # Restore positional parameters
50- set -- " ${POSITIONAL[@]} "
51-
52- # Show help if requested
53- if [ " $SHOW_HELP " = true ]; then
54- exit 0
55- fi
56-
57- # Check directory argument
58- TARGET_DIR=/var/tmp/subvortex
62+ TARGET_BASE=" /var/tmp/subvortex"
5963
60- if [ -z " $TARGET_DIR " ]; then
61- echo " ❌ Error: No directory provided ."
64+ if [ ! -d " $TARGET_BASE " ]; then
65+ echo " ❌ Error: Directory ' $TARGET_BASE ' does not exist ."
6266 exit 1
6367fi
6468
65- if [ ! -d " $TARGET_DIR " ]; then
66- echo " ❌ Error: Directory '$TARGET_DIR ' does not exist."
67- exit 1
68- fi
69+ cd " $TARGET_BASE "
70+ all_dirs=($( find . -maxdepth 1 -mindepth 1 -type d -exec basename {} \; ) )
71+
72+ versioned_dirs=()
73+ non_versioned_dirs=()
6974
70- # --- Find and preserve the latest subvortex version ---
71- cd " $TARGET_DIR "
72- version_dirs=($( find . -maxdepth 1 -type d -name " subvortex-*" -exec basename {} \; | grep -E ' ^subvortex-[0-9]+\.[0-9]+\.[0-9]+([a-z]+\d+)?$' ) )
75+ # Classify directories
76+ for dir in " ${all_dirs[@]} " ; do
77+ if [[ " $dir " =~ ^subvortex-[0-9]+\. [0-9]+\. [0-9]+ ([^/]+)? $ ]]; then
78+ versioned_dirs+=(" $dir " )
79+ else
80+ non_versioned_dirs+=(" $dir " )
81+ fi
82+ done
7383
74- if [ ${# version_dirs[@]} -eq 0 ]; then
75- echo " ⚠️ No matching subvortex version directories found."
76- exit 0
84+ # Identify latest versioned directory
85+ latest_version=" "
86+ if [ ${# versioned_dirs[@]} -gt 0 ]; then
87+ latest_version=$( printf " %s\n" " ${versioned_dirs[@]} " | version_sort | tail -n 1)
7788fi
7889
79- latest_version= $( printf " %s\n " " ${version_dirs[@]} " | sort -V | tail -n 1 )
90+ echo " 🧹 Cleaning up: $TARGET_BASE "
8091
81- if [ " $REMOVE_LATEST " = false ]; then
82- echo " 🛡️ Preserving latest version: $latest_version "
83- else
84- echo " 🧨 '--remove' provided — deleting everything, including: $latest_version "
85- fi
92+ for dir in " ${all_dirs[@]} " ; do
93+ keep=false
8694
87- # --- Remove content ---
88- for entry in " $TARGET_DIR " /* " $TARGET_DIR " /.* ; do
89- base=$( basename " $entry " )
95+ if [ " $REMOVE_LATEST " = false ]; then
96+ for nvd in " ${non_versioned_dirs[@]} " ; do
97+ if [ " $dir " == " $nvd " ]; then
98+ keep=true
99+ break
100+ fi
101+ done
90102
91- if [ " $base " == " ." ] || [ " $base " == " .." ]; then
92- continue
103+ if [ " $dir " == " $latest_version " ]; then
104+ keep=true
105+ fi
93106 fi
94107
95- if [ " $REMOVE_LATEST " = false ] && [ " $base " == " $latest_version " ]; then
96- continue
108+ if [ " $keep " = true ]; then
109+ echo " 🛡️ Preserving: $dir "
110+ else
111+ echo " 🔥 Removing: $dir "
112+ rm -rf " $dir "
97113 fi
98-
99- echo " 🧹 Removing: $entry "
100- rm -rf " $entry "
101114done
102115
103116echo " ✅ Cleanup complete."
0 commit comments