OSDN Git Service

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