OSDN Git Service

[update] : Added comments for depend packages
[alterlinux/alterlinux.git] / tools / channel.sh
index b236c4a..72bd041 100755 (executable)
@@ -7,11 +7,13 @@ opt_only_add=false
 opt_dir_name=false
 opt_nochkver=false
 opt_nobuiltin=false
-opt_allarch=false
 opt_fullpath=false
-alteriso_version="3.0"
+opt_nocheck=false
+opt_line=false
+alteriso_version="3.1"
 mode=""
 arch="all"
+kernel="all"
 
 _help() {
     echo "usage ${0} [options] [command]"
@@ -20,7 +22,9 @@ _help() {
     echo
     echo " General command:"
     echo "    check [name]       Returns whether the specified channel name is valid."
+    echo "    desc [name]        Display a description of the specified channel"
     echo "    show               Display a list of channels"
+    echo "    ver                Display a version declared on the channel"
     echo "    help               This help message"
     echo
     echo " General options:"
@@ -28,21 +32,29 @@ _help() {
     echo "    -b | --nobuiltin          Exclude built-in channels"
     echo "    -d | --dirname            Display directory names of all channel as it is"
     echo "    -f | --fullpath           Display the full path of the channel (Use with -db)"
-    echo "    -m | --multi              Only channels supported by allarch.sh"
+    echo "    -k | --kernel [name]      Specify the supported kernel"
     echo "    -n | --nochkver           Ignore channel version"
     echo "    -o | --only-add           Only additional channels"
     echo "    -v | --version [ver]      Specifies the AlterISO version"
     echo "    -h | --help               This help message"
+    echo
+    echo "         --nocheck            Do not check the channel with desc command.This option helps speed up."
+    echo "         --line               Line break the output"
+    echo
+    echo " check command exit code"
+    echo "    0 (correct)               Normal available channel"
+    echo "    1 (directory)             Channel outside the channel directory"
+    echo "    2 (incorrect)             Unavailable channel"
+    echo "    3                         Other error"
 }
 
 gen_channel_list() {
     local _dirname
     for _dirname in $(ls -l "${script_path}"/channels/ | awk '$1 ~ /d/ {print $9}'); do
-        if [[ -n $(ls "${script_path}"/channels/${_dirname}) ]] && [[ "$(cat "${script_path}/channels/${_dirname}/alteriso" 2> /dev/null)" = "alteriso=${alteriso_version}" ]] || [[ "${opt_nochkver}" = true ]]; then
+        if [[ -n $(ls "${script_path}"/channels/${_dirname}) ]] && check_alteriso_version "${_dirname}/" || [[ "${opt_nochkver}" = true ]]; then
             if [[  ! "${arch}" = "all" ]] && [[ -z "$(cat "${script_path}/channels/${_dirname}/architecture" 2> /dev/null | grep -h -v ^'#' | grep -x "${arch}")" ]]; then
                 continue
-            fi
-            if [[ "${_dirname}" = "share" ]]; then
+            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
                 continue
             elif [[ $(echo "${_dirname}" | sed 's/^.*\.\([^\.]*\)$/\1/') = "add" ]]; then
                 if [[ "${opt_dir_name}" = true ]]; then
@@ -52,7 +64,8 @@ gen_channel_list() {
                         channellist+=("${_dirname}")
                     fi
                 else
-                    channellist+=("$(echo ${_dirname} | sed 's/\.[^\.]*$//')")
+                    #channellist+=("$(echo ${_dirname} | sed 's/\.[^\.]*$//')")
+                    readarray -t -O "${#channellist[@]}" channellist < <(echo "${_dirname}" | sed 's/\.[^\.]*$//')
                 fi
             elif [[ ! -d "${script_path}/channels/${_dirname}.add" ]] && [[ "${opt_only_add}" = false ]]; then
                 if [[ "${opt_fullpath}" = true ]]; then
@@ -66,51 +79,116 @@ gen_channel_list() {
         fi
     done
     if [[ "${opt_nobuiltin}" = false ]]; then
-        if [[ "${opt_allarch}" = false ]]; then
-            channellist+=("rebuild")
-        fi
         channellist+=("clean")
     fi
 }
 
+# check?alteriso_version <channel dir>
+get_alteriso_version(){
+    local _channel
+    if [[ ! -d "${script_path}/channels/${1}" ]]; then
+        _channel="${script_path}/channels/${1}.add"
+    else
+        _channel="${script_path}/channels/${1}"
+    fi
+    if [[ ! -d "${_channel}" ]]; then
+        echo "${1} was not found." >&2
+        exit 1
+    fi
+    if [[ ! -f "${_channel}/alteriso" ]]; then
+        if (( $(find ./ -maxdepth 1 -mindepth 1 -name "*.x86_64" -o -name ".i686" -o -name "*.any" 2> /dev/null | wc -l) == 0 )); then
+            echo "2.0"
+        fi
+    else
+        echo "$(
+            source "${_channel}/alteriso"
+            echo "${alteriso}"
+        )"
+    fi
+}
+
+check_alteriso_version(){
+    #if [[ "$(get_alteriso_version "${1%.add}")" = "${alteriso_version}" ]]; then
+    if [[ "$(get_alteriso_version "${1%.add}" | cut -d "." -f 1)" = "$(echo "${alteriso_version}" | cut -d "." -f 1)" ]]; then
+        return 0
+    else
+        return 1
+    fi
+}
+
 check() {
+    local _channel_name
+
     gen_channel_list
     if [[ ! "${#}" = "1" ]]; then
         _help
-        exit 1
+        exit 3
     fi
     if [[ $(printf '%s\n' "${channellist[@]}" | grep -qx "${1}"; echo -n ${?} ) -eq 0 ]]; then
-        echo "correct"
-    elif [[ -d "${1}" ]] && [[ -n $(ls "${1}") ]] && [[ "$(cat "${1}/alteriso" 2> /dev/null)" = "alteriso=${alteriso_version}" ]] || [[ "${opt_nochkver}" = true ]]; then
-            local _channel_name="$(basename "${1%/}")"
-            if [[ ! "${_channel_name}" = "share" ]] && [[ $(echo "${_channel_name}" | sed 's/^.*\.\([^\.]*\)$/\1/') = "add" ]] || [[ ! -d "${script_path}/channels/${_dirname}.add" ]] && [[ "${opt_only_add}" = false ]]; then
-                echo "directory"
-            fi
+        #echo "correct"
+        exit 0
+    elif [[ -d "${1}" ]] && [[ -n $(ls "${1}") ]]; then
+        _channel_name="$(basename "${1%/}")"
+        if check_alteriso_version "${_channel_name}" || [[ "${opt_nochkver}" = true ]]; then
+            #echo "directory"
+            exit 1
+        else
+            #echo "incorrect"
+            exit 2
+        fi
     else
-        echo "incorrect"
+        #echo "incorrect"
+        exit 2
+    fi
+}
+
+desc() {
+    #gen_channel_list
+    if [[ ! "${#}" = "1" ]]; then
+        _help
+        exit 1
+    fi
+    if [[ "${opt_nocheck}" = false ]] && ! bash "${script_path}/tools/channel.sh" -a ${arch} -n -b check "${1}"; then
+        exit 1
+    fi
+    local _channel
+    if [[ ! -d "${script_path}/channels/${1}" ]]; then
+        _channel="${1}.add"
+    else
+        _channel="${1}"
+    fi
+    if ! check_alteriso_version "${_channel}" && [[ "${opt_nochkver}" = false ]]; then
+        "${script_path}/tools/msg.sh" --noadjust -l 'ERROR:' --noappname error "Not compatible with AlterISO3"
+    elif [[ -f "${script_path}/channels/${_channel}/description.txt" ]]; then
+        echo -ne "$(cat "${script_path}/channels/${_channel}/description.txt")\n"
+    else
+        "${script_path}/tools/msg.sh" --noadjust -l 'WARN :' --noappname warn "This channel does not have a description.txt"
     fi
 }
 
 show() {
     gen_channel_list
     if (( "${#channellist[*]}" > 0)); then
-        echo "${channellist[*]}"
+        if [[ "${opt_line}" = true ]]; then
+            printf "%s\n" "${channellist[@]}"
+        else
+            echo "${channellist[*]}"
+        fi
     fi
 }
 
 
 # Parse options
-ARGUMENT="${@}"
-_opt_short="a:bdfmnov:h"
-_opt_long="arch:,nobuiltin,dirname,fullpath,multi,only-add,nochkver,version:,help"
-OPT=$(getopt -o ${_opt_short} -l ${_opt_long} -- ${ARGUMENT})
-[[ ${?} != 0 ]] && exit 1
-
+OPTS="a:bdfk:nov:h"
+OPTL="arch:,nobuiltin,dirname,fullpath,kernel:,only-add,nochkver,version:,help,nocheck,line"
+if ! OPT=$(getopt -o ${OPTS} -l ${OPTL} -- "${@}"); then
+    exit 1
+fi
 eval set -- "${OPT}"
-unset OPT _opt_short _opt_long
+unset OPT OPTS OPTL
 
 while true; do
-    case ${1} in
+    case "${1}" in
         -a | --arch)
             arch="${2}"
             shift 2
@@ -127,9 +205,9 @@ while true; do
             opt_fullpath=true
             shift 1
             ;;
-        -m | --multi)
-            opt_allarch=true
-            shift 1
+        -k | --kernel)
+            kernel="${2}"
+            shift 2
             ;;
         -n | --nochkver)
             opt_nochkver=true
@@ -147,6 +225,14 @@ while true; do
             _help
             exit 0
             ;;
+        --nocheck)
+            opt_nocheck=true
+            shift 1
+            ;;
+        --line)
+            opt_line=true
+            shift 1
+            ;;
         --)
             shift 1
             break
@@ -164,8 +250,10 @@ else
 fi
 
 case "${mode}" in
-    "check" ) check ${@}    ;;
-    "show"  ) show          ;;
-    "help"  ) _help; exit 0 ;;
-    *       ) _help; exit 1 ;;
+    "check" ) check "${@}"                ;;
+    "show"  ) show                        ;;
+    "desc"  ) desc "${@}"                 ;;
+    "ver"   ) get_alteriso_version "${@}" ;;
+    "help"  ) _help; exit 0               ;;
+    *       ) _help; exit 1               ;;
 esac