Move xlaunch to bin dir
[khome.git] / home / bin / xlaunch
CommitLineData
d6c84e8b
SK
1#! /bin/bash
2
3set -e
4
6db09847 5SESSION='xlaunch'
1cbcd78f
SK
6SOCK_NAME="$SESSION"
7TMUX="tmux -L $SOCK_NAME"
ac1c27a5
SK
8LOG_FILE=~/var/log/xlaunch.log
9
10log() {
11 local -r fmt="$1"
12 shift
13
e9c91333 14 printf "[%s] ${fmt}\n" "$(date +'%F %T')" $@ >> "$LOG_FILE"
ac1c27a5
SK
15}
16
6db09847
SK
17counter_next() {
18 local -r file="$_counter_file"
e2b9f534 19
6db09847
SK
20 awk '{n = $1} END {print n + 1}' "$file" | sponge "$file"
21 cat "$file"
e2b9f534
SK
22}
23
6db09847
SK
24tmux_new_win() {
25 local -r command="$1"
26 local -r window_id=$(counter_next)
27 local -r window_name=$(basename "$command")
28 local -r pane=0
29
30 log \
31 '[debug] tmux window_id:"%s", window_name:"%s", command:"%s"' \
32 "$window_id" "$window_name" "$command"
d6c84e8b 33
6db09847
SK
34 $TMUX new-window -t "$SESSION" -n "$window_name"
35 $TMUX send-keys -t "$SESSION":"$window_id"."$pane" "$command" ENTER
d6c84e8b
SK
36}
37
6db09847
SK
38launch_from_directory() {
39 local -r scripts_dir="$1"
658bbf0d 40
6db09847 41 log '[info] Looking for scripts in directory: "%s"' "$scripts_dir"
57081bde 42 if test -d "$scripts_dir"
658bbf0d
SK
43 then
44 for script in "$scripts_dir"/*; do
ac1c27a5 45 log '[debug] Launching script: "%s"' "$script"
6db09847
SK
46 tmux_new_win "$script"
47 sleep 0.1 # TODO Find a way to block between starts instead.
658bbf0d 48 done
57081bde 49 else
6db09847 50 log '[warning] Directory not found: %s' "$scripts_dir"
658bbf0d 51 fi
d6c84e8b
SK
52}
53
6db09847
SK
54_start() {
55 $TMUX new-session -d -s "$SESSION"
56 $TMUX set-option -gt "$SESSION" allow-rename off
ac1c27a5 57
6db09847
SK
58 _counter_file=$(mktemp)
59 launch_from_directory ~/.xlaunch.d
60 launch_from_directory ~/.xlaunch.d."$(hostname)"
61}
ac1c27a5 62
6db09847
SK
63_startx() {
64 log '[info] Starting X11'
65 _restart
ac1c27a5
SK
66 log '[info] Launching dwm'
67 exec dwm
68}
d6c84e8b 69
6db09847
SK
70_stop() {
71 $TMUX kill-session -t "$SESSION"
72}
73
74_restart() {
75 _stop || true
76 _start
77}
78
79_attach() {
80 $TMUX attach -t "$SESSION"
81}
82
83main() {
84 local -r command="$1"
85
86 case "$command" in
87 #'' ) _startx;;
88 'startx' ) _startx;;
89 'start' ) _start;;
90 'stop' ) _stop;;
91 'restart' ) _restart;;
92 'attach' ) _attach;;
93 *)
94 echo "[error] Unknown command: \"$command\". Known: startx, start, stop, restart, attach."
95 exit 1;;
96 esac
97}
98
99main "$*"
This page took 0.040663 seconds and 4 git commands to generate.