Files
wcx_script/wunf_upd_prefix.sh
2025-10-09 10:55:52 +02:00

156 lines
3.5 KiB
Bash
Executable File

#!/usr/bin/env bash
set -euo pipefail
CSV_DEFAULT="wunf_scenes.csv"
usage() {
echo "Användning:"
echo " $0 <filnamn> [csv-fil]"
echo " $0 --all <katalog> [csv-fil]"
echo " $0 --list <textfil> [csv-fil] # en väg per rad"
echo " ls -1 --quoting-style=literal \[WUNF\]_*.mp4 > filenames.txt && ./script/wunf_upd_prefix.sh --list filenames.txt wunf_scenes.csv && rm filenames.txt"
exit 1
}
rename_file() {
local FILE="$1"
local CSV="$2"
local base dir rest first second remainder name code code_us newbase newpath
if [[ ! -f "$FILE" ]]; then
echo "Fel: Filen finns inte: $FILE" >&2
return 1
fi
base="$(basename -- "$FILE")"
dir="$(dirname -- "$FILE")"
# Kontrollera prefix
if [[ "$base" != "[WUNF]"* ]]; then
echo "Hoppar över (ej [WUNF]-prefix): $base" >&2
return 0
fi
# Plocka ut allt efter prefixet [WUNF]_
rest="${base#"[WUNF]"}"
rest="${rest#_}"
IFS='_' read -r first second remainder <<< "$rest"
if [[ -z "${first:-}" || -z "${second:-}" ]]; then
echo "Fel: Kunde inte extrahera två namn-delar i: $base" >&2
return 1
fi
# Namn = två första token, med '_' -> ' '
name="${first//_/ } ${second//_/ }"
name="$(printf '%s' "$name" | sed -E 's/^[[:space:]]+|[[:space:]]+$//g')"
# slå upp i CSV (format Namn;Kod;Tid)
code="$(
awk -F';' -v target="$name" '
BEGIN{IGNORECASE=1}
{
n=$1; gsub(/^[ \t]+|[ \t]+$/, "", n);
if (n==target) {
c=$2; gsub(/^[ \t]+|[ \t]+$/, "", c);
print c; exit
}
}' "$CSV"
)"
if [[ -z "$code" ]]; then
echo "Ingen kod för \"$name\" hittades i $CSV (fil: $base)" >&2
return 1
fi
code_us="${code// /_}"
newbase="$(printf '%s' "$base" | sed -E "s/^\[WUNF\]/[$code_us]/")"
newpath="$dir/$newbase"
if [[ "$base" == "$newbase" ]]; then
echo "Hoppar över (redan rätt namn): $base" >&2
return 0
fi
if [[ -e "$newpath" ]]; then
echo "Fel: Målfilen finns redan: $newpath" >&2
return 1
fi
mv -- "$dir/$base" "$newpath"
echo "Bytt namn:"
echo " Från: $base"
echo " Till: $newbase"
}
### Huvudprogram
if [[ $# -lt 1 ]]; then
usage
fi
mode="$1"
case "$mode" in
--all)
if [[ $# -lt 2 ]]; then usage; fi
DIR="$2"
CSV="${3:-$CSV_DEFAULT}"
if [[ ! -d "$DIR" ]]; then
echo "Fel: Katalog finns inte: $DIR" >&2
exit 1
fi
if [[ ! -f "$CSV" ]]; then
echo "Fel: CSV-filen finns inte: $CSV" >&2
exit 1
fi
shopt -s nullglob
for f in "$DIR"/\[WUNF\]*.mp4; do
rename_file "$f" "$CSV" || true
done
;;
--list)
if [[ $# -lt 2 ]]; then usage; fi
LIST="$2"
CSV="${3:-$CSV_DEFAULT}"
if [[ ! -f "$LIST" ]]; then
echo "Fel: Textfil finns inte: $LIST" >&2
exit 1
fi
if [[ ! -f "$CSV" ]]; then
echo "Fel: CSV-filen finns inte: $CSV" >&2
exit 1
fi
# Läs en rad i taget; ignorera tomma rader och kommentarer
# Hanterar vägar med mellanslag korrekt
while IFS= read -r line || [[ -n "$line" ]]; do
# Trim CR (om filen är CRLF)
line="${line%$'\r'}"
# Trim whitespace runt
trimmed="$(printf '%s' "$line" | sed -E 's/^[[:space:]]+|[[:space:]]+$//g')"
# Hoppa över tomma rader och kommentarer
[[ -z "$trimmed" || "$trimmed" =~ ^# ]] && continue
rename_file "$trimmed" "$CSV" || true
done < "$LIST"
;;
*)
FILE="$mode"
CSV="${2:-$CSV_DEFAULT}"
if [[ ! -f "$CSV" ]]; then
echo "Fel: CSV-filen finns inte: $CSV" >&2
exit 1
fi
rename_file "$FILE" "$CSV"
;;
esac