#!/bin/bash # # cosmic-move.sh - Move a batch of cosmic-*_cosmic.txz packages (and their # .asc / .md5 sidecars) out of the live repo directory into a version-named # archive directory. Only packages whose filename starts with "cosmic-" are # moved (e.g. "just" and "launcher" are left alone). Unlike the old hardcoded # version of this script, the target version is auto-detected (from # cosmic-comp) or can be supplied explicitly, so this works for any COSMIC # release without editing the file. # # Usage: # cosmic-move.sh [-s SRC_DIR] [-d DEST_BASE] [-n] [VERSION] # # -s SRC_DIR Directory containing the cosmic-*_cosmic.txz packages # (default: current directory) # -d DEST_BASE Base directory under which the version dir is created # (default: same as SRC_DIR) # -n Dry run - show what would happen, move nothing # VERSION Optional. If omitted, the version is auto-detected from # the cosmic-comp-*.txz package found in SRC_DIR. # # Examples: # ./cosmic-move.sh # -> auto-detects version from cosmic-comp-*.txz in ./, moves # everything into .// # # ./cosmic-move.sh -s /opt/htdocs/mirror/slackware/cosmic -d /opt/htdocs/mirror/slackware/cosmic/old # -> auto-detects version, moves into # /opt/htdocs/mirror/slackware/cosmic/old// # # ./cosmic-move.sh -n 1.4.0 # -> dry run, force version 1.4.0 instead of auto-detecting # set -euo pipefail SRC_DIR="." DEST_BASE="" DRY_RUN=0 VERSION="" usage() { grep '^#' "$0" | sed -n '2,29p' | sed 's/^#//' exit 1 } while getopts ":s:d:nh" opt; do case "$opt" in s) SRC_DIR="$OPTARG" ;; d) DEST_BASE="$OPTARG" ;; n) DRY_RUN=1 ;; h) usage ;; \?) echo "Unknown option: -$OPTARG" >&2; usage ;; :) echo "Option -$OPTARG requires an argument" >&2; usage ;; esac done shift $((OPTIND - 1)) # Optional positional VERSION argument overrides auto-detection if [ $# -ge 1 ]; then VERSION="$1" fi SRC_DIR="${SRC_DIR%/}" [ -z "$DEST_BASE" ] && DEST_BASE="$SRC_DIR" DEST_BASE="${DEST_BASE%/}" if [ ! -d "$SRC_DIR" ]; then echo "Error: source directory '$SRC_DIR' does not exist" >&2 exit 1 fi # --- Auto-detect version from cosmic-comp if not supplied ------------------ if [ -z "$VERSION" ]; then comp_pkg=$(find "$SRC_DIR" -maxdepth 1 -name 'cosmic-comp-*_cosmic.txz' | sort -V | tail -n1) if [ -z "$comp_pkg" ]; then echo "Error: no VERSION given and no cosmic-comp-*_cosmic.txz found in '$SRC_DIR' to auto-detect from." >&2 echo "Pass a version explicitly, e.g.: $0 1.4.0" >&2 exit 1 fi base=$(basename "$comp_pkg") # cosmic-comp-1.4.0-x86_64-1_cosmic.txz -> 1.4.0 if [[ "$base" =~ -([0-9]+\.[0-9]+\.[0-9]+)-(x86_64|noarch|i586|i686)-[0-9]+_cosmic\.txz$ ]]; then VERSION="${BASH_REMATCH[1]}" else echo "Error: could not parse version out of '$base'" >&2 exit 1 fi echo "Auto-detected COSMIC version: $VERSION (from $base)" fi DEST_DIR="$DEST_BASE/$VERSION" echo "Source: $SRC_DIR" echo "Destination: $DEST_DIR" [ "$DRY_RUN" -eq 1 ] && echo "Mode: DRY RUN (nothing will be moved)" echo if [ "$DRY_RUN" -eq 0 ]; then mkdir -p "$DEST_DIR" fi shopt -s nullglob moved=0 for pkg in "$SRC_DIR"/cosmic-*_cosmic.txz; do fname=$(basename "$pkg") # Skip packages that aren't versioned like X.Y.Z (e.g. date-stamped # builds such as 20250110.4528864) - those aren't part of a normal # COSMIC release and shouldn't be swept into the version directory. if ! [[ "$fname" =~ -[0-9]+\.[0-9]+\.[0-9]+-(x86_64|noarch|i586|i686)-[0-9]+_cosmic\.txz$ ]]; then echo "skipping (non-standard version): $fname" continue fi if [ "$DRY_RUN" -eq 1 ]; then echo "would move: $fname" else mv -v "$pkg" "$DEST_DIR/" fi moved=$((moved + 1)) # Move matching sidecar files (signature / checksum) if present for ext in asc md5 meta lst txt; do side="$SRC_DIR/${fname}.${ext}" if [ -f "$side" ]; then if [ "$DRY_RUN" -eq 1 ]; then echo "would move: $(basename "$side")" else mv -v "$side" "$DEST_DIR/" fi fi done done shopt -u nullglob if [ "$moved" -eq 0 ]; then echo "No cosmic-*_cosmic.txz packages found in '$SRC_DIR' - nothing to do." exit 0 fi echo echo "Done. $moved package(s) $( [ "$DRY_RUN" -eq 1 ] && echo 'would be moved' || echo 'moved' ) to $DEST_DIR"