OSDN Git Service

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