-
Notifications
You must be signed in to change notification settings - Fork 5
/
tdmctl
executable file
·214 lines (199 loc) · 6.16 KB
/
tdmctl
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#!/bin/bash
# tdmctl: a tool to control and configure tdm.
# This file is part of tdm, a tiny display manager.
#
# tdm is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# tdm is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with tdm. If not, see <http://www.gnu.org/licenses/>.
function load_confdir_path() {
# usage: load_confdir_path
for cfd in "${XDG_CONFIG_HOME:-$HOME/.config}/tdm" "$HOME/.tdm" ; do
if [ -d "$cfd" ] ; then
echo "$cfd"
return
fi
done
echo "${XDG_CONFIG_HOME:-$HOME/.config}/tdm"
}
init(){
[[ "$1" = "-f" || "$1" = "--force" ]]&&rm -rf "${CONFDIR}"
# build the directory tree if not exist
if [[ ! -d "${CONFDIR}" ]]; then
cp -Rv "${PREFIX}/share/tdm" "${CONFDIR}"
[ ! -d "${CONFDIR}/extra" ] && mkdir "${CONFDIR}/extra"
else
echo "Nothing done (configuration directory already present)."
echo "If you were asked to run 'tdmctl init', please check your configuration or remove '${CONFDIR}' and re-run 'tdmctl init'"
fi
}
usage(){
echo "tdmctl init: initialize the config directory."
echo "tdmctl list: list available sessions (X and extra)."
echo "tdmctl cache: list cached files."
echo "tdmctl check [extra/]<session>: see what <session> is."
echo "tdmctl default [session]: show/set default X session."
echo "tdmctl add <name> <path> [X(default)/extra]: add a session."
echo "tdmctl remove <name>"
echo "tdmctl enable/disable <session>: enable/disable session."
echo "tdmctl migrate"
exit
}
verify(){
if [ ! -d "${CONFDIR}" ] ; then
echo "Configuration directory '${CONFDIR}' does not exist"
echo "Please run 'tdmctl init' first"
exit
fi
if [ ! -d "${CONFDIR}/sessions" ] ; then
echo "Configuration directory '${CONFDIR}' does not exist"
echo "Please fix your configuration"
exit
fi
if [ ! -d "${CONFDIR}/extra" ] ; then
echo "Configuration directory '${CONFDIR}/extra' does not exist"
echo "Please fix your configuration"
exit
fi
}
check(){
readlink "$1" || cat "$1" 2>/dev/null || echo "$1 not found or missing read permission"
}
function migrate() {
if [ "$CONFDIR" != "${XDG_CONFIG_HOME:-$HOME/.config}/tdm" ] ; then
mv -v "$CONFDIR" "${XDG_CONFIG_HOME:-$HOME/.config}/tdm"
else
echo "A configuration is already present in '${XDG_CONFIG_HOME:-$HOME/.config}/tdm'. Please migrate manually."
fi
}
CONFDIR="$(load_confdir_path)"
CACHEDIR="${CONFDIR}/cache"
PREFIX=/usr/local
if [ "$CONFDIR" == "$HOME/.tdm" ] ; then
echo "Following tdm v1.3.0, the configuration files should be moved to '${XDG_CONFIG_HOME:-$HOME/.config}/tdm' in order to become XDG compliant"
echo "Use 'tdmctl migrate' to automatically migrate your configuration to the new location"
echo "Support for the old configuration directory ($CONFDIR) will be dropped in tdm 2.x.y"
fi
if [ ! -n "$1" ]; then
usage
exit
fi
case "$1" in
init)
shift
init "$@"
;;
list)
verify
for session in "$CONFDIR/sessions"/*; do
[ -x "$session" ] && basename "$session"
done
for session in "$CONFDIR/extra"/*; do
[ -x "$session" ] && echo "extra/$(basename "$session")"
done
;;
cache)
verify
for file in "$CACHEDIR"/*; do
fn=$(basename "$file")
echo "${fn:1}"
done
;;
default)
verify
if [ ! -n "$2" ]; then
if [ ! -L "$CONFDIR/default" ] ; then
echo "No default session found"
exit 1
fi
echo "Checking $(readlink "$CONFDIR/default")"
check "$(readlink "$CONFDIR/default")"
else
if [[ "$2" == extra/* ]] ; then
FILE="$CONFDIR/$2"
else
FILE="$CONFDIR/sessions/$2"
fi
if [ -x "$FILE" ]; then
echo "Setting default to $2"
ln -sf "$FILE" "$CONFDIR/default"
else
echo "tdmctl error: $2 is not available"
fi
fi
;;
check)
verify
if [ ! -n "$2" ]; then
usage
fi
if [[ "$2" == extra/* ]] ; then
FILE="$CONFDIR/$2"
else
FILE="$CONFDIR/sessions/$2"
fi
if [ -f "$FILE" ]; then
check "$FILE"
else
echo "$2 not exist!"
exit 1
fi
;;
add)
verify
[ -n "$3" ]||usage
if [[ "$4" == "X" || "$4" == "" ]]; then
ln -s "$3" "${CONFDIR}/sessions/$2"
elif [ "$4" == "extra" ]; then
ln -s "$3" "${CONFDIR}/extra/$2"
else
usage
fi
;;
remove)
verify
[ -n "$2" ] || usage
if [ -L "${CONFDIR}/sessions/$2" ]; then
rm -v "${CONFDIR}/sessions/$2"
fi
if [ -L "${CONFDIR}/extra/$2" ]; then
rm -v "${CONFDIR}/extra/$2"
fi
;;
enable)
verify
if [ -L "${CACHEDIR}/X$2" ]; then
mv -v "${CACHEDIR}/X$2" "${CONFDIR}/sessions/$2"
fi
if [ -L "${CACHEDIR}/E$2" ]; then
mv -v "${CACHEDIR}/E$2" "${CONFDIR}/extra/$2"
fi
;;
disable)
verify
if [ ! -d "${CACHEDIR}" ]; then
mkdir -p "${CACHEDIR}"
fi
# backup to cache
if [ -L "${CONFDIR}/sessions/$2" ]; then
mv -v "${CONFDIR}/sessions/$2" "${CACHEDIR}/X$2"
fi
if [ -L "${CONFDIR}/extra/$2" ]; then
mv -v "${CONFDIR}/extra/$2" "${CACHEDIR}/E$2"
fi
;;
migrate)
migrate
;;
*)
usage
;;
esac