aboutsummarylogtreecommitdiffstats
path: root/jetbrains-project-launcher.sh
blob: 16f08804d95220e6167e647323990632548bdc07 (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
#!/bin/bash

# Configuration file path
CONFIG_FILE="$HOME/.config/jetbrains-project-launcher.conf"

# Declare variables for configuration
WATCH_DIRS=""
declare -A IDE_MAP

# Read the configuration file
while IFS= read -r line || [ -n "$line" ]; do
    # Skip comments and empty lines
    [[ "$line" =~ ^#.*$ || -z "$line" ]] && continue

    # Check for section headers
    if [[ "$line" =~ ^\[(.*)\]$ ]]; then
        CURRENT_SECTION="${BASH_REMATCH[1]}"
        continue
    fi

    # Parse key-value pairs
    if [[ "$line" =~ ^original_desktop_entry=(.*)$ ]]; then
        IDE_MAP["$CURRENT_SECTION"]="${BASH_REMATCH[1]}"
        WATCH_DIRS+="$HOME/$CURRENT_SECTION "
    fi
done < "$CONFIG_FILE"

# Trim trailing whitespace from WATCH_DIRS
WATCH_DIRS=$(echo "$WATCH_DIRS" | xargs)

# Function to generate desktop entries
generate_desktop_entry() {
    local desktop_entry_path="$1"
    local ide_dir="$2"
    local project="$3"
    local project_path="$4"

    local target_file="$HOME/.local/share/applications/jetbrains-project-launcher-$ide_dir-$project.desktop"

    # Check if desktop entry file exists
    if [[ ! -f "$desktop_entry_path" ]]; then
        echo "Error: Desktop entry file '$desktop_entry_path' not found. Skipping..."
        return
    fi

    # Create the new desktop entry
    cp "$desktop_entry_path" "$target_file"
    sed -i "s|Exec=\(.*\) %u|Exec=\1 $project_path %u|" "$target_file"
    sed -i "s|^Name=.*|Name=$project|" "$target_file"
    sed -i "s|^Comment=.*|Comment=Open $project in $ide_dir|" "$target_file"
    echo "Keywords=$ide_dir;$project;project" >> "$target_file"

    echo "Created desktop entry for $project in $ide_dir."
}

# Clear existing desktop entries
rm -f "$HOME/.local/share/applications/jetbrains-project-launcher-*.desktop"

# Process directories to generate desktop entries
for dir in $WATCH_DIRS; do
    [[ ! -d "$dir" ]] && echo "Warning: Directory '$dir' not found. Skipping..." && continue

    for subdir in "$dir"/*; do
        if [[ -d "$subdir" ]]; then
            IDE_DIR=$(basename "$(dirname "$subdir")")
            PROJECT=$(basename "$subdir")
            DESKTOP_ENTRY="${IDE_MAP[$IDE_DIR]}"
            PROJECT_PATH="$subdir"

            generate_desktop_entry "$DESKTOP_ENTRY" "$IDE_DIR" "$PROJECT" "$PROJECT_PATH"
        fi
    done
done

# Monitor directories for changes
inotifywait -me create,delete,moved_to,moved_from --format '%w%f %e' $WATCH_DIRS | while read DIR EVENT; do
    IDE_DIR=$(basename "$(dirname "$DIR")")
    PROJECT=$(basename "$DIR")

    if [[ -d "$DIR" && ( "$EVENT" == *CREATE* || "$EVENT" == *MOVED_TO* ) ]]; then
        echo "Creating desktop entry for $PROJECT in $IDE_DIR"
        DESKTOP_ENTRY="${IDE_MAP[$IDE_DIR]}"
        generate_desktop_entry "$DESKTOP_ENTRY" "$IDE_DIR" "$PROJECT" "$DIR"
    elif [[ "$EVENT" == *DELETE* || "$EVENT" == *MOVED_FROM* ]]; then
        echo "Deleting desktop entry for $PROJECT in $IDE_DIR"
        rm -f "$HOME/.local/share/applications/jetbrains-project-launcher-$IDE_DIR-$PROJECT.desktop"
    fi
done