-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathz16.sh
executable file
·184 lines (168 loc) · 4.22 KB
/
z16.sh
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
#!/usr/bin/env bash
#
# author: @bekcpear
# license: GPLv2
#
# Dependencies:
#
# * sys-apps/coreutils
# |-- chown
# |-- chmod
# |-- cp
# |-- date
# |-- echo
# |-- id
# |-- ln
# |-- ls
# |-- mkdir
# |-- readlink (GNU version)
# |-- realpath
# |-- rmdir
# |-- touch
# `-- unlink
#
# * sys-apps/grep
# `-- grep
#
# * sys-apps/util-linux
# `-- getopt (Linux version)
#
# * sys-libs/glibc
# `-- getent
#
# * net-misc/openssh [optional]
# |-- scp
# `-- ssh
# paramaters:
# commands: init|load|fetch|unload|config|list|help
# optional options: --/-OPT VALUE !!!TODO
# argument: [(<INSTANCE NAME>)]
# file structure:
# <MAIN-FOLDER>
# |-- .z16.g.conf (global configuration file)
# |-- <INSTANCE-NAME-0>
# |-- .z16.l.conf (default name)
# |-- <FILE/DIRECTORY>
# | # support prefixed with `dot-` instead of `.`
# `-- ...
# |-- <INSTANCE-NAME-1>
# |-- <INSTANCE-NAME-2>
# `-- ...
#
# TODO: merge specified path
# TODO: remove unlinked links when loading instances
# TODO: rename instance
# TODO: generate instances through a path list file and restore
# TODO: database of loaded instances
# TODO: about SUID/SGID
# TODO: SELinux support
#
#
set +abfhkmnptuvBCEHPT
set -Beh
AVAILABLECMD="init|config|load|fetch|unload"
AVAILABLECMD_NOARGS="list|help"
declare -a INSTANCES
declare -A CONFIGS
declare -a IGNORES
CMD='help'
#READONLY VARIABLE
declare -r Z16_TMPDIR='/tmp/.z16_tmpdir_de82969a-064c-43d7-b761-6061eb1669f8'
Z16_OWSP_P='_bd3c8c_z16_ownership'
#DEFAULT VARIABLES THAT CAN ONLY BE MODIFIED BY COMMAND LINE OPTION
FORCEOVERRIDE=0
PRETEND=0
Z16_SSH_RAW=
declare -A Z16_SSH=([IDENTITYOPTS]="" [USER]="" [HOSTNAME]="" [PORT]=22 [MUX_TIMEOUT]=600 [KEEP]=0)
VERBOSEOUT1=/dev/null
VERBOSEOUT2=/dev/null
declare -r SYSCONFPATH="/etc/z16/z16rc"
CONFPATH="${HOME%/}/.config/z16/z16rc"
#DEFAULT VARIABLES
D_VARS_Z16=(
INSTDIR
INSTGLOBALCONFNAME
)
eval "D_${D_VARS_Z16[0]}='${HOME%/}/.local/share/z16'"
eval "D_${D_VARS_Z16[1]}='.z16.g.conf'"
D_VARS_L=(
PARENTDIR
USER
GROUP
IGNORE
)
D_VARS_G=(
INSTLOCALCONFNAME
${D_VARS_L[@]}
)
eval "D_${D_VARS_G[0]}='.z16.l.conf'"
eval "D_${D_VARS_G[1]}='/tmp/z16.tmp.d'"
eval "D_${D_VARS_G[2]}=''"
eval "D_${D_VARS_G[3]}=''"
eval "D_${D_VARS_G[4]}=''"
CUSER=
CGROUP=
CGROUPS=
SPATH="${0}"
if [[ -L "${SPATH}" ]]; then
eval "SPATH=\$(realpath '${SPATH}')"
fi
source "${SPATH%/*}"/meta.sh
source "${SPATH%/*}"/helper.sh
source "${SPATH%/*}"/parse.sh
source "${SPATH%/*}"/main.sh
# parse shell parameters
#
parseparam "$@"
# do initialize configurations
#
source "${SPATH%/*}"/initZ16.sh
# parse shell configurations
#
function _mkvarnotempty() {
local -i idx
for (( idx = 0; idx < ${#D_VARS_Z16[@]}; ++idx )); do
eval "CONFIGS[${D_VARS_Z16[idx]}]=\${CONFIGS[${D_VARS_Z16[idx]}]:-\${D_${D_VARS_Z16[idx]}}}"
done
}
if [[ -e "${SYSCONFPATH}" ]]; then
parseconfigs "${SYSCONFPATH}" D_VARS_Z16 _ CONFIGS
_mkvarnotempty
fi
if [[ -e "${CONFPATH}" ]]; then
parseconfigs "${CONFPATH}" D_VARS_Z16 _ CONFIGS
_mkvarnotempty
fi
eval "CONFIGS[${D_VARS_Z16[0]}]=\$(_absolutepath '${CONFIGS[${D_VARS_Z16[0]}]}')"
_fatalunsupportedpath "${CONFIGS[${D_VARS_Z16[0]}]}"
set +e
# parse instance global configurations
#
parseconfigs "${CONFIGS[${D_VARS_Z16[0]}]%/}/${CONFIGS[${D_VARS_Z16[1]}]#/}" D_VARS_G IGNORES CONFIGS
if [[ ${?} != 0 ]]; then
printlog "You may not configure correctly." warn
fatalerr "Parse global configurations failed!"
fi
for (( idx = 0; idx < ${#D_VARS_G[@]}; ++idx )); do
eval "CONFIGS[${D_VARS_G[idx]}]=\${CONFIGS[${D_VARS_G[idx]}]:-\${D_${D_VARS_G[idx]}}}"
done
if [[ ${CMD} =~ ${AVAILABLECMD} && ${CMD} != init ]]; then
# parse instance local configurations
#
for (( idx = 0; idx < ${#INSTANCES[@]}; ++idx )); do
eval "declare -A CONFIGS_${idx}"
eval "declare -a IGNORES_${idx}"
eval "parseconfigs \
"${CONFIGS[${D_VARS_Z16[0]}]%/}"/"${INSTANCES[idx]}"/"${CONFIGS[${D_VARS_G[0]}]#/}" \
D_VARS_L IGNORES_${idx} CONFIGS_${idx}"
if [[ ${?} != 0 ]]; then
printlog "You may need to initialize instances \"${INSTANCES[idx]}\" first." warn
fatalerr "Parse configurations of \"${INSTANCES[idx]}\" failed."
fi
done
fi
set -e
# exec main commands
#
execmain
# vim: et:ts=2:sts:sw=2