OSDN Git Service

[update] : Check version with channel.sh
[alterlinux/alterlinux.git] / tools / channel.sh
1 #!/usr/bin/env bash
2
3 set -e
4
5 script_path="$( cd -P "$( dirname "$(readlink -f "$0")" )" && cd .. && pwd )"
6 opt_only_add=false
7 opt_dir_name=false
8 opt_nochkver=false
9 opt_nobuiltin=false
10 opt_fullpath=false
11 opt_nocheck=false
12 alteriso_version="3.1"
13 mode=""
14 arch="all"
15 kernel="all"
16
17 _help() {
18     echo "usage ${0} [options] [command]"
19     echo
20     echo "The script that performs processing related to channels" 
21     echo
22     echo " General command:"
23     echo "    check [name]       Returns whether the specified channel name is valid."
24     echo "    desc [name]        Display a description of the specified channel"
25     echo "    show               Display a list of channels"
26     echo "    ver                Display a version declared on the channel"
27     echo "    help               This help message"
28     echo
29     echo " General options:"
30     echo "    -a | --arch [arch]        Specify the architecture"
31     echo "    -b | --nobuiltin          Exclude built-in channels"
32     echo "    -d | --dirname            Display directory names of all channel as it is"
33     echo "    -f | --fullpath           Display the full path of the channel (Use with -db)"
34     echo "    -k | --kernel [name]      Specify the supported kernel"
35     echo "    -n | --nochkver           Ignore channel version"
36     echo "    -o | --only-add           Only additional channels"
37     echo "    -v | --version [ver]      Specifies the AlterISO version"
38     echo "    -h | --help               This help message"
39     echo
40     echo "         --nocheck            Do not check the channel with desc command.This option helps speed up."
41 }
42
43 gen_channel_list() {
44     local _dirname
45     for _dirname in $(ls -l "${script_path}"/channels/ | awk '$1 ~ /d/ {print $9}'); do
46         if [[ -n $(ls "${script_path}"/channels/${_dirname}) ]] && check_alteriso_version "${_dirname}/" || [[ "${opt_nochkver}" = true ]]; then
47             if [[  ! "${arch}" = "all" ]] && [[ -z "$(cat "${script_path}/channels/${_dirname}/architecture" 2> /dev/null | grep -h -v ^'#' | grep -x "${arch}")" ]] || [[ "${_dirname}" = "share" ]] || [[ "${_dirname}" = "share-extra" ]]; then
48                 continue
49             elif [[ ! "${kernel}" = "all" ]] && [[ -f "${channel_dir}/kernel_list-${arch}" ]] && [[ -z $( ( cat "${script_path}/channels/${_dirname}/kernel_list-${arch}" | grep -h -v ^'#' | grep -x "${kernel}" ) 2> /dev/null) ]]; then
50                 continue
51             elif [[ $(echo "${_dirname}" | sed 's/^.*\.\([^\.]*\)$/\1/') = "add" ]]; then
52                 if [[ "${opt_dir_name}" = true ]]; then
53                     if [[ "${opt_fullpath}" = true ]]; then
54                         channellist+=("${script_path}/channels/${_dirname}/")
55                     else
56                         channellist+=("${_dirname}")
57                     fi
58                 else
59                     channellist+=("$(echo ${_dirname} | sed 's/\.[^\.]*$//')")
60                 fi
61             elif [[ ! -d "${script_path}/channels/${_dirname}.add" ]] && [[ "${opt_only_add}" = false ]]; then
62                 if [[ "${opt_fullpath}" = true ]]; then
63                     channellist+=("${script_path}/channels/${_dirname}/")
64                 else
65                     channellist+=("${_dirname}")
66                 fi
67             else
68                 continue
69             fi
70         fi
71     done
72     if [[ "${opt_nobuiltin}" = false ]]; then
73         channellist+=("clean")
74     fi
75 }
76
77 # check?alteriso_version <channel dir>
78 get_alteriso_version(){
79     local _channel
80     if [[ ! -d "${script_path}/channels/${1}" ]]; then
81         _channel="${script_path}/channels/${1}.add"
82     else
83         _channel="${script_path}/channels/${1}"
84     fi
85     if [[ ! -d "${_channel}" ]]; then
86         echo "${1} was not found." >&2
87         exit 1
88     fi
89     if [[ ! -f "${_channel}/alteriso" ]]; then
90         if (( $(find ./ -maxdepth 1 -mindepth 1 -name "*.x86_64" -o -name ".i686" -o -name "*.any" 2> /dev/null | wc -l) == 0 )); then
91             echo "2.0"
92         fi
93     else
94         echo "$(
95             source "${_channel}/alteriso"
96             echo "${alteriso}"
97         )"
98     fi
99 }
100
101 check_alteriso_version(){
102     #if [[ "$(get_alteriso_version "${1%.add}")" = "${alteriso_version}" ]]; then
103     if [[ "$(get_alteriso_version "${1%.add}" | cut -d "." -f 1)" = "$(echo "${alteriso_version}" | cut -d "." -f 1)" ]]; then
104         return 0
105     else
106         return 1
107     fi
108 }
109
110 check() {
111     gen_channel_list
112     if [[ ! "${#}" = "1" ]]; then
113         _help
114         exit 1
115     fi
116     if [[ $(printf '%s\n' "${channellist[@]}" | grep -qx "${1}"; echo -n ${?} ) -eq 0 ]]; then
117         echo "correct"
118     elif [[ -d "${1}" ]] && [[ -n $(ls "${1}") ]] && [[ ! "$(basename "${1%/}")" = "share" ]] && [[ ! "$(basename "${1%/}")" = "share-extra" ]]; then
119         local _channel_name="$(basename "${1%/}")"
120         if ! check_alteriso_version "${_channel}" || [[ "${opt_nochkver}" = true ]]; then
121             echo "directory"
122         else
123             echo "incorrect"
124         fi
125     else
126         echo "incorrect"
127     fi
128 }
129
130 desc() {
131     #gen_channel_list
132     if [[ ! "${#}" = "1" ]]; then
133         _help
134         exit 1
135     fi
136     if [[ "${opt_nocheck}" = false ]] && [[ ! "$(bash "${script_path}/tools/channel.sh" -a ${arch} -n -b check "${1}")" = "correct" ]]; then
137         exit 1
138     fi
139     local _channel
140     if [[ ! -d "${script_path}/channels/${1}" ]]; then
141         _channel="${1}.add"
142     else
143         _channel="${1}"
144     fi
145     if ! check_alteriso_version "${_channel}" && [[ "${opt_nochkver}" = false ]]; then
146         "${script_path}/tools/msg.sh" --noadjust -l 'ERROR:' --noappname error "Not compatible with AlterISO3"
147     elif [[ -f "${script_path}/channels/${_channel}/description.txt" ]]; then
148         echo -ne "$(cat "${script_path}/channels/${_channel}/description.txt")\n"
149     else
150         "${script_path}/tools/msg.sh" --noadjust -l 'WARN :' --noappname warn "This channel does not have a description.txt"
151     fi
152 }
153
154 show() {
155     gen_channel_list
156     if (( "${#channellist[*]}" > 0)); then
157         echo "${channellist[*]}"
158     fi
159 }
160
161
162 # Parse options
163 ARGUMENT="${@}"
164 opt_short="a:bdfk:nov:h"
165 opt_long="arch:,nobuiltin,dirname,fullpath,kernel:,only-add,nochkver,version:,help,nocheck"
166 OPT=$(getopt -o ${opt_short} -l ${opt_long} -- ${ARGUMENT})
167 [[ ${?} != 0 ]] && exit 1
168
169 eval set -- "${OPT}"
170 unset OPT opt_short opt_long
171
172 while true; do
173     case ${1} in
174         -a | --arch)
175             arch="${2}"
176             shift 2
177             ;;
178         -b | --nobuiltin)
179             opt_nobuiltin=true
180             shift 1
181             ;;
182         -d | --dirname)
183             opt_dir_name=true
184             shift 1
185             ;;
186         -f | --fullpath)
187             opt_fullpath=true
188             shift 1
189             ;;
190         -k | --kernel)
191             kernel="${2}"
192             shift 2
193             ;;
194         -n | --nochkver)
195             opt_nochkver=true
196             shift 1
197             ;;
198         -o | --only-add)
199             opt_only_add=true
200             shift 1
201             ;;
202         -v | --version)
203             alteriso_version="${2}"
204             shift 2
205             ;;
206         -h | --help)
207             _help
208             exit 0
209             ;;
210         --nocheck)
211             opt_nocheck=true
212             shift 1
213             ;;
214         --)
215             shift 1
216             break
217             ;;
218
219     esac
220 done
221
222 if [[ -z "${1}" ]]; then
223     _help
224     exit 1
225 else
226     mode="${1}"
227     shift 1
228 fi
229
230 case "${mode}" in
231     "check" ) check ${@}                  ;;
232     "show"  ) show                        ;;
233     "desc"  ) desc ${@}                   ;;
234     "ver"   ) get_alteriso_version ${@} ;;
235     "help"  ) _help; exit 0               ;;
236     *       ) _help; exit 1               ;;
237 esac