-
Notifications
You must be signed in to change notification settings - Fork 1
/
clipctl
executable file
·82 lines (73 loc) · 1.68 KB
/
clipctl
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
#!/bin/sh
clipmanager_name="clipmanager"
clipmanager_pid=$(pgrep -u "$(id -u)" -nf "$clipmanager_name$")
log()
{
# printf "%s\n" "$1" | xargs
printf "\033[31m->\033[97m %s\033[0m\n" "$1" | xargs
}
redirect()
{
exec </dev/null 1>&0 2>&0
}
fork()
{
exec setsid -f -- "$@"
}
spawn()
{
redirect
fork "$@"
}
usage()
{
echo "clipctl"
echo " enable: enable clipmanager"
echo " disable: disable clipmanager"
echo " run/start: start clipmanager"
echo " kill/stop: kill clipmanager"
echo " restart: restart clipmanager"
echo " status: check if clipmanager is running"
echo " clear: clear clipboard"
echo " help: help menu"
exit
}
is_running()
{
if [ -z "$clipmanager_pid" ]; then
log "Clipmanager is not running"
return 1
else
return 0
fi
}
is_running_inv()
{
if [ -z "$clipmanager_pid" ]; then
return 1
else
log "Clipmanager is already running"
return 0
fi
}
clip_kill()
{
kill -TERM "$clipmanager_pid" && printf "kill" | xclip -selection clipboard
}
case "$1" in
"enable") is_running && kill -USR2 "$clipmanager_pid" ;;
"disable") is_running && kill -USR1 "$clipmanager_pid" ;;
"run"|"start") is_running_inv || spawn clipmanager ;;
"kill"|"stop") is_running && clip_kill ;;
"restart")
if [ -n "$clipmanager_pid" ]; then
clip_kill && spawn clipmanager
else
spawn clipmanager
fi
;;
"status") is_running && is_running_inv ;;
"clear") printf "clear" | xclip -selection clipboard ;;
"help") usage ;;
*) log "Invalid argument. Try help for help" ;;
esac