summaryrefslogtreecommitdiff
path: root/bin/set-screen
blob: f2ffbfd98cd7ae2973daae1bcae8942c95f6ad56 (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
#!/bin/bash 
 
CONFIG_DIR="${HOME}/.config/set-screen"
 
function get_display_state_hash() {
    local hash etc
    read hash etc < <(
    for i in /sys/class/drm/card*/card*-*; do
        echo $i
        cat $i/status
        cat $i/edid
    done | sha1sum -b)
    echo "$hash"
}
 
function display_state_dir() {
    local hash
    hash="$(get_display_state_hash)"
    echo "${CONFIG_DIR}/$hash"
}
 
function make_display_state_dir() {
    local dir
    dir="$(display_state_dir)"
    mkdir -p "$dir"
    echo "Created $dir"
    if [[ -n "$1" ]]; then
        local srccmd="${CONFIG_DIR}/$1/command"
        if [[ -x "$srccmd" ]]; then
            ln -s "$srccmd" "$dir/command" && \
                echo "Linked <$1> command"
        else
            echo "<$1> command is not there, ignoring"
        fi
    fi
}
 
function run_display_state_commands() {
    local dir
    if [[ -n "$1" ]]; then
       dir="${CONFIG_DIR}/$1"
    else
        dir="$(display_state_dir)"
    fi
 
    if [[ -x "${dir}/command" ]]; then
        if [[ -x "${CONFIG_DIR}/pre-command" ]]; then
            "${CONFIG_DIR}/pre-command"
        fi
        "${dir}/command"
        if [[ -x "${CONFIG_DIR}/post-command" ]]; then
            "${CONFIG_DIR}/post-command"
        fi
    else
        >&2 echo "There's no settings for the current display configuration; run '$0 make' to create them"
    fi
}
 
function print_usage() {
    cat <<EOF
$0 -- re-configure screens
 
Usage:
 $0 make   create directory for current state
 $0 which  show directory for current state
 $0 set    execute 'command' script in directory for current state
 $0        same as 'set'
 $0 help   show this text
EOF
}
 
case "$1" in
    'make') make_display_state_dir "$2" ;;
    'which') display_state_dir ;;
    '' | 'set') run_display_state_commands "$2";;
    'help' | '--help' ) print_usage ; exit 0 ;;
    *) >&2 print_usage; exit 1 ;;
esac