- a file "series_name.txt" where you put all the episode names in order, 1 per line
- a script to which you give the arguments: "$HardlinkDir" "$dirA" "$dirB" "$dirC" (...)
a file "series_name.txt" where you put all the episode names in order, 1 per line. for exemple :
s01e01 - Episode Name
s01e02 - Episode Name
etc.
And a script to which you give the arguments: "HardlinkDir" "series_name.txt" "dirA" "dirB" "dirC" (...)
#!/usr/bin/env bash
_usage() {
cat <<'EOF'
this script needs at least those arguments:
# $1=HardLinkDir, $2=series_name.txt # containing 1 line per episode name
# $3 to $n : the directory, in order, with the *.mp4 and *.mkv in them, in order
it will then EMPTY the $1 directory, and re-create hard links (ln) in it.
EOF
for msg in "$@"; do printf "Erreur: %s\n" "$msg" ; done
exit 1
}
if [ "$#" -lt 3 ]; then
_usage "We need at least 3 args : HardLinkDir series_name.txt dirA ... "
fi
lndir="$1" ;
episode_list="$2" ; series_name="${2/.txt/}" ;
[ -d "$lndir" ] || _usage "arg1 : lndir=$lndir : is not a directory"
[ -f "$episode_list" ] || _usage "arg2 : episode_list=$episode_list : is not a file"
shift 2 # we "absorbed" the first 2 args : the rest are the dirs with the movie files
arg=2
for dir in "$@"; do
arg=$((arg + 1))
[ -d "$dir" ] || _usage "arg${arg}: '$dir' : is not a directory."
done
rm "${lndir:-__must_be_nonvoid__}"/* # delete first the files in that dir, if any
for dir in "$@"; do
ls "${dir}"/*.???
done | awk -v series_name="${series_name}" -v lndir="${lndir}" '
BEGIN { after_first=0 }
(NR == FNR) { rem="we read the first file... so the episode list"
episode[NR]=$0
}
((NR != FNR) && ( after_first==0 )) { after_first=1; episode_number=0 }
((NR != FNR) && ( $0 ~ /..mp4|.mkv/ ) {
rem="NR>FNR, so we read the 2nd file, -, stdin..."
rem="we also match .mp4 or .mkv, so we have a video file fullname as input"
episode_number++
fullfilename=$0; ext=fullfilename ; sub(".*[.]","",ext)
destname = lndir "/" series_name " - " episode[episode_number] "." ext
cmd="ln \"" fullfilename "\" \"" destname "\""
printf("executing: %s\n",cmd)
system(cmd); close(cmd)
}
' "$episode_list" "-" # first file is the episode_list file, then stdin