OSDN Git Service

[fix] : Fixed default entry name
[alterlinux/alterlinux.git] / tools / keyring.sh
1 #!/usr/bin/env bash
2 #
3 # Yamada Hayao
4 # Twitter: @Hayao0819
5 # Email  : hayao@fascode.net
6 #
7 # (c) 2019-2021 Fascode Network.
8 #
9 # keyring.sh
10 #
11 # Script to import Alter Linux and ArchLinux keys.
12 #
13
14
15 set -e
16
17 script_path="$( cd -P "$( dirname "$(readlink -f "$0")" )" && cd .. && pwd )"
18 arch="$(uname -m)"
19 archlinux32_repo="http://mirror.juniorjpdj.pl/archlinux32/i486/core/"
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 # Message common function
26 # msg_common [type] [-n] [string]
27 msg_common(){
28     local _msg_opts=("-a" "keyring.sh") _type="${1}"
29     shift 1
30     [[ "${1}" = "-n" ]] && _msg_opts+=("-o" "-n") && shift 1
31     _msg_opts+=("${_type}" "${@}")
32     "${script_path}/tools/msg.sh" "${_msg_opts[@]}"
33 }
34
35 # Show an INFO message
36 # ${1}: message string
37 msg_info() { msg_common info "${@}"; }
38
39 # Show an Warning message
40 # ${1}: message string
41 msg_warn() { msg_common warn "${@}"; }
42
43 # Show an ERROR message then exit with status
44 # ${1}: message string
45 # ${2}: exit code number (with 0 does not exit)
46 msg_error() {
47     msg_common error "${1}"
48     [[ -n "${2:-}" ]] && exit "${2}"
49     return 0
50 }
51
52 # Usage: getclm <number>
53 # 標準入力から値を受けとり、引数で指定された列を抽出します。
54 getclm() { cut -d " " -f "${1}"; }
55
56
57 # Show usage
58 _usage () {
59     echo "usage ${0} [options]"
60     echo
61     echo " General options:"
62     echo "    -a | --alter-add       Add alterlinux-keyring."
63     echo "    -r | --alter-remove    Remove alterlinux-keyring."
64     echo "    -c | --arch-add        Add archlinux-keyring."
65     echo "    -h | --help            Show this help and exit."
66     echo "    -l | --arch32-add      Add archlinux32-keyring."
67     echo "    -i | --arch32-remove   Remove archlinux32-keyring."
68     exit "${1}"
69 }
70
71
72 # Check if the package is installed.
73 checkpkg() {
74     local _pkg
75     _pkg=$(echo "${1}" | cut -d'/' -f2)
76
77     if [[ ${#} -gt 2 ]]; then
78         msg_error "Multiple package specification is not available."
79     fi
80
81     if [[ -n $( pacman -Q "${_pkg}" 2> /dev/null| getclm 1 ) ]]; then
82         echo -n "true"
83     else
84         echo -n "false"
85     fi
86 }
87
88
89 run() {
90     msg_info "Running ${*}"
91     eval "${@}"
92 }
93
94
95 prepare() {
96     if [[ ! ${UID} = 0 ]]; then
97         msg_error "You dont have root permission."
98         msg_error 'Please run as root.'
99         exit 1
100     fi
101
102     if [[ ! -f "${alter_pacman_conf_x86_64}" ]]; then
103         msg_error "${alter_pacman_conf_x86_64} does not exist."
104         exit 1
105     fi
106
107     if [[ ! -f "${alter_pacman_conf_i686}" ]]; then
108         msg_error "${alter_pacman_conf_i686} does not exist."
109         exit 1
110     fi
111
112     pacman -Sc --noconfirm > /dev/null 2>&1
113     pacman -Syy
114 }
115
116
117 update_arch_key() {
118     pacman-key --refresh-keys
119     pacman-key --init
120     pacman-key --populate archlinux
121     pacman -Sy --noconfirm core/archlinux-keyring
122     pacman-key --init
123     pacman-key --populate archlinux
124 }
125
126
127 update_alter_key() {
128     curl -Lo - "http://repo.dyama.net/fascode.pub" \
129         | pacman-key -a -
130     pacman-key --lsign-key development@fascode.net
131
132     pacman --config "${alter_pacman_conf_x86_64}" -Sy --noconfirm alter-stable/alterlinux-keyring
133
134     pacman-key --init
135     pacman-key --populate alterlinux
136 }
137
138
139 remove_alter_key() {
140     pacman-key -d BDC396346243AB57ACD090F9F50544048389DA36
141     if checkpkg alterlinux-keyring; then
142         pacman -Rsnc --noconfirm alterlinux-keyring
143     fi
144 }
145
146 update_arch32_key() {
147     ! pacman -Ssq archlinux32-keyring | grep -x archlinux32-keyring 2> /dev/null 1>&2 && msg_error "Not found archlinux32-keyring on remote repository. You should install it manually." 1
148     pacman --noconfirm -S archlinux32-keyring
149     pacman-key --init
150     pacman-key --populate archlinux32
151     #pacman-key --refresh-keys
152 }
153
154 new_update_arch32_key(){
155     local _savedir="${HOME}/.cache"
156     while read -r _pkg ; do
157         curl -o "${_savedir}/${_pkg}" "${archlinux32_repo}/${_pkg}"
158         pacman -U --noconfirm "${_savedir}/${_pkg}"
159         rm -f "${_savedir}/${_pkg}"
160     done < <(curl -sL "${archlinux32_repo}" | sed "s|<a href=\"||g" | cut -d "\"" -f 1 | grep -v "^<" | grep -v ".sig$" | grep ".pkg.tar." | grep "archlinux32-keyring" | grep -v "archlinux32-keyring-transition")
161     pacman-key --init
162     pacman-key --populate archlinux32
163 }
164
165 remove_arch32_key() {
166     pacman -Rsnc archlinux32-keyring
167 }
168
169
170 # 引数解析
171 while getopts 'archli-:' arg; do
172     case "${arg}" in
173         # alter-add
174         a)
175             run prepare
176             run update_alter_key
177             ;;
178         # alter-remove
179         r)
180             run prepare
181             run remove_alter_key
182             ;;
183         # arch-add
184         c)
185             run prepare
186             run update_arch_key
187             ;;
188         # help
189         h)
190             _usage 0
191             ;;
192         # arch32-add
193         l)
194             run prepare
195             run update_arch32_key
196             ;;
197         # arch32-remove
198         i)
199             run prepare
200             run remove_arch32_key
201             ;;
202         -)
203             case "${OPTARG}" in
204                 alter-add)
205                     run prepare
206                     run update_alter_key
207                     ;;
208                 alter-remove)
209                     run prepare
210                     run remove_alter_key
211                     ;;
212                 arch-add)
213                     run prepare
214                     run update_arch_key
215                     ;;
216                 help)
217                     _usage 0
218                     ;;
219                 arch32-add)
220                     run prepare
221                     run update_arch32_key
222                     ;;
223                 arch32-remove)
224                     run prepare
225                     run remove_arch32_key
226                     ;;
227                 *)
228                     _usage 1
229                     ;;
230             esac
231             ;;
232         *) _usage; exit 1;;
233     esac
234 done
235
236
237 # 引数が何もなければ全てを実行する
238 if [[ ${#} = 0 ]]; then
239     run prepare
240     # run update_arch_key
241     run update_alter_key
242     run update_arch32_key
243 fi