OSDN Git Service

[update] : msg and repouodate
[alterlinux/alterlinux-repository.git] / scripts / main.sh
1 #!/usr/bin/env bash
2 #
3 # Yamada Hayao
4 # Twitter: @Hayao0819
5 # Email  : hayao@fascode.net
6 #
7 # (c) 2019-2020 Fascode Network.
8 #
9 # Repository management script
10 # Executed via administrator web and API of YamaD Server 
11 #
12
13 script_name=$(basename ${0})
14
15 script_path="$(readlink -f ${0%/*})"
16
17 arch="$(uname -m)"
18 git_url="https://github.com/FascodeNet/alterlinux-pkgbuilds"
19 repo_name="alter-stable"
20
21 work_dir="${script_path}/work"
22 repo_dir="${script_path}/repo"
23
24 debug=false
25
26 command=""
27
28 set -e
29
30 # usage <exit code>
31 _usage() {
32     echo "usage ${0} [options] [repository] [command]"
33     echo
34     echo " General options:"
35     echo
36     echo "    -a | --arch <arch>             Specify the architecture"
37     echo "    -r | --repodir <dir>           Specify the repository dir"
38     echo "    -g | --giturl <url>            Specify the URL of the repository where the PKGBUILD list is stored"
39     echo "    -w | --workdir <dir>           Specify the work dir"
40     echo "    -h | --help                    This help messageExecuted via administrator web and Yama D Saba APIs"
41     echo
42     echo "    list                      List packages"
43     echo "    build                     BUold all packages"
44
45     if [[ -n "${1:-}" ]]; then
46         exit "${1}"
47     fi
48 }
49
50
51 # Color echo
52 # usage: echo_color -b <backcolor> -t <textcolor> -d <decoration> [Text]
53 #
54 # Text Color
55 # 30 => Black
56 # 31 => Red
57 # 32 => Green
58 # 33 => Yellow
59 # 34 => Blue
60 # 35 => Magenta
61 # 36 => Cyan
62 # 37 => White
63 #
64 # Background color
65 # 40 => Black
66 # 41 => Red
67 # 42 => Green
68 # 43 => Yellow
69 # 44 => Blue
70 # 45 => Magenta
71 # 46 => Cyan
72 # 47 => White
73 #
74 # Text decoration
75 # You can specify multiple decorations with ;.
76 # 0 => All attributs off (ノーマル)
77 # 1 => Bold on (太字)
78 # 4 => Underscore (下線)
79 # 5 => Blink on (点滅)
80 # 7 => Reverse video on (色反転)
81 # 8 => Concealed on
82
83 echo_color() {
84     local backcolor
85     local textcolor
86     local decotypes
87     local echo_opts
88     local arg
89     local OPTIND
90     local OPT
91     
92     echo_opts="-e"
93     
94     while getopts 'b:t:d:n' arg; do
95         case "${arg}" in
96             b) backcolor="${OPTARG}" ;;
97             t) textcolor="${OPTARG}" ;;
98             d) decotypes="${OPTARG}" ;;
99             n) echo_opts="-n -e"     ;;
100         esac
101     done
102     
103     shift $((OPTIND - 1))
104     
105     echo ${echo_opts} "\e[$([[ -v backcolor ]] && echo -n "${backcolor}"; [[ -v textcolor ]] && echo -n ";${textcolor}"; [[ -v decotypes ]] && echo -n ";${decotypes}")m${*}\e[m"
106 }
107
108
109 # Show an INFO message
110 # $1: message string
111 _msg_info() {
112     local echo_opts="-e"
113     local arg
114     local OPTIND
115     local OPT
116     while getopts 'n' arg; do
117         case "${arg}" in
118             n) echo_opts="${echo_opts} -n" ;;
119         esac
120     done
121     shift $((OPTIND - 1))
122     echo ${echo_opts} "$( echo_color -t '36' "[${script_name}]")    $( echo_color -t '32' 'Info') ${*}"
123 }
124
125
126 # Show an Warning message
127 # $1: message string
128 _msg_warn() {
129     local echo_opts="-e"
130     local arg
131     local OPTIND
132     local OPT
133     while getopts 'n' arg; do
134         case "${arg}" in
135             n) echo_opts="${echo_opts} -n" ;;
136         esac
137     done
138     shift $((OPTIND - 1))
139     echo ${echo_opts} "$( echo_color -t '36' "[${script_name}]") $( echo_color -t '33' 'Warning') ${*}" >&2
140 }
141
142
143 # Show an debug message
144 # $1: message string
145 _msg_debug() {
146     local echo_opts="-e"
147     local arg
148     local OPTIND
149     local OPT
150     while getopts 'n' arg; do
151         case "${arg}" in
152             n) echo_opts="${echo_opts} -n" ;;
153         esac
154     done
155     shift $((OPTIND - 1))
156     if [[ "${debug}" = true ]]; then
157         echo ${echo_opts} "$( echo_color -t '36' "[${script_name}]")   $( echo_color -t '35' 'Debug') ${*}"
158     fi
159 }
160
161
162 # Show an ERROR message then exit with status
163 # $1: message string
164 # $2: exit code number (with 0 does not exit)
165 _msg_error() {
166     local echo_opts="-e"
167     local arg
168     local OPTIND
169     local OPT
170     local OPTARG
171     while getopts 'n' arg; do
172         case "${arg}" in
173             n) echo_opts="${echo_opts} -n" ;;
174         esac
175     done
176     shift $((OPTIND - 1))
177     echo ${echo_opts} "$( echo_color -t '36' "[${script_name}]")   $( echo_color -t '31' 'Error') ${1}" >&2
178     if [[ -n "${2:-}" ]]; then
179         exit ${2}
180     fi
181 }
182
183 # rm helper
184 # Delete the file if it exists.
185 # For directories, rm -rf is used.
186 # If the file does not exist, skip it.
187 # remove <file> <file> ...
188 remove() {
189     local _list
190     local _file
191     _list=($(echo "$@"))
192     for _file in "${_list[@]}"; do
193         if [[ -f ${_file} ]]; then
194             _msg_debug "Removeing ${_file}"
195             rm -f "${_file}"
196             elif [[ -d ${_file} ]]; then
197             _msg_debug "Removeing ${_file}"
198             rm -rf "${_file}"
199         fi
200     done
201 }
202
203
204 # check_file <path> <description> 
205 check_file() {
206     local file_path="${1}"
207     local file_description="${2}"
208     if [[ ! -f "${file_path}" ]]; then
209         _msg_error "${file_description} (${file_path}) does not exist." "1"
210     fi
211 }
212
213
214 # check_command <command>
215 check_command() {
216     local command="${1}"
217     check_file "$(which ${command})" "${command}"
218 }
219
220
221 prepare() {
222     mkdir -p "${repo_dir}/${repo_name}/${arch}"
223     mkdir -p "${work_dir}"
224     check_command makepkg
225     check_command git
226     check_command pacman
227 }
228
229 root_check() {
230     # root check
231     if [[ "${UID}" = 0 ]]; then
232         _msg_error "It cannot be run by the root user."
233         _msg_error "Be sure to execute it from a general user." "1"
234     fi
235 }
236
237 repo_update() {
238     cd "${repo_dir}/${repo_name}/${arch}"
239     rm -rf *.db.* *.files.* *.db *.files
240     repo-add "${repo_name}.db.tar.gz" $(ls ./*.pkg.tar.* | grep -v .sig | grep -v .sh)
241 }
242
243 sign_pkg() {
244     local pkg
245     cd "${repo_dir}/${repo_name}/${arch}"
246     rm -rf *.sig
247     for pkg in $(ls ./*.pkg.tar.* | grep -v .sig | grep -v .sh); do
248         _msg_info "Signing ${pkg}..."
249         gpg --detach-sign ${pkg}
250     done
251 }
252
253
254 build() {
255     root_check
256
257     rm -rf "${work_dir}/git_work"
258     mkdir -p "${work_dir}/lockfile/${repo_name}/${arch}"
259     mkdir -p "${work_dir}/pkgs/${repo_name}/${arch}"
260     git clone "${git_url}" "${work_dir}/git_work"
261     local init_dir=$(pwd)
262     local build_list
263
264     cd "${work_dir}/git_work/${repo_name}/${arch}"
265     local pkg
266     if [[ "${pkgs[@]}" = "ALL" ]]; then
267         build_list=($(ls 2> /dev/null))
268     else
269         build_list=(${pkgs[@]})
270     fi
271
272     for pkg in ${build_list[@]}; do
273         cd "${pkg}"
274         if [[ ! -f "${work_dir}/lockfile/${repo_name}/${arch}/${pkg}" ]]; then
275             makepkg --syncdeps --rmdeps --clean --cleanbuild --force --noconfirm --needed --skippgpcheck
276             mv *.pkg.tar.* "${work_dir}/pkgs/${repo_name}/${arch}"
277             touch "${work_dir}/lockfile/${repo_name}/${arch}/${pkg}"
278         else
279             _msg_info "${pkg} is already built."
280         fi
281         cd ..
282         rm -rf "${pkg}"
283     done
284
285     _msg_info "Copying package to repository directory..."
286     cp "${work_dir}/pkgs/${repo_name}/${arch}/"* "${repo_dir}/${repo_name}/${arch}"
287
288     sudo rm -rf "${work_dir}/git_work"
289
290
291     if ${sign}; then
292         sign_pkg
293     fi
294     repo_update
295 }
296
297
298 # Parse options
299 if [[ -z "${@}" ]]; then
300     _usage 0
301 fi
302
303 options="${@}"
304 _opt_short="h,a:,g:,r:,w:"
305 _opt_long="help,arch:,giturl:,repodir:,workdir:"
306 OPT=$(getopt -o ${_opt_short} -l ${_opt_long} -- "${@}")
307 if [[ ${?} != 0 ]]; then
308     exit 1
309 fi
310
311 eval set -- "${OPT}"
312 unset OPT
313 unset _opt_short
314 unset _opt_long
315
316 while :; do
317     case ${1} in
318         --help | -h)
319             _usage 0
320             shift 1
321             ;;
322         --arch | -a)
323             arch="${2}"
324             shift 2
325             ;;
326         --giturl | -g)
327             git_url="${2}"
328             shift 2
329             ;;
330         --repodir | -r)
331             repo_dir="${2}"
332             shift 2
333             ;;
334         --workdir | -w)
335             work_dir="${2}"
336             shift 2
337             ;;
338         --)
339             shift 1
340             break
341             ;;
342         *)
343             _msg_error "Invalid argument '${1}'"
344             _usage 1
345             ;;
346     esac
347 done
348
349 if [[ -n "${1}" ]]; then
350     repo_name="${1}"
351     repo_config="${script_path}/${repo_name}"
352     if [[ ! -f "${repo_config}" ]]; then
353         _msg_error "Repository [${repo_name}] does not exist." "1"
354     else
355         source "${repo_config}"
356     fi
357 else
358     _usage 1
359 fi
360
361 if [[ -n "${2}" ]]; then
362      case "${2}" in
363         "list") command="list" ;;
364         "build") command="build" ;;
365         *) _msg_error "Invalid command '${2}'" "1" ;;
366     esac
367 else
368     _usage 1
369 fi
370
371
372 # Run
373 prepare
374 ${command}