summaryrefslogtreecommitdiff
path: root/bookmarks_check
blob: f2ddc9d16f685dcfaf461c214daa7c375bb21e77 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#!/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 <filename>"
    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"