aboutsummaryrefslogtreecommitdiff
path: root/scripts/dot-local/bin/print.sh
blob: 8e5651923b9e8647ee7db2dcb7e23d6a10eb02be (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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#!/usr/bin/env bash

# Print Menu System
# Interactive printer selection and options configuration

set -euo pipefail

TEMP_DIR="/tmp/rofi-print-$$"
mkdir -p "$TEMP_DIR"

trap 'rm -rf "$TEMP_DIR"' EXIT

# Function to get list of printers
get_printers() {
    lpstat -p 2>/dev/null | awk '{print $2}' | sort
}

# Function to get printer options using lpoptions
get_printer_options() {
    local printer="$1"
    lpoptions -p "$printer" -l 2>/dev/null || echo ""
}

# Function to parse option values
parse_option_line() {
    local line="$1"
    local option_name=$(echo "$line" | cut -d'/' -f1 | tr -d ' ')
    local values=$(echo "$line" | sed 's/^[^:]*: //')
    echo "$option_name|$values"
}

# Function to get current default value
get_default_value() {
    local values="$1"
    echo "$values" | grep -o '\*[^ ]*' | tr -d '*' || echo ""
}

# Function to get all available values
get_available_values() {
    local values="$1"
    echo "$values" | tr ' ' '\n' | tr -d '*'
}

# Main script
main() {
    FILE="$1"

    if [ ! -f "$FILE" ]; then
        echo "File not provided or not found: $FILE" >&2
        exit 1
    fi

    # Get available printers
    PRINTERS=$(get_printers)
    if [ -z "$PRINTERS" ]; then
        echo "No printers found" >&2
        exit 1
    fi

    # Select printer
    PRINTER=$(echo "$PRINTERS" | fuzzel --dmenu -i -p "Select printer")
    if [ -z "$PRINTER" ]; then
        echo "No printer selected" >&2
        exit 1
    fi

    # Get printer options
    OPTIONS=$(get_printer_options "$PRINTER")

    # Build lp command
    LP_OPTS=()

    # Process each option
    declare -A selected_options

    while IFS= read -r line; do
        if [ -z "$line" ]; then continue; fi

        # Parse option line
        IFS='|' read -r opt_name values <<< "$(parse_option_line "$line")"

        if [ -z "$opt_name" ] || [ -z "$values" ]; then continue; fi

        # Get default and available values
        default=$(get_default_value "$values")
        available=$(get_available_values "$values")

        # Create friendly names for common options
        case "$opt_name" in
            *Duplex*|*Sided*)
                friendly_name="Duplex (Double-sided)"
                ;;
            *Staple*)
                friendly_name="Stapling"
                ;;
            *MediaType*)
                friendly_name="Paper Type"
                ;;
            *PageSize*|*media*)
                friendly_name="Paper Size"
                ;;
            *OutputBin*)
                friendly_name="Output Tray"
                ;;
            *Collate*)
                friendly_name="Collate"
                ;;
            *ColorModel*|*Color*)
                friendly_name="Color Mode"
                ;;
            *Resolution*)
                friendly_name="Print Quality"
                ;;
            *)
                friendly_name="$opt_name"
                ;;
        esac

        # Present options to user with default highlighted
        available_with_default=$(echo "$available" | sed "s/^${default}$/${default} (default)/")

        selected=$(echo "$available_with_default" | fuzzel --dmenu -i -p  "$friendly_name")

        if [ -n "$selected" ]; then
            # Remove " (default)" suffix if present
            selected=$(echo "$selected" | sed 's/ (default)$//')

            # Only add if different from default
            if [ "$selected" != "$default" ]; then
                selected_options["$opt_name"]="$selected"
            fi
        fi

    done <<< "$OPTIONS"

    # Ask for number of copies
    COPIES=$(echo -e "1\n2\n3\n4\n5\n10\n20" | fuzzel --dmenu -i -p  "Number of copies")
    COPIES=${COPIES:-1}

    # Build final lp command options
    for opt in "${!selected_options[@]}"; do
        LP_OPTS+=("-o" "${opt}=${selected_options[$opt]}")
    done

    if [ "$COPIES" -gt 1 ]; then
        LP_OPTS+=("-n" "$COPIES")
    fi

    # Confirmation
    OPTS_SUMMARY="Printer: $PRINTER\nFile: $(basename "$FILE")\nCopies: $COPIES"
    if [ ${#selected_options[@]} -gt 0 ]; then
        OPTS_SUMMARY+="\n\nOptions:"
        for opt in "${!selected_options[@]}"; do
            OPTS_SUMMARY+="\n  $opt = ${selected_options[$opt]}"
        done
    fi

    echo lp -d "$PRINTER" "${LP_OPTS[@]}" "$FILE" 2>"$TEMP_DIR/error.log"

    echo "$OPTS_SUMMARY"
    CONFIRM=$(echo -e "Print\nCancel" | fuzzel --dmenu -i -p  "Confirm print job")

    if [ "$CONFIRM" = "Print" ]; then
        # Execute print command
        if lp -d "$PRINTER" "${LP_OPTS[@]}" "$FILE" 2>"$TEMP_DIR/error.log"; then
            notify-send "Print.sh" "Print job submitted successfully"
        else
            ERROR=$(cat "$TEMP_DIR/error.log")
            echo "Print failed:\n$ERROR" >&2
            exit 1
        fi
    else
        echo "Print cancelled"
        exit 0
    fi
}

main "$@"