OSDN Git Service

d6e80ebf6ccd9e213b0812f24608e0e2755fc780
[alterlinux/alterlinux.git] / tools / pkglist.sh
1 #!/usr/bin/env bash
2
3 set -e
4
5 script_path="$( cd -P "$( dirname "$(readlink -f "$0")" )" && cd .. && pwd )"
6 module_dir="${script_path}/modules"
7 modules=()
8
9 boot_splash=false
10 pkgdir_name="packages"
11 line=false
12 debug=false
13 memtest86=false
14 nocolor=false
15
16 additional_exclude_pkg=()
17
18 arch=""
19 channel_dir=""
20 kernel=""
21 locale_name=""
22
23
24 _help() {
25     echo "usage ${0} [options] [module 1] [module 2]..."
26     echo
27     echo "Get a list of packages to install on that channel"
28     echo
29     echo " General options:"
30     echo "    -a | --arch [arch]        Specify the architecture"
31     echo "    -b | --boot-splash        Enable boot splash"
32     echo "    -c | --channel [dir]      Specify the channel directory"
33     echo "    -d | --debug              Enable debug message"
34     echo "    -e | --exclude [pkgs]     List of packages to be additionally excluded"
35     echo "    -k | --kernel [kernel]    Specify the kernel"
36     echo "    -l | --locale [locale]    Specify the locale"
37     echo "    -m | --memtest86          Enable memtest86 package"
38     echo "    -h | --help               This help message"
39     echo "         --aur                AUR packages"
40     echo "         --line               Line break the output"
41 }
42
43 # Execute command for each module
44 # It will be executed with {} replaced with the module name.
45 # for_module <command>
46 for_module(){ local module && for module in "${modules[@]}"; do eval "${@//"{}"/${module}}"; done; }
47
48 # Message functions
49 msg_common(){
50     local _args=(-s "5" -a "pkglist.sh")
51     [[ "${nocolor}" = true ]] && _args+=("--nocolor")
52     "${script_path}/tools/msg.sh" "${_args[@]}" "${@}"
53 }
54
55 msg_error() {
56     msg_common -l "Error" -r "red" -p "stderr" error "${1}" &
57 }
58
59 msg_info() {
60     msg_common -l "Info" -r "green" -p "stderr" info "${1}" &
61 }
62
63 msg_debug() {
64     if [[ "${debug}" = true ]]; then
65         msg_common -l "Debug" -r "magenta" -p "stderr" debug "${1}" &
66     fi
67 }
68
69
70 # Parse options
71 ARGUMENT=("${@}")
72 OPTS="a:bc:de:k:l:mh"
73 OPTL="arch:,boot-splash,channel:,debug,exclude:,kernel:,locale:,memtest86,aur,help,line,nocolor"
74 if ! OPT=$(getopt -o ${OPTS} -l ${OPTL} -- "${ARGUMENT[@]}"); then
75     exit 1
76 fi
77
78 eval set -- "${OPT}"
79 unset OPT OPTS OPTL ARGUMENT
80
81 while true; do
82     case "${1}" in
83         -a | --arch)
84             arch="${2}"
85             shift 2
86             ;;
87         -b | --boot-splash)
88             boot_splash=true
89             shift 1
90             ;;
91         -c | --channel)
92             channel_dir="${2}"
93             shift 2
94             ;;
95         -d | --debug)
96             debug=true
97             shift 1
98             ;;
99         -e | --exclude)
100             IFS=" " read -r -a additional_exclude_pkg <<< "${2}"
101             shift 2
102             ;;
103         -k | --kernel)
104             kernel="${2}"
105             shift 2
106             ;;
107         -l | --locale)
108             locale_name="${2}"
109             shift 2
110             ;;
111         -m | --memtest86)
112             memtest86=true
113             shift 1
114             ;;
115         --aur)
116             pkgdir_name="packages_aur"
117             shift 1
118             ;;
119         --line)
120             line=true
121             shift 1
122             ;;
123         -h | --help)
124             _help
125             exit 0
126             ;;
127         --nocolor)
128             nocolor=true
129             shift 1
130             ;;
131         --)
132             shift 1
133             break
134             ;;
135
136     esac
137 done
138
139 for module in "${@}"; do
140     if "${script_path}/tools/module.sh" check "${module}"; then
141         modules=("${@}")
142     else
143         msg_debug "Module ${module} was not found"
144     fi
145 done
146
147 if [[ -z "${arch}" ]] || [[ "${arch}" = "" ]]; then
148     msg_error "Architecture not specified"
149     exit 1
150 elif [[ -z "${channel_dir}" ]] || [[ "${channel_dir}" = "" ]]; then
151     msg_error "Channel directory not specified"
152     exit 1
153 elif [[ -z "${kernel}" ]] || [[ "${kernel}" = "" ]]; then
154     msg_error "kernel not specified"
155     exit 1
156 elif [[ -z "${locale_name}" ]] || [[ "${locale_name}" = "" ]]; then
157     msg_error "Locale not specified"
158     exit 1
159 fi
160
161 set +e
162
163 #-- Detect package list to load --#
164 # Add the files for each channel to the list of files to read.
165 readarray -t _loadfilelist < <(ls ${channel_dir}/${pkgdir_name}.${arch}/*.${arch} 2> /dev/null)
166
167 _loadfilelist+=(
168     "${channel_dir}/${pkgdir_name}.${arch}/lang/${locale_name}.${arch}"
169     "${channel_dir}/${pkgdir_name}.${arch}/kernel/${kernel}.${arch}"
170 )
171
172 # module package list
173 for_module '_loadfilelist+=($(ls ${module_dir}/{}/${pkgdir_name}.${arch}/*.${arch} 2> /dev/null))'
174 for_module '_loadfilelist+=(${module_dir}/{}/${pkgdir_name}.${arch}/lang/${locale_name}.${arch})'
175 for_module '_loadfilelist+=(${module_dir}/{}/${pkgdir_name}.${arch}/kernel/${kernel}.${arch})'
176
177 # Plymouth package list
178 if [[ "${boot_splash}" = true ]]; then
179     readarray -t -O "${#_loadfilelist[@]}" _loadfilelist < <(ls ${channel_dir}/${pkgdir_name}.${arch}/plymouth/*.${arch} 2> /dev/null)
180     for_module '_loadfilelist+=($(ls ${module_dir}/{}/${pkgdir_name}.${arch}/plymouth/*.${arch} 2> /dev/null))'
181 fi
182
183 # memtest86 package list
184 if [[ "${memtest86}" = true ]]; then
185     readarray -t -O "${#_loadfilelist[@]}" _loadfilelist <(ls ${channel_dir}/${pkgdir_name}.${arch}/memtest86/*.${arch} 2> /dev/null)
186
187     for_module '_loadfilelist+=($(ls ${module_dir}/{}/${pkgdir_name}.${arch}/memtest86/*.${arch} 2> /dev/null))'
188 fi
189
190 #-- Read package list --#
191 # Read the file and remove comments starting with # and add it to the list of packages to install.
192 _pkglist=()
193 for _file in "${_loadfilelist[@]}"; do
194     if [[ -f "${_file}" ]]; then
195         msg_debug "Loaded package file ${_file}"
196         #_pkglist=( ${_pkglist[@]} "$(grep -h -v ^'#' ${_file})" )
197         readarray -t -O "${#_pkglist[@]}" _pkglist < <(grep -h -v ^'#' "${_file}")
198     else
199         msg_debug "The file was not found ${_file}"
200     fi
201 done
202
203 #-- Read exclude list --#
204 # Exclude packages from the share exclusion list
205 _excludefile=("${channel_dir}/packages.${arch}/exclude" "${channel_dir}/packages_aur.${arch}/exclude")
206 for_module '_excludefile+=("${module_dir}/{}/packages.${arch}/exclude" "${module_dir}/{}/packages_aur.${arch}/exclude")'
207
208 for _file in "${_excludefile[@]}"; do
209     if [[ -f "${_file}" ]]; then
210         #_excludelist+=($(grep -h -v ^'#' "${_file}") )
211         readarray -t -O "${#_excludelist[@]}" _excludelist < <(grep -h -v ^'#' "${_file}")
212     fi
213 done
214
215 #-- additional_exclude_pkg のパッケージを_excludelistに追加 --#
216 if (( "${#additional_exclude_pkg[@]}" >= 1 )); then
217     _excludelist+=("${additional_exclude_pkg[@]}")
218     msg_debug "Additional excluded packages: ${additional_exclude_pkg[*]}"
219 fi
220
221 #-- パッケージリストをソートし重複を削除 --#
222 #_pkglist=($(printf "%s\n" "${_pkglist[@]}" | sort | uniq | tr "\n" " "))
223 readarray -t _pkglist < <(printf "%s\n" "${_pkglist[@]}" | sort | uniq)
224
225 #-- excludeに記述されたパッケージを除外 --#
226 for _pkg in "${_excludelist[@]}"; do
227     #_pkglist=($(printf "%s\n" "${_pkglist[@]}" | grep -xv "${_pkg}" | tr "\n" " "))
228     readarray -t _pkglist < <(printf "%s\n" "${_pkglist[@]}" | grep -xv "${_pkg}")
229 done
230
231 #-- excludeされたパッケージを表示 --#
232 if (( "${#_excludelist[@]}" >= 1 )); then
233     msg_debug "The following packages have been removed from the installation list."
234     msg_debug "Excluded packages: ${_excludelist[*]}"
235 else
236     msg_debug "No packages are excluded."
237 fi
238
239 wait
240
241 if [[ "${line}" = true ]]; then
242     printf "%s\n" "${_pkglist[@]}"
243 else
244     echo "${_pkglist[*]}" >&1
245 fi