#!/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" PUBLIC_ACCESS_FILE="$HOME/.local/bin/bookmarked_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 check if filepath exists in bookmarks path_exists() { local path="$1" if [ ! -f "$BOOKMARKS_FILE" ]; then return 1 fi jq -e --arg filepath "$path" '.[] | select(.filepath == $filepath)' "$BOOKMARKS_FILE" > /dev/null } # 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" if ! path_exists "$filepath"; then bookmark_num=$(get_last_bookmark_number) bookmark_num=$((bookmark_num + 1)) echo "Added new bookmark with number $bookmark_num" is_new_bookmark=1 add_new_bookmark "$filepath" "$title" "$bookmark_num" else bookmark_num=$(jq -r --arg filepath "$filepath" 'to_entries[] | select(.value.filepath == $filepath) | .key' "$BOOKMARKS_FILE") echo "Bookmark already exists with number $bookmark_num" is_new_bookmark=0 # Do not modify existing bookmark to preserve position fi # Determine which file to write to based on filepath and execute mpv if [[ "$filepath" == */Videos/maths_and_rika/* ]]; then if [ "$is_new_bookmark" -eq 1 ]; then echo "mpv --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 --x11-name=mpv_2 --script-opts=bookmarker-start_bookmark=$bookmark_num "$filepath" & xdo activate -n mpv_2 elif [[ "$filepath" == */Videos/computer_science/* ]]; then if [ "$is_new_bookmark" -eq 1 ]; then echo "mpv --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 --x11-name=mpv_1 --script-opts=bookmarker-start_bookmark=$bookmark_num "$filepath" & xdo activate -n mpv_1 elif [[ "$filepath" == */Videos/variety_and_shows/* ]]; then if [ "$is_new_bookmark" -eq 1 ]; then echo "mpv --x11-name=mpv_3 --pause --script-opts=bookmarker-start_bookmark=$bookmark_num \"$filepath\" &" >> "$PUBLIC_ACCESS_FILE" echo "Added to TV shows bookmarks file" fi mpv --x11-name=mpv_3 --script-opts=bookmarker-start_bookmark=$bookmark_num "$filepath" & xdo activate -n mpv_3 else # If file is not in known categories, run start_mpv echo "File path does not match any known category. Running start_mpv." start_mpv "$filepath" & fi echo "MPV launched with appropriate settings"