Skip to content

Instantly share code, notes, and snippets.

@felipemarinho97
Created August 24, 2024 17:26
Show Gist options
  • Save felipemarinho97/261474b690d708d257f7755e70e311be to your computer and use it in GitHub Desktop.
Save felipemarinho97/261474b690d708d257f7755e70e311be to your computer and use it in GitHub Desktop.
#!/bin/bash
# Usage: import-show-pack.sh
# --show-pack-dir <show_pack_dir>
# --show-import-dir <show_import_dir>
# --dry-run
# --verbose
# --help
# Process arguments
while [[ $# -gt 0 ]]; do
case "$1" in
--show-pack-dir)
show_pack_dir="$2"
echo "Using show_pack_dir: $show_pack_dir"
shift 2
;;
--show-pack-dir=*)
show_pack_dir="${1#*=}"
echo "Using show_pack_dir: $show_pack_dir"
shift
;;
--show-import-dir)
show_import_dir="$2"
echo "Using show_import_dir: $show_import_dir"
shift 2
;;
--show-import-dir=*)
show_import_dir="${1#*=}"
echo "Using show_import_dir: $show_import_dir"
shift
;;
--dry-run)
dry_run=true
shift
;;
--verbose)
set -x
shift
;;
--help)
echo "Usage: import-show-pack.sh"
echo " --show-pack-dir <show_pack_dir>"
echo " --show-import-dir <show_import_dir>"
echo " --dry-run"
echo " --verbose"
echo " --help"
exit 0
;;
*)
echo "Unknown option: $1"
exit 1
;;
esac
done
if [ -z "$show_pack_dir" ]; then
echo "show_pack_dir is not set"
exit 1
fi
if [ -z "$show_import_dir" ]; then
echo "show_import_dir is not set"
exit 1
fi
# function to get the season number from the show pack name
function get_season_number {
num=$(echo "$1" | grep -oP 'S\d+' | grep -oP '\d+')
echo "$num"
}
# function to get the folder name from the show pack name
# Example: "The.Big.Bang.Theory.S01E01.mkv" -> "Season 01"
function get_folder_name {
season_num=$(get_season_number "$1")
if [ -z "$season_num" ]; then
echo "Season number not found in file name: $1"
exit 1
fi
echo "Season $season_num"
}
# function to check if a file is a video file
function is_video_file {
if [[ "$1" =~ \.(mkv|mp4|avi)$ ]]; then
echo "true"
else
echo "false"
fi
}
printf "\n== Starting importation of show pack ==\n\n"
# find all video files in the show pack directory
find "$show_pack_dir" -type f | while read -r file; do
file_name=$(basename "$file")
if [ "$(is_video_file "$file_name")" == "false" ]; then
continue
fi
folder_name=$(get_folder_name "$file_name")
season_num=$(get_season_number "$file_name")
temp_show_import_dir="$show_import_dir/$folder_name"
if [ ! -d "$temp_show_import_dir" ]; then
echo "Creating directory: $temp_show_import_dir"
if [ -z "$dry_run" ]; then
mkdir -p "$temp_show_import_dir"
fi
fi
echo "Hardlinking file: $file -> $temp_show_import_dir/$file_name"
if [ -z "$dry_run" ]; then
ln "$file" "$temp_show_import_dir/$file_name"
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment