This repository was archived by the owner on Jun 18, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.sh
More file actions
executable file
·216 lines (190 loc) · 6.23 KB
/
build.sh
File metadata and controls
executable file
·216 lines (190 loc) · 6.23 KB
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
215
216
#!/usr/bin/env bash
set -eu
CurrentDir="$(cd "$(dirname "${0}")" || exit 1 ; pwd)"
SrcLibDir="${CurrentDir}/lib"
SrcDataDir="$CurrentDir/data"
QctlLibDir="$SrcLibDir"
LoadShells=("${SrcLibDir}/main.sh")
Remove_Line=(
"#!/usr/bin/env bash"
"%COMMON_CODE%"
)
ExcludeBase=false
# Load main library
for file in "${LoadShells[@]}"; do
[[ -e "${file}" ]] && source "${file}"
done
: "${BaseDir="/"}"
: "${EtcDir="/etc/qctl"}"
: "${LibDir="/lib/qctl"}"
: "${BinDir="/bin"}"
: "${DataDir="/usr/share/qctl"}"
BuildMode=""
TargetPath=""
HelpDoc(){
echo "usage build.sh [option]"
echo
echo "Build qctl and install to the dir"
echo
echo " Install options:"
echo " -s | --script [path] Build the specified script"
echo " Script will be outputed to stdin"
echo " -i | --install Build all scripts and install"
echo " Outout directory can be specified"
echo
echo " Install options:"
echo " -b | --basedir [dir] The path of root. etc, bin and lib files will be installed here"
echo " Default: ${BaseDir-""}"
echo " -e | --etcdir [dir] The path of config directory under the base dir"
echo " Default: ${EtcDir-""}"
echo " -l | --libdir [dir] The path of library directory under the base dir"
echo " Default: ${LibDir-""}"
echo " -c | --bindir [dir] The path of commands directory under the base dir"
echo " Default: ${BinDir-""}"
echo " -d | --datadir [dir] The path of misc data directory under the base dir"
echo " Default: ${DataDir-""}"
echo " --excludebase Specify the path including basedir in the script"
echo " Please read following message for detail"
echo
echo " About \"--excludebase\""
echo "By default, installed scripts use the path containing basedir. "
echo "This is useful if you are installing directly on your system."
echo "Specify this option if you do not want the directory name to include basedir for reasons such as packaging."
echo
echo "For example, if you use this option, the library itself will be installed in <basedir>/<libdir>, "
echo "but the actual command will try to load <libdir>."
echo
echo " General options:"
echo " -h This help message"
}
BuildScript(){
# 削除する行の一覧
readarray -t _Grep_Args < <(
for _Str in "${Remove_Line[@]}"; do
printf -- "-e\n"
echo "${_Str}"
done
)
# CommonCodeの挿入箇所
local _CommonCodeLine
_CommonCodeLine="$(sed -n "/%COMMON_CODE%/=" "${1}")"
echo "#1/usr/bin/env bash"
if [[ -n "${_CommonCodeLine}" ]]; then
sed "${_CommonCodeLine}r ${CurrentDir}/src/qctl-common" "${1}"
else
cat "${1}"
fi | \
grep -Fxv "${_Grep_Args[@]}" | \
sed '/^$/d' | \
if [[ "${ExcludeBase}" = true ]]; then
sed "s|%LIB_DIR%|${LibDir}|g" | \
sed "s|%DATA_DIR%|${DataDir}|g" | \
sed "s|%CONFIG_FILE%|$EtcDir/qctl.conf|g"
else
sed "s|%LIB_DIR%|${LibFullPath}|g" | \
sed "s|%DATA_DIR%|${DataFullPath}|g" | \
sed "s|%CONFIG_FILE%|$EtcFullPath/qctl.conf|g"
fi
}
RunInstall(){
local _Dir
for _Dir in Etc Bin Data Lib; do
eval "mkdir -p \"\${${_Dir}FullPath}\""
eval "${_Dir}FullPath=\"\$(readlinkf \"\${${_Dir}FullPath}\")\""
done
EtcFullPath=$(readlinkf "${EtcFullPath}")
# Install /src/ to /bin
local _File
while read -r _File; do
BuildScript "${_File}" > "${BinFullPath}/$(basename "${_File}")"
chmod 755 "${BinFullPath}/$(basename "${_File}")"
done < <(find "${CurrentDir}/src/" -mindepth 1)
# Install /lib to /lib
while read -r _File; do
cat "${_File}" > "${LibFullPath}/$(basename "${_File}")"
chmod 755 "${LibFullPath}/$(basename "${_File}")"
done < <(find "${CurrentDir}/lib/" -mindepth 1)
# Install qctl.conf to /etc
cat "$CurrentDir/qctl.conf" > "${EtcFullPath}/qctl.conf"
# Install /data to /usr/share
while read -r _File; do
cp "${_File}" "${DataFullPath}"
done < <(find "${CurrentDir}/data" -mindepth 1)
}
Main(){
case "${BuildMode}" in
"Script")
BuildScript "${TargetPath}"
;;
"Install")
RunInstall
;;
"")
MsgError "There is nothing to do."
exit 0
;;
esac
}
# Parse options
OPTS=("s:" "i" "b:" "e:" "l:" "c:" "h") OPTL=("script:" "install" "basedir:" "etcdir:" "libdir:" "bindir:" "help" "excludebase")
ParseCmdOpt SHORT="$(printf "%s" "${OPTS[@]}")" LONG="$(printf "%s," "${OPTL[@]}")" -- "${@}" || exit 1
eval set -- "${OPTRET[@]}"
unset OPTRET OPTS OPTL
while true; do
case "${1}" in
-s | --script)
BuildMode="Script"
TargetPath="${2}"
shift 2
;;
-i | --install)
BuildMode="Install"
shift 1
;;
-b | --basedir)
BaseDir="${2}"
shift 2
;;
-e | --etcdir)
EtcDir="${2}"
shift 2
;;
-l | --libdir)
LibDir="${2}"
shift 2
;;
-c | --bindir)
BinDir="$2"
shift 2
;;
-d | --datadir)
DataDir="$2"
shift 2
;;
-h | --help)
HelpDoc
exit 0
;;
--excludebase)
ExcludeBase=true
shift 1
;;
--)
shift 1
break
;;
*)
MsgError "Argument exception error '${1}'"
MsgError "Please report this error to the developer." 1
;;
esac
done
#EtcFullPath="$(readlinkf "$BaseDir/$EtcDir")"
#LibFullPath="$(readlinkf "$BaseDir/$LibDir")"
#BinFullPath="$(readlinkf "$BaseDir/$BinDir")"
#DataFullPath="$(readlinkf "$BaseDir/$DataDir")"
EtcFullPath="$BaseDir/$EtcDir"
LibFullPath="$BaseDir/$LibDir"
BinFullPath="$BaseDir/$BinDir"
DataFullPath="$BaseDir/$DataDir"
Main "${@}"