#!/bin/bash BOOKMARKS_FILE="$HOME/.config/mpv/bookmarks.json" MATH_RIKA_FILE="$HOME/.local/bin/mt_play" COMP_SCI_FILE="$HOME/.local/bin/cs_play" TVSHOW_FILE="$HOME/.local/bin/bookmarked_play" ANIME_FILE="$HOME/.local/bin/anime_play" MOVIE_FILE="$HOME/.local/bin/movie_play" # Function to check if jq is installed check_jq() { if ! command -v jq &> /dev/null; then echo "jq is not installed. Please install it to parse JSON." exit 1 fi } # Function to get the last bookmark number get_last_bookmark_number() { if [ ! -f "$BOOKMARKS_FILE" ] || [ "$(jq 'keys | length' "$BOOKMARKS_FILE")" -eq 0 ]; then echo "0" else jq 'keys | map(tonumber) | max' "$BOOKMARKS_FILE" fi } # Function to get absolute path get_absolute_path() { local path="$1" if [[ "$path" = /* ]]; then echo "$path" else echo "$(pwd)/$path" fi } # Function to add new bookmark to the bookmarks.json file add_new_bookmark() { local filepath="$1" local title="$2" local bookmark_num="$3" local pos=0 # Starting position for new bookmarks local filename="$(basename "$filepath")" # Ensure bookmarks file exists if [ ! -f "$BOOKMARKS_FILE" ]; then echo "{}" > "$BOOKMARKS_FILE" fi jq --arg num "$bookmark_num" \ --arg filename "$filename" \ --arg filepath "$filepath" \ --arg title "$title" \ --argjson pos "$pos" \ '.[$num] = {filename: $filename, pos: $pos, filepath: $filepath, title: $title}' \ "$BOOKMARKS_FILE" > "${BOOKMARKS_FILE}.tmp" && mv "${BOOKMARKS_FILE}.tmp" "$BOOKMARKS_FILE" echo "Bookmark details added to $BOOKMARKS_FILE" } # Main script check_jq if [ "$#" -ne 1 ]; then echo "Usage: $0 " exit 1 fi filename="$1" filepath=$(get_absolute_path "$filename") title="$filename" # Determine which category the file belongs to if [[ "$filepath" == */Videos/maths_and_rika/* ]]; then category="maths_and_rika" elif [[ "$filepath" == */Videos/computer_science/* ]]; then category="computer_science" elif [[ "$filepath" == */Videos/variety_and_shows/* ]]; then category="variety_and_shows" elif [[ "$filepath" == */Videos/anime/* ]]; then category="anime" elif [[ "$filepath" == */Videos/movies/* ]]; then category="movies" else category="other" fi # Create a bookmark if the file is in the specified directories if [ "$category" != "other" ]; then bookmark_num=$(get_last_bookmark_number) bookmark_num=$((bookmark_num + 1)) echo "Added new bookmark with number $bookmark_num" add_new_bookmark "$filepath" "$title" "$bookmark_num" is_new_bookmark=1 else # For files not in specified directories, do not create a bookmark echo "File is not in a specified directory; not creating a bookmark." is_new_bookmark=0 bookmark_num="" fi # Execute mpv based on the category if [ "$category" == "maths_and_rika" ]; then if [ "$is_new_bookmark" -eq 1 ]; then echo "mpv_launcher --x11-name=mpv_2 --pause --script-opts=bookmarker-start_bookmark=$bookmark_num \"$filepath\" &" >> "$MATH_RIKA_FILE" echo "Added to math and rika bookmarks file" fi mpv_launcher --x11-name=mpv_2 --script-opts=bookmarker-start_bookmark=$bookmark_num "$filepath" & xdo activate -n mpv_2 elif [ "$category" == "computer_science" ]; then if [ "$is_new_bookmark" -eq 1 ]; then echo "mpv_launcher --x11-name=mpv_1 --pause --script-opts=bookmarker-start_bookmark=$bookmark_num \"$filepath\" &" >> "$COMP_SCI_FILE" echo "Added to computer science bookmarks file" fi mpv_launcher --x11-name=mpv_1 --script-opts=bookmarker-start_bookmark=$bookmark_num "$filepath" & xdo activate -n mpv_1 elif [ "$category" == "variety_and_shows" ]; then if [ "$is_new_bookmark" -eq 1 ]; then echo "mpv_launcher --x11-name=mpv_3 --pause --script-opts=bookmarker-start_bookmark=$bookmark_num \"$filepath\" &" >> "$TVSHOW_FILE" echo "Added to TV shows bookmarks file" fi mpv_launcher --x11-name=mpv_3 --script-opts=bookmarker-start_bookmark=$bookmark_num "$filepath" & xdo activate -n mpv_3 elif [ "$category" == "anime" ]; then if [ "$is_new_bookmark" -eq 1 ]; then echo "mpv_launcher --x11-name=mpv_4 --pause --script-opts=bookmarker-start_bookmark=$bookmark_num \"$filepath\" &" >> "$ANIME_FILE" echo "Added to anime bookmarks file" fi mpv_launcher --x11-name=mpv_4 --script-opts=bookmarker-start_bookmark=$bookmark_num "$filepath" & xdo activate -n mpv_4 elif [ "$category" == "movies" ]; then if [ "$is_new_bookmark" -eq 1 ]; then echo "mpv_launcher --x11-name=mpv_5 --pause --script-opts=bookmarker-start_bookmark=$bookmark_num \"$filepath\" &" >> "$MOVIE_FILE" echo "Added to movies bookmarks file" fi mpv_launcher --x11-name=mpv_5 --script-opts=bookmarker-start_bookmark=$bookmark_num "$filepath" & xdo activate -n mpv_5 else # For files not in specified directories, use start_mpv without bookmarks echo "Launching mpv without bookmarks." start_mpv "$filepath" & fi echo "MPV launched with appropriate settings"