Skip to content

Commit febc4b9

Browse files
committed
tcu: Theme Config Utility
1 parent edab6ea commit febc4b9

File tree

1 file changed

+264
-0
lines changed

1 file changed

+264
-0
lines changed

tcu

Lines changed: 264 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,264 @@
1+
#!/usr/bin/env bash
2+
3+
VERSION=1.0.0
4+
5+
BASH=`which bash`
6+
BASH_VERSION=`$BASH --version |head -1 |sed 's/.*version \(.*\)-release.*/\1/' |cut -c 1`
7+
if [ $BASH_VERSION -lt 4 ]; then
8+
echo "Error: bash must be at least version 4."
9+
exit 1
10+
fi
11+
12+
# Retrieve script directory (not following link)
13+
SCRIPT="${BASH_SOURCE[0]}"
14+
SCRIPT_DIR="$( cd "$( dirname "${SCRIPT}" )" && pwd )"
15+
INVOCATION_DIR=`pwd`
16+
PDIR="$(dirname "$SCRIPT_DIR")"
17+
BDIR="$(basename "$SCRIPT_DIR")"
18+
19+
# Retrieve script name (follow link)
20+
PROG=`basename "$0"`
21+
22+
# Themes are stored in an array
23+
# key: lowercase theme, value: directory of the theme
24+
declare -A THEME
25+
THEME[dark]=Dark
26+
27+
DFT_THEME=dark
28+
29+
# on macOS, the default getopt is a useless piece of shit
30+
# install getopt via MacPorts or brew and put it in the PATH before /usr/bin
31+
if [ "`getopt -V |cut -d\" \" -f1`" != "getopt" ]; then
32+
echo "Error: getopt not compatible enough."
33+
exit 1
34+
fi
35+
36+
function usage {
37+
echo "usage: ${PROG} [-t|--theme THEME] [-p|--profile PROFILE_DIR] [-x|--no-theme] [-c|--copy] [-d|--debug] [-V|--version] [-h] [--help]"
38+
}
39+
40+
function long_usage {
41+
echo "${PROG} - Theme Config Utility "
42+
echo ""
43+
usage
44+
echo ""
45+
echo " -t, --theme THEME "
46+
echo " activate THEME "
47+
echo " available themes: "
48+
for i in ${!THEME[@]}
49+
do
50+
echo " $i"
51+
done
52+
echo " none (same as -x, --no-theme) "
53+
echo ""
54+
echo " -p, --profile PROFILE_DIR "
55+
echo " Joplin profile directory "
56+
echo ""
57+
echo " -x, --no-theme "
58+
echo " deactivate current theme "
59+
echo ""
60+
echo " -d, --debug "
61+
echo " print debug information "
62+
echo ""
63+
echo " -c, --copy "
64+
echo " copy files instead of creating symbolic links "
65+
echo ""
66+
echo " -V, --version "
67+
echo " version information "
68+
echo ""
69+
echo " -h "
70+
echo " usage information "
71+
echo ""
72+
echo " --help "
73+
echo " this help "
74+
echo ""
75+
}
76+
77+
tflag=0
78+
targ=$DFT_THEME
79+
pflag=0
80+
xflag=0
81+
cflag=0
82+
dflag=0
83+
Vflag=0
84+
hflag=0
85+
hlflag=0
86+
87+
options=$(getopt -n $PROG -q -o t:p:xcdVh -l help,version,debug,theme:,profile:,no-theme,copy -- "$@")
88+
if [ $? != 0 ]
89+
then
90+
echo "${PROG}: Error: unrecognized option $1"
91+
usage
92+
exit 1
93+
fi
94+
95+
eval set -- "$options"
96+
97+
while [ $# -gt 0 ]
98+
do
99+
case "$1" in
100+
-t|--theme)
101+
tflag=1
102+
targ=${2,,}
103+
shift;;
104+
-p|--profile)
105+
pflag=1
106+
parg=$2
107+
shift;;
108+
-x|--no-theme)
109+
xflag=1;;
110+
-c|--copy)
111+
cflag=1;;
112+
-V|--version)
113+
Vflag=1;;
114+
-d|--debug)
115+
dflag=1;;
116+
-h)
117+
hflag=1;;
118+
--help)
119+
hlflag=1;;
120+
--)
121+
shift; break;;
122+
*)
123+
exit;
124+
break;;
125+
esac
126+
shift
127+
done
128+
129+
if [ "$hflag" == "1" ]; then
130+
usage
131+
exit
132+
fi
133+
134+
if [ "$hlflag" == "1" ]; then
135+
long_usage
136+
exit
137+
fi
138+
139+
if [ "$Vflag" == "1" ]; then
140+
echo "$PROG $VERSION"
141+
exit
142+
fi
143+
144+
function debug {
145+
if [ "$dflag" == "1" ]; then
146+
echo "[debug] $@"
147+
fi
148+
}
149+
150+
if [ "$xflag" == "1" ]; then
151+
tflag=1
152+
targ=none
153+
fi
154+
155+
if [ "$tflag" == "0" ]; then
156+
debug "Using default theme: $DFT_THEME"
157+
fi
158+
159+
if [[ ${THEME[$targ]+_} || $targ == "none" ]]; then
160+
if [ $targ == "none" ]; then
161+
THEME_DIR=None
162+
else
163+
THEME_DIR=${THEME[$targ]}
164+
fi
165+
else
166+
echo "Error: available themes: ${!THEME[@]} none"
167+
exit
168+
fi
169+
170+
if [ $targ != "none" ]; then
171+
debug "Theme: $targ [$THEME_DIR]"
172+
else
173+
debug "Deactivate current theme"
174+
fi
175+
176+
if [ "$pflag" == "1" ]; then
177+
if [ -d "$parg" ]; then
178+
PROFILE_DIR="$parg"
179+
else
180+
echo "Error: profile directory [$parg] does not exist."
181+
exit 1
182+
fi
183+
else
184+
# Check, if database.sqlite exists in parent dir -> then it's the profile dir
185+
if [ -f "$PDIR/database.sqlite" ]; then
186+
PROFILE_DIR="$PDIR"
187+
elif [ -d "$HOME/.config/joplin-desktop" ]; then
188+
PROFILE_DIR=$HOME/.config/joplin-desktop
189+
else
190+
echo "Error: No profile directory found. Please specify with -p or --profile"
191+
exit 1
192+
fi
193+
fi
194+
195+
debug "Profile directory: $PROFILE_DIR"
196+
197+
cd $PROFILE_DIR
198+
199+
# extension for backup file (if target is different than source)
200+
EXT=`date +%Y%m%d%H%M%S`
201+
202+
themefiles=("userchrome.css" "userstyle.css")
203+
204+
# Deactivate theme
205+
if [ $targ == "none" ]; then
206+
for f in ${themefiles[*]}
207+
do
208+
if [ -f $f ]; then
209+
B=0
210+
if [ ! -f .theme ]; then
211+
B=1
212+
else
213+
installed=`cat .theme`
214+
215+
# is the data in the file .theme really a valid theme (key)?
216+
if [ ! ${THEME[$installed]+_} ]; then
217+
B=1
218+
# now check if the current file differs from the one in the theme directory
219+
elif ! diff -q $f ${SCRIPT_DIR}/${THEME[$installed]}/$f >/dev/null; then
220+
B=1
221+
fi
222+
fi
223+
if [ $B == 1 ]; then
224+
debug "Backup $f -> $f.$EXT"
225+
cp $f $f.$EXT
226+
fi
227+
debug "Remove $f"
228+
rm -f $f
229+
fi
230+
done
231+
rm -f .theme
232+
echo "Current theme deactivated."
233+
exit
234+
fi
235+
236+
# Activate theme
237+
for f in ${themefiles[*]}
238+
do
239+
if [ -f $f ]; then
240+
# check, if we have to backup
241+
if ! diff -q $f ${SCRIPT_DIR}/${THEME_DIR}/$f >/dev/null; then
242+
debug "Backup $f -> $f.$EXT"
243+
cp $f $f.$EXT
244+
fi
245+
rm -f $f
246+
fi
247+
# copy or create links
248+
if [ "$cflag" == "1" ]; then
249+
debug "copy ${SCRIPT_DIR}/${THEME_DIR}/$f -> $f"
250+
cp ${SCRIPT_DIR}/${THEME_DIR}/$f .
251+
else
252+
# check, if we can use relative links
253+
if [ -d $BDIR ]; then
254+
debug "create relative symbolic link [ $f -> $BDIR/${THEME_DIR}/$f]"
255+
ln -fs $BDIR/${THEME_DIR}/$f $f
256+
else
257+
debug "create absolute symbolic link [ $f -> $SCRIPT_DIR/${THEME_DIR}/$f]"
258+
ln -fs $SCRIPT_DIR/${THEME_DIR}/$f $f
259+
fi
260+
fi
261+
done
262+
263+
echo "$targ" >.theme
264+
echo "Theme [$THEME_DIR] activated."

0 commit comments

Comments
 (0)