| 1 | #!/bin/bash |
| 2 | # Author: Steven Shiau <steven _at_ clonezilla org> |
| 3 | # License: GPL |
| 4 | # Description: Find the url in rpm-md repository setting (quite similar to yum repository setting). This script is specially for OpenSuSE 11.1 using zypper repository output from command "zypper lr -e myrepo.repo". |
| 5 | # This script is modified from find-url-in-yum-set which is for Fedora. |
| 6 | # parameter: rpm_md_repo_set repo releasever |
| 7 | # if the setting in "rpm_md_repo_set" is like: |
| 8 | # baseurl=http://download.opensuse.org/distribution/11.1/repo/oss/ |
| 9 | |
| 10 | # Ex: find-url-in-rpm-md-set myrepo.repo repo-oss 11.1 |
| 11 | # The result is like: |
| 12 | # http://download.opensuse.org distribution |
| 13 | |
| 14 | rpm_md_repo_set="$1" |
| 15 | repo_name="$2" |
| 16 | releasever="$3" |
| 17 | |
| 18 | [ -f "$rpm_md_repo_set" ] || exit 1 |
| 19 | [ -n "$repo_name" ] || exit 1 |
| 20 | [ -n "$releasever" ] || exit 1 |
| 21 | |
| 22 | get_repo_block() { |
| 23 | local CFG_FILE=$1 |
| 24 | local block="$2" |
| 25 | [ -f "$CFG_FILE" ] || exit 1 |
| 26 | # By using |
| 27 | # grep -n "^\[.*\]" myrepo.repo |grep -A1 "\[base\]" |
| 28 | # We can get the results like: |
| 29 | # 175:[base] |
| 30 | # 210:[update] |
| 31 | # so we know we can replace the one between line no. 175 and 210 |
| 32 | between_lines=$(grep -ni "^\[.*\]" $CFG_FILE | grep -i -A1 "\[$block\]" | cut -d":" -f1) |
| 33 | begin_line=$(echo $between_lines | awk -F" " '{print $1}') |
| 34 | end_line=$(echo $between_lines | awk -F" " '{print $2}') |
| 35 | # if end_line is nothing, it must be the last block, i.e. we can not find the next [.*] |
| 36 | if [ -z "$end_line" ]; then |
| 37 | end_line=$(wc -l $CFG_FILE | awk -F" " '{print $1}') |
| 38 | else |
| 39 | # if not nothing, backword one line |
| 40 | end_line=$(($end_line - 1)) |
| 41 | fi |
| 42 | echo "$begin_line $end_line" |
| 43 | } |
| 44 | |
| 45 | # |
| 46 | convert_url_os_path() { |
| 47 | local url="$1" |
| 48 | [ -n "$url" ] || exit 1 |
| 49 | # For FC: |
| 50 | # baseurl=http://free.nchc.org.tw//fedora/linux/core//$releasever/$basearch/os |
| 51 | # we want to get: |
| 52 | # url_os=http://free.nchc.org.tw/ |
| 53 | # os_ayo_path=fedora/linux/core/ |
| 54 | # For OpenSuSE 10.0 |
| 55 | # baseurl=http://free.nchc.org.tw/opensuse/opensuse/distribution/SL-$releasever-OSS/inst-source/suse/ |
| 56 | # we want to get: |
| 57 | # url_os=http://free.nchc.org.tw |
| 58 | # os_ayo_path=opensuse/opensuse/distribution |
| 59 | |
| 60 | # Note! Maybe in *.repo, releasever is shown as "$releasever", "4" or "10.0" |
| 61 | # Case 1: releasever is shown as "$releasever" |
| 62 | url_os_coreroot="$(echo $baseurl | sed -r -e 's@/(SL-|)*\$releasever(-OSS|)*/.*@@g')" |
| 63 | # Case 2: releasever is shown as "10.0". This is really a special case for OpenSuSE 10.0 and 10.1 only. |
| 64 | # Ex: http://opensource.nchc.org.tw/opensuse/distribution/SL-10.0-OSS/ |
| 65 | # or http://opensource.nchc.org.tw/opensuse/distribution/SL-10.1/ |
| 66 | # From OpenSuSE 10.2 or later, it's clean as: http://opensource.nchc.org.tw/opensuse/distribution/10.2/ |
| 67 | if [ -n "$(echo $url_os_coreroot | grep "SL-")" ]; then |
| 68 | url_os_coreroot="$(echo $url_os_coreroot | sed -r -e "s@/(SL-|)*$releasever(-OSS|)*/.*@@g")" |
| 69 | fi |
| 70 | # Case 3: releasever is shown as "8", for example. |
| 71 | # baseurl=http://free.nchc.org.tw/fedora/linux/releases/8/Everything/i386/os/ |
| 72 | url_os_coreroot="$(echo $url_os_coreroot | sed -r -e "s@/$releasever/.*@@g")" |
| 73 | |
| 74 | # |
| 75 | os_ayo_path="$(echo $url_os_coreroot | sed -re "s|(http\|ftp\|file)://[^/]*/||g")" |
| 76 | url_os="$(echo $url_os_coreroot | sed -re "s|$os_ayo_path||g")" |
| 77 | # remove the last / in $url_os |
| 78 | url_os="$(echo $url_os | sed -re "s|/$||g")" |
| 79 | echo $url_os $os_ayo_path |
| 80 | } # end of convert_url_os_path |
| 81 | |
| 82 | # for rpm md setting |
| 83 | # For OpenSuSE 11.1, the block is [repo-oss] or [repo-update]. |
| 84 | # Ex. |
| 85 | # [repo-oss] |
| 86 | # name=openSUSE-11.1-Oss |
| 87 | # enabled=1 |
| 88 | # autorefresh=1 |
| 89 | # baseurl=http://ftp.twaren.net/Linux/OpenSuSE/distribution/11.1/repo/oss/ |
| 90 | # path=/ |
| 91 | # type=yast2 |
| 92 | # priority=100 |
| 93 | # keeppackages=0 |
| 94 | |
| 95 | # [repo-update] |
| 96 | # name=openSUSE-11.1-Update |
| 97 | # enabled=1 |
| 98 | # autorefresh=1 |
| 99 | # baseurl=http://download.opensuse.org/update/11.1/ |
| 100 | # path=/ |
| 101 | # type=rpm-md |
| 102 | # priority=20 |
| 103 | # keeppackages=0 |
| 104 | |
| 105 | for ipart in $repo_name; do |
| 106 | lines="$(get_repo_block $rpm_md_repo_set $ipart)" |
| 107 | begin_line=$(echo $lines | awk -F" " '{print $1}') |
| 108 | end_line=$(echo $lines | awk -F" " '{print $2}') |
| 109 | [ -n "$begin_line" -a -n "$end_line" ] && break |
| 110 | done |
| 111 | |
| 112 | [ -z "$begin_line" -a -z "$end_line" ] && exit 1 |
| 113 | |
| 114 | chk_cmd="if ($begin_line..$end_line) {print}" |
| 115 | baseurl="$(perl -n -e "$chk_cmd" $rpm_md_repo_set | grep -Ei "^[[:space:]]*baseurl[[:space:]]*=[[:space:]]*" | sed -re "s/^[[:space:]]*baseurl[[:space:]]*=[[:space:]]*//gi")" |
| 116 | convert_url_os_path $baseurl |