OSDN Git Service

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