OSDN Git Service

[update] : Updated help display
[alterlinux/LFBS.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 share_dir="${script_path}/channels/share"
7
8 boot_splash=false
9 pkgdir_name="packages"
10
11 arch=""
12 channel_dir=""
13 kernel=""
14 locale_name=""
15
16 #arch="x86_64"
17 #channel_dir="${script_path}/channels/xfce"
18 #kernel="zen"
19 #locale_name="en"
20
21 _help() {
22     echo "usage ${0} [options] [command]"
23     echo
24     echo "Get a list of packages to install on that channel"
25     echo
26     echo " General options:"
27     echo "    -a | --arch [arch]        Specify the architecture"
28     echo "    -b | --boot-splash        Enable boot splash"
29     echo "    -c | --channel            Specify the channel directory"
30     echo "    -k | --kernel             Specify the kernel"
31     echo "    -h | --help               This help message"
32 }
33
34 # Usage: getclm <number>
35 # 標準入力から値を受けとり、引数で指定された列を抽出します。
36 getclm() {
37     echo "$(cat -)" | cut -d " " -f "${1}"
38 }
39
40 # Message functions
41 msg_error() {
42     "${script_path}/tools/msg.sh" -s "5" -a "pkglist.sh" -l "Error" -r "red" error "${1}"
43 }
44
45 msg_info() {
46     "${script_path}/tools/msg.sh" -s "5" -a "pkglist.sh" -l "Info" -r "green" error "${1}"
47 }
48
49 msg_debug() {
50     "${script_path}/tools/msg.sh" -s "5" -a "pkglist.sh" -l "Debug" -r "magenta" error "${1}"
51 }
52
53
54 # Parse options
55 ARGUMENT="${@}"
56 _opt_short="a:bc:k:l:h"
57 _opt_long="arch:,boot-splash,channel:,kernel:,locale:,help"
58 OPT=$(getopt -o ${_opt_short} -l ${_opt_long} -- ${ARGUMENT})
59 [[ ${?} != 0 ]] && exit 1
60
61 eval set -- "${OPT}"
62 unset OPT _opt_short _opt_long
63
64 while true; do
65     case ${1} in
66         -a | --arch)
67             arch="${2}"
68             shift 2
69             ;;
70         -b | --boot-splash)
71             boot_splash=true
72             shift 1
73             ;;
74         -c | --channel)
75             channel_dir="${2}"
76             shift 2
77             ;;
78         -k | --kernel)
79             kernel="${2}"
80             shift 2
81             ;;
82         -l | --locale)
83             locale_name="${2}"
84             shift 2
85             ;;
86         -h | --help)
87             _help
88             exit 0
89             ;;
90         --)
91             shift 1
92             break
93             ;;
94
95     esac
96 done
97
98
99 if [[ -z "${arch}" ]]; then
100     msg_error "Architecture not specified"
101     exit 1
102 elif [[ -z "${channel_dir}" ]]; then
103     msg_error "Channel directory not specified"
104     exit 1
105 #elif [[ -z "${kernel}" ]]; then
106     #msg_error "kernel not specified"
107     #exit 1
108 elif [[ -z "${locale_name}" ]]; then
109     msg_error "Locale not specified"
110     exit 1
111 fi
112
113
114 pkgdir_name="packages"
115
116 set +e
117
118
119 #-- Detect package list to load --#
120 # Add the files for each channel to the list of files to read.
121 _loadfilelist=(
122     # share packages
123     $(ls ${share_dir}/${pkgdir_name}.${arch}/*.${arch} 2> /dev/null)
124     "${share_dir}/${pkgdir_name}.${arch}/lang/${locale_name}.${arch}"
125
126     # channel packages
127     $(ls ${channel_dir}/${pkgdir_name}.${arch}/*.${arch} 2> /dev/null)
128     "${channel_dir}/${pkgdir_name}.${arch}/lang/${locale_name}.${arch}"
129
130     # kernel packages
131     "${share_dir}/${pkgdir_name}.${arch}/kernel/${kernel}.${arch}"
132     "${channel_dir}/${pkgdir_name}.${arch}/kernel/${kernel}.${arch}"
133 )
134
135 # Plymouth package list
136 if [[ "${boot_splash}" = true ]]; then
137     _loadfilelist+=(
138         $(ls ${share_dir}/${pkgdir_name}.${arch}/plymouth/*.${arch} 2> /dev/null)
139         $(ls ${channel_dir}/${pkgdir_name}.${arch}/plymouth/*.${arch} 2> /dev/null)
140     )
141 fi
142
143
144 #-- Read package list --#
145 # Read the file and remove comments starting with # and add it to the list of packages to install.
146 for _file in ${_loadfilelist[@]}; do
147     if [[ -f "${_file}" ]]; then
148         msg_debug "Loaded package file ${_file}"
149         _pkglist=( ${_pkglist[@]} "$(grep -h -v ^'#' ${_file})" )
150     fi
151 done
152
153 #-- Read exclude list --#
154 # Exclude packages from the share exclusion list
155 _excludefile=(
156     "${share_dir}/${pkgdir_name}.${arch}/exclude"
157     "${channel_dir}/${pkgdir_name}.${arch}/exclude"
158 )
159
160 for _file in ${_excludefile[@]}; do
161     if [[ -f "${_file}" ]]; then
162         _excludelist=( ${_excludelist[@]} $(grep -h -v ^'#' "${_file}") )
163     fi
164 done
165
166 #-- excludeに記述されたパッケージを除外 --#
167 # _pkglistを_subpkglistにコピーしexcludeのパッケージを除外し再代入
168 _subpkglist=(${_pkglist[@]})
169 unset _pkglist
170 for _pkg in ${_subpkglist[@]}; do
171     # もし変数_pkgの値が配列_excludelistに含まれていなかったらpkglistに追加する
172     if [[ ! $(printf '%s\n' "${_excludelist[@]}" | grep -qx "${_pkg}"; echo -n ${?} ) = 0 ]]; then
173         _pkglist=(${_pkglist[@]} "${_pkg}")
174     fi
175 done
176 unset _subpkglist
177
178 #-- excludeされたパッケージを表示 --#
179 if [[ -n "${_excludelist[*]}" ]]; then
180     msg_debug "The following packages have been removed from the installation list."
181     msg_debug "Excluded packages: ${_excludelist[*]}"
182 fi
183
184 # Sort the list of packages in abc order.
185 _pkglist=($(for _pkg in ${_pkglist[@]}; do echo "${_pkg}"; done | sort | perl -pe 's/\n/ /g'))
186
187 echo "${_pkglist[@]}" >&1