OSDN Git Service

[update] : aptpac
[alterlinux/alterlinux-pkgbuilds.git] / get_from_aur.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 #
10
11 script_name=$(basename ${0})
12
13 script_path="$(readlink -f ${0%/*})"
14 arch="x86_64"
15 repo="alter-stable"
16 debug=false
17 force=false
18 skip=false
19 nocolor=false
20
21 set -e
22
23 # usage <exit code>
24 _usage() {
25     echo "usage ${0} [options] [packages] [packages] ..."
26     echo
27     echo " General options:"
28     echo
29     echo "    -a | --arch <arch>        Specify the architecture"
30     echo "    -f | --force              Overwrite existing directory"
31     echo "    -s | --skip               Skip if PKGBUILD already exists"
32     echo "    -r | --repo <repo>        Specify the repository name"
33     echo "    -h | --help               This help messageExecuted via administrator web and Yama D Saba APIs"
34
35     if [[ -n "${1:-}" ]]; then
36         exit "${1}"
37     fi
38 }
39
40
41 # Color echo
42 # usage: echo_color -b <backcolor> -t <textcolor> -d <decoration> [Text]
43 #
44 # Text Color
45 # 30 => Black
46 # 31 => Red
47 # 32 => Green
48 # 33 => Yellow
49 # 34 => Blue
50 # 35 => Magenta
51 # 36 => Cyan
52 # 37 => White
53 #
54 # Background color
55 # 40 => Black
56 # 41 => Red
57 # 42 => Green
58 # 43 => Yellow
59 # 44 => Blue
60 # 45 => Magenta
61 # 46 => Cyan
62 # 47 => White
63 #
64 # Text decoration
65 # You can specify multiple decorations with ;.
66 # 0 => All attributs off (ノーマル)
67 # 1 => Bold on (太字)
68 # 4 => Underscore (下線)
69 # 5 => Blink on (点滅)
70 # 7 => Reverse video on (色反転)
71 # 8 => Concealed on
72
73 echo_color() {
74     local backcolor
75     local textcolor
76     local decotypes
77     local echo_opts
78     local arg
79     local OPTIND
80     local OPT
81     
82     echo_opts="-e"
83     
84     while getopts 'b:t:d:n' arg; do
85         case "${arg}" in
86             b) backcolor="${OPTARG}" ;;
87             t) textcolor="${OPTARG}" ;;
88             d) decotypes="${OPTARG}" ;;
89             n) echo_opts="-n -e"     ;;
90         esac
91     done
92     
93     shift $((OPTIND - 1))
94     
95     echo ${echo_opts} "\e[$([[ -v backcolor ]] && echo -n "${backcolor}"; [[ -v textcolor ]] && echo -n ";${textcolor}"; [[ -v decotypes ]] && echo -n ";${decotypes}")m${*}\e[m"
96 }
97
98
99 # Show an INFO message
100 # $1: message string
101 _msg_info() {
102     local echo_opts="-e"
103     local arg
104     local OPTIND
105     local OPT
106     while getopts 'n' arg; do
107         case "${arg}" in
108             n) echo_opts="${echo_opts} -n" ;;
109         esac
110     done
111     shift $((OPTIND - 1))
112     if [[ "${nocolor}" = true ]]; then
113         echo ${echo_opts} "[${script_name}]    Info ${*}"
114     else
115         echo ${echo_opts} "$( echo_color -t '36' "[${script_name}]")    $( echo_color -t '32' 'Info') ${*}"
116     fi
117 }
118
119
120 # Show an Warning message
121 # $1: message string
122 _msg_warn() {
123     local echo_opts="-e"
124     local arg
125     local OPTIND
126     local OPT
127     while getopts 'n' arg; do
128         case "${arg}" in
129             n) echo_opts="${echo_opts} -n" ;;
130         esac
131     done
132     shift $((OPTIND - 1))
133     if [[ "${nocolor}" = true ]]; then
134         echo ${echo_opts} "[${script_name}] Warning ${*}"
135     else
136         echo ${echo_opts} "$( echo_color -t '36' "[${script_name}]") $( echo_color -t '33' 'Warning') ${*}" >&2
137     fi
138 }
139
140
141 # Show an debug message
142 # $1: message string
143 _msg_debug() {
144     local echo_opts="-e"
145     local arg
146     local OPTIND
147     local OPT
148     while getopts 'n' arg; do
149         case "${arg}" in
150             n) echo_opts="${echo_opts} -n" ;;
151         esac
152     done
153     shift $((OPTIND - 1))
154     if [[ "${debug}" = true ]]; then
155         if [[ "${nocolor}" = true ]]; then
156             echo ${echo_opts} "[${script_name}]   Debug ${*}"
157         else
158             echo ${echo_opts} "$( echo_color -t '36' "[${script_name}]")   $( echo_color -t '35' 'Debug') ${*}"
159         fi
160     fi
161 }
162
163
164 # Show an ERROR message then exit with status
165 # $1: message string
166 # $2: exit code number (with 0 does not exit)
167 _msg_error() {
168     local echo_opts="-e"
169     local arg
170     local OPTIND
171     local OPT
172     local OPTARG
173     while getopts 'n' arg; do
174         case "${arg}" in
175             n) echo_opts="${echo_opts} -n" ;;
176         esac
177     done
178     shift $((OPTIND - 1))
179     if [[ "${nocolor}" = true ]]; then
180         echo ${echo_opts} "[${script_name}]   Error ${1}"
181     else
182         echo ${echo_opts} "$( echo_color -t '36' "[${script_name}]")   $( echo_color -t '31' 'Error') ${1}" >&2
183     fi
184     if [[ -n "${2:-}" ]]; then
185         exit ${2}
186     fi
187 }
188
189 # rm helper
190 # Delete the file if it exists.
191 # For directories, rm -rf is used.
192 # If the file does not exist, skip it.
193 # remove <file> <file> ...
194 remove() {
195     local _list
196     local _file
197     _list=($(echo "$@"))
198     for _file in "${_list[@]}"; do
199         if [[ -f ${_file} ]]; then
200             _msg_debug "Removeing ${_file}"
201             rm -f "${_file}"
202             elif [[ -d ${_file} ]]; then
203             _msg_debug "Removeing ${_file}"
204             rm -rf "${_file}"
205         fi
206     done
207 }
208
209 # Parse options
210 if [[ -z "${@}" ]]; then
211     _usage 0
212 fi
213
214 _opt_short="h,r:,a:sf"
215 _opt_long="help,repo:,arch:,skip,force,nocolor"
216
217 OPT=$(getopt -o ${_opt_short} -l ${_opt_long} -- "${@}")
218 if [[ ${?} != 0 ]]; then
219     exit 1
220 fi
221
222 eval set -- "${OPT}"
223 unset OPT
224 unset _opt_short
225 unset _opt_long
226
227 while :; do
228     case ${1} in
229         --help | -h)
230             _usage 0
231             shift 1
232             ;;
233         --repo | -r)
234             repo="${2}"
235             shift 2
236             ;;
237         --arch | -a)
238             arch="${2}"
239             shift 2
240             ;;
241         --force | -f)
242             force=true
243             shift 1
244             ;;
245         --skip | -s)
246             skip=true
247             shift 1
248             ;;
249         --nocolor)
250             nocolor=true
251             shift 1
252             ;;
253         --) 
254             shift 1
255             break
256             ;;
257         *)
258             _msg_error "Invalid argument '${1}'"
259             _usage 1
260             ;;
261     esac
262 done
263
264 echo ${@}
265
266 for pkg in ${@}; do
267     if [[ "${force}" = true ]]; then
268         rm -rf "${script_path}/${repo}/${arch}/${pkg}"
269     elif [[ -d "${script_path}/${repo}/${arch}/${pkg}" ]]; then
270         _msg_error "${pkg} has already been added."
271         if [[ "${skip}" = true ]]; then
272             continue
273         else
274             exit 1
275         fi
276     fi
277     mkdir -p "${script_path}/${repo}/${arch}/${pkg}"
278     git clone "https://aur.archlinux.org/${pkg}.git" "${script_path}/${repo}/${arch}/${pkg}"
279     rm -rf "${script_path}/${repo}/${arch}/${pkg}/.git"
280 done