OSDN Git Service

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