forked from Botspot/pi-apps
-
Notifications
You must be signed in to change notification settings - Fork 0
/
settings
executable file
·199 lines (161 loc) · 7.03 KB
/
settings
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
#!/bin/bash
DIRECTORY="$(readlink -f "$(dirname "$0")")"
source "${DIRECTORY}/api" || error "failed to source ${DIRECTORY}/api"
function error {
echo -e "\e[91m$1\e[39m"
exit 1
}
#ensure settings dir exists
if [ ! -d "${DIRECTORY}/data/settings" ];then
echo "creating settings directory"
mkdir -p "${DIRECTORY}/data/settings"
#create default files inside
echo '' > "${DIRECTORY}/data/settings/reinstall-after-update"
fi
#$1 is usually left blank. If it equals 'refresh', then empty settings will be created and then the script will exit.
if [ "$1" == 'refresh' ];then
#set default settings, if they don't exist
settings="$(ls "${DIRECTORY}/etc/setting-params" | tr '\n' '|')"
PREIFS="$IFS"
IFS='|'
for name in $settings
do
if [ ! -f "${DIRECTORY}/data/settings/${name}" ] || [ -z "$(cat "${DIRECTORY}/data/settings/${name}")" ];then
cat "${DIRECTORY}/etc/setting-params/${name}" | grep -v '#' | head -n1 > "${DIRECTORY}/data/settings/${name}"
fi
done
IFS="$PREIFS"
exit 0
elif [ "$1" == 'revert' ];then #If $1 equals 'revert', then overwrite all settings with the defaults and then the script will exit.
#overwrite all settings with the defaults
settings="$(ls "${DIRECTORY}/etc/setting-params" | tr '\n' '|')"
PREIFS="$IFS"
IFS='|'
for name in $settings
do
cat "${DIRECTORY}/etc/setting-params/${name}" | grep -v '#' | head -n1 > "${DIRECTORY}/data/settings/${name}"
done
IFS="$PREIFS"
exit 0
fi
if [ ! -f ~/.local/share/applications/pi-apps-settings.desktop ];then
echo "Creating Settings menu button"
echo "[Desktop Entry]
Name=Pi-Apps Settings
Comment=Configure Pi-Apps or create an App
Exec=${DIRECTORY}/settings
Icon=${DIRECTORY}/icons/settings.png
Terminal=false
StartupWMClass=Pi-Apps-Settings
Type=Application
Categories=Settings;
StartupNotify=true" > ~/.local/share/applications/pi-apps-settings.desktop
fi
exitloop=''
while [ -z $exitloop ];do
settings="$(ls "${DIRECTORY}/etc/setting-params" | tr '\n' '|')"
yadparams=()
tooltips=()
PREIFS="$IFS"
IFS='|'
# generate yad/xlunch themes based on system installed themes
# yad is compiled using gtk3 on all distros so only gtk3 themes are valid
theme_dirs=( /usr/share/themes ~/.local/share/themes ~/.themes )
# begin array with yad default theme
declare gtk3_themes=("default")
for td in "${theme_dirs[@]}"; do
for th in "$td"/*; do
[[ -d "$th" ]] || continue
thname=${th##*/}
[ "$thname" != Default ] && [ $(ls "$th/gtk-3."* 2>/dev/null | wc -l ) -gt 0 ] && gtk3_themes+=("$thname")
done
done
gtk3_themes=( "${gtk3_themes[@]/#/yad-}" )
# add xlunch preset themes to the array
gtk3_themes+=( "xlunch-dark" )
gtk3_themes+=( "xlunch-dark-3d" )
gtk3_themes+=( "xlunch-light-3d" )
params=$( IFS=$'\n'; echo "${gtk3_themes[*]}" )
unset gtk3_themes
name="App List Style"
#create file if necessary
if [ ! -f "${DIRECTORY}/data/settings/${name}" ] || [ -z "$(cat "${DIRECTORY}/data/settings/${name}")" ];then
cat "${DIRECTORY}/etc/setting-params/${name}" | grep -v '#' | head -n1 > "${DIRECTORY}/data/settings/${name}"
fi
#get current setting
curval="$(cat "${DIRECTORY}/data/settings/${name}")"
#order params, with selected option first
params="$(echo "$params" | grep -x "$curval")
$(echo "$params" | grep -vx "$curval")"
# remove blank lines (like if the current selected option no longer exists) and format as one line output separated by !
params="$(echo "$params" | awk NF | tr '\n' '!')"
params="${params::-1}"
tooltip="Pi-Apps can display the apps as a compact list (yad), or as a group of larger icons. (xlunch)"
yadparams+=(--field=:CB "${params}")
tooltips+=(--field="${name}":!!"${tooltip}":BTN '')
unset name
unset tooltip
unset params
unset curval
for name in $settings
do
[ "${name}" == "App List Style" ] && continue
params="$(cat "${DIRECTORY}/etc/setting-params/${name}" | grep -v '#')"
#create file if necessary
if [ ! -f "${DIRECTORY}/data/settings/${name}" ] || [ -z "$(cat "${DIRECTORY}/data/settings/${name}")" ];then
cat "${DIRECTORY}/etc/setting-params/${name}" | grep -v '#' | head -n1 > "${DIRECTORY}/data/settings/${name}"
fi
#get current setting
curval="$(cat "${DIRECTORY}/data/settings/${name}")"
#order params, with selected option first
params="$(echo "$params" | grep -x "$curval")
$(echo "$params" | grep -vx "$curval")"
params="$(echo "$params" | tr '\n' '!')"
params="${params::-1}"
tooltip="$(cat "${DIRECTORY}/etc/setting-params/${name}" | grep '^#' | tr -d '#' | tr '\n' '\r')"
tooltip="${tooltip::-1}"
#echo "Params of ${name}: ${params}"
#echo "Tooltip of ${name}: ${tooltip}"
yadparams+=(--field=:CB "${params}")
tooltips+=(--field="${name}":!!"${tooltip}":BTN '')
done
IFS="$PREIFS"
output="$(yad --class Pi-Apps-Settings --name "Pi-Apps Settings" --center --title='Pi-Apps Settings' --width=310 --height=300 \
--form --separator='\n' --window-icon="${DIRECTORY}/icons/settings.png" --columns=2 \
"${tooltips[@]}" \
--field='Categories'!"${DIRECTORY}/icons/categories.png"!"Does an App belong in Editors instead of Tools? This lets you move it.":FBTN "bash -c '${DIRECTORY}/etc/categoryedit 1>&2'" \
--field='Log files'!"${DIRECTORY}/icons/log-file.png"!"View past installation logs. Useful for debugging, or to see what you installed yesterday.":FBTN "bash -c '${DIRECTORY}/etc/logviewer 1>&2'" \
--field='Multi-Install'!"${DIRECTORY}/icons/multi-select.png"!"Install multiple apps at the same time.":FBTN "bash -c '${DIRECTORY}/api multi_install_gui 1>&2'" \
"${yadparams[@]}" \
--field='New App'!"${DIRECTORY}/icons/create.png"!"Make your own app! It's pretty easy if you follow the instructions.":FBTN "bash -c '${DIRECTORY}/createapp 1>&2'" \
--field='Import App'!"${DIRECTORY}/icons/categories/Imported.png"!"Did someone else make an app but it's not on Pi-Apps yet? Import it here.":FBTN "bash -c '${DIRECTORY}/etc/import-app 1>&2'" \
--field='Multi-Uninstall'!"${DIRECTORY}/icons/multi-select.png"!"Uninstall multiple apps at the same time.":FBTN "bash -c '${DIRECTORY}/api multi_uninstall_gui 1>&2'" \
--button='Reset'!"${DIRECTORY}/icons/backup.png"!'Reset all settings to their defaults':2 \
--button=Cancel!"${DIRECTORY}/icons/exit.png":1 \
--button=Save!"${DIRECTORY}/icons/check.png":0 \
)"
button=$? #get exit code to determine which button was pressed
#exit if save was not clicked
[ $button -ne 0 ] && [ $button -ne 2 ] && exit 0
if [ $button -eq 2 ];then
output=''
"${0}" revert
else
exitloop=yes
fi
done
#remove empty lines from $output
output="$(echo "$output" | grep .)"
echo "Output: ${output}EOO"
settings="$(ls "${DIRECTORY}/etc/setting-params" | tr '\n' '|')"
settingnumber=1
PREIFS="$IFS"
IFS='|'
for name in $settings
do
curval="$(echo "$output" | sed -n "${settingnumber}p")"
echo "Setting '$name' to '$curval'"
echo "$curval" > "${DIRECTORY}/data/settings/${name}"
settingnumber=$((settingnumber + 1))
done
IFS="$PREIFS"