OSDN Git Service

[add] : i686 packages
[alterlinux/alterlinux.git] / keyring.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 # keyring.sh
10 #
11 # Script to import AlterLinux and ArchLinux keys.
12 #
13
14
15 set -e
16
17 script_path="$(readlink -f ${0%/*})"
18 arch=$(uname -m)
19
20
21 # Set pacman.conf when build alterlinux
22 alter_pacman_conf_x86_64="${script_path}/system/pacman-x86_64.conf"
23 alter_pacman_conf_i686="${script_path}/system/pacman-i686.conf"
24
25
26 # Color echo
27 # usage: echo_color -b <backcolor> -t <textcolor> -d <decoration> [Text]
28 #
29 # Text Color
30 # 30 => Black
31 # 31 => Red
32 # 32 => Green
33 # 33 => Yellow
34 # 34 => Blue
35 # 35 => Magenta
36 # 36 => Cyan
37 # 37 => White
38 #
39 # Background color
40 # 40 => Black
41 # 41 => Red
42 # 42 => Green
43 # 43 => Yellow
44 # 44 => Blue
45 # 45 => Magenta
46 # 46 => Cyan
47 # 47 => White
48 #
49 # Text decoration
50 # You can specify multiple decorations with ;.
51 # 0 => All attributs off (ノーマル)
52 # 1 => Bold on (太字)
53 # 4 => Underscore (下線)
54 # 5 => Blink on (点滅)
55 # 7 => Reverse video on (色反転)
56 # 8 => Concealed on
57
58 echo_color() {
59     local backcolor
60     local textcolor
61     local decotypes
62     local echo_opts
63     local OPTIND_bak="${OPTIND}"
64     unset OPTIND
65
66     echo_opts="-e"
67
68     while getopts 'b:t:d:n' arg; do
69         case "${arg}" in
70             b) backcolor="${OPTARG}" ;;
71             t) textcolor="${OPTARG}" ;;
72             d) decotypes="${OPTARG}" ;;
73             n) echo_opts="-n -e"     ;;
74         esac
75     done
76
77     shift $((OPTIND - 1))
78
79     echo ${echo_opts} "\e[$([[ -v backcolor ]] && echo -n "${backcolor}"; [[ -v textcolor ]] && echo -n ";${textcolor}"; [[ -v decotypes ]] && echo -n ";${decotypes}")m${*}\e[m"
80     OPTIND=${OPTIND_bak}
81 }
82
83
84 # Show an INFO message
85 # $1: message string
86 msg_info() {
87     local _msg="${1}"
88     echo "$( echo_color -t '36' '[keyring.sh]')    $( echo_color -t '32' 'Info') ${_msg}"
89 }
90
91
92 # Show an Warning message
93 # $1: message string
94 msg_warn() {
95     local _msg="${1}"
96     echo "$( echo_color -t '36' '[keyring.sh]') $( echo_color -t '33' 'Warning') ${_msg}" >&2
97 }
98
99
100 # Show an debug message
101 # $1: message string
102 msg_debug() {
103     local _msg="${1}"
104     if [[ ${debug} = true ]]; then
105         echo "$( echo_color -t '36' '[keyring.sh]')   $( echo_color -t '35' 'Debug') ${_msg}"
106     fi
107 }
108
109
110 # Show an ERROR message then exit with status
111 # $1: message string
112 # $2: exit code number (with 0 does not exit)
113 msg_error() {
114     local _msg="${1}"
115     echo "$( echo_color -t '36' '[keyring.sh]')   $( echo_color -t '31' 'Error') ${_msg}" >&2
116 }
117
118
119 # Show usage
120 _usage () {
121     echo "usage ${0} [options]"
122     echo
123     echo " General options:"
124     echo "    -a | --alter-add       Add alterlinux-keyring."
125     echo "    -r | --alter-remove    Remove alterlinux-keyring."
126     echo "    -c | --arch-add        Add archlinux-keyring."
127     echo "    -h | --help            Show this help and exit."
128     echo "    -l | --arch32-add      Add archlinux32-keyring."
129     echo "    -i | --arch32-remove   Remove archlinux32-keyring."
130     exit "${1}"
131 }
132
133
134 # Check if the package is installed.
135 checkpkg() {
136     local _pkg
137     _pkg=$(echo "${1}" | cut -d'/' -f2)
138
139     if [[ ${#} -gt 2 ]]; then
140         msg_error "Multiple package specification is not available."
141     fi
142
143     if [[ -n $( pacman -Q "${_pkg}" 2> /dev/null| awk '{print $1}' ) ]]; then
144         echo -n "true"
145     else
146         echo -n "false"
147     fi
148 }
149
150
151 run() {
152     msg_info "Running ${*}"
153     ${@}
154 }
155
156
157 prepare() {
158     if [[ ! ${UID} = 0 ]]; then
159         msg_error "You dont have root permission."
160         msg_error 'Please run as root.'
161         exit 1
162     fi
163
164     if [[ ! -f "${alter_pacman_conf_x86_64}" ]]; then
165         msg_error "${alter_pacman_conf_x86_64} does not exist."
166         exit 1
167     fi
168
169     if [[ ! -f "${alter_pacman_conf_i686}" ]]; then
170         msg_error "${alter_pacman_conf_i686} does not exist."
171         exit 1
172     fi
173
174     #pacman -Sy
175 }
176
177
178 update_arch_key() {
179     pacman-key --refresh-keys
180     pacman-key --init
181     pacman-key --populate archlinux
182     pacman -S --noconfirm  core/archlinux-keyring
183     pacman-key --init
184     pacman-key --populate archlinux
185     pacman -Sy
186 }
187
188
189 update_alter_key() {
190     curl -L -o "/tmp/fascode.pub" "https://山d.com/repo/fascode.pub"
191     pacman-key -a "/tmp/fascode.pub"
192     rm -f "/tmp/fascode.pub"
193     pacman-key --lsign-key development@fascode.net
194
195     pacman --config "${alter_pacman_conf_x86_64}" -Sy --noconfirm
196     pacman --config "${alter_pacman_conf_x86_64}" -S --noconfirm alter-stable/alterlinux-keyring
197
198     pacman-key --init
199     pacman-key --populate alterlinux
200     pacman -Sy
201 }
202
203
204 remove_alter_key() {
205     pacman-key -d BDC396346243AB57ACD090F9F50544048389DA36
206     if checkpkg alterlinux-keyring; then
207         pacman -Rsnc --noconfirm alterlinux-keyring
208     fi
209     pacman -Sy
210 }
211
212 update_arch32_key() {
213     pacman -Sy --config "${alter_pacman_conf_i686}"
214     pacman --noconfirm -S --config "${alter_pacman_conf_i686}" alter-stable/archlinux32-keyring
215     pacman-key --init
216     pacman-key --populate archlinux32
217     #pacman-key --refresh-keys
218 }
219
220 remove_arch32_key() {
221     pacman -Rsnc archlinux32-keyring
222 }
223
224
225 # 引数解析
226 while getopts 'archli-:' arg; do
227     case "${arg}" in
228         # alter-add
229         a)
230             run prepare
231             run update_alter_key
232             ;;
233         # alter-remove
234         r)
235             run prepare
236             run remove_alter_key
237             ;;
238         # arch-add
239         c)
240             run prepare
241             run update_arch_key
242             ;;
243         # help
244         h) 
245             _usage 0
246             ;;
247         # arch32-add
248         l)
249             run prepare
250             run update_arch32_key
251             ;;
252         # arch32-remove
253         i)
254             run prepare
255             run remove_arch32_key
256             ;;
257         -)
258             case "${OPTARG}" in
259                 alter-add)
260                     run prepare
261                     run update_alter_key
262                     ;;
263                 alter-remove)
264                     run prepare
265                     run remove_alter_key
266                     ;;
267                 arch-add)
268                     run prepare
269                     run update_arch_key
270                     ;;
271                 help)
272                     _usage 0
273                     ;;
274                 arch32-add)
275                     run prepare
276                     run update_arch32_key
277                     ;;
278                 arch32-remove)
279                     run prepare
280                     run remove_arch32_key
281                     ;;
282                 *)
283                     _usage 1
284                     ;;
285             esac
286             ;;
287         *) _usage; exit 1;;
288     esac
289 done
290
291
292 # 引数が何もなければ全てを実行する
293 if [[ ${#} = 0 ]]; then
294     run prepare
295     # run update_arch_key
296     run update_alter_key
297     # run update_arch32_key
298 fi