david_williams | 438e208 | 2009-06-07 05:59:09 +0000 | [diff] [blame] | 1 | #!/bin/bash |
| 2 | |
| 3 | # Simple script to count number of mirrors available for a particular URL |
| 4 | |
| 5 | # Copyright (c) 2009 IBM |
| 6 | # All rights reserved. This program and the accompanying materials |
| 7 | # are made available under the terms of the Eclipse Public License v1.0 |
| 8 | # which is available at |
| 9 | # http://www.eclipse.org/legal/epl-v10.html |
| 10 | # |
| 11 | # Contributors: |
| 12 | # David Williams - initial API and implementation |
| 13 | |
| 14 | |
| 15 | function usage() { |
| 16 | printf "\n\tSimple script to count number of mirrors available for a particular URL" >&2 |
| 17 | printf "\n\tUsage: %s [-h] | [-v] [-f] [-p] [-t number] urls" $(basename $0) >&2 |
| 18 | printf "\n\t\t%s" "where h==help, v==verbose, f==only ftp mirrors, p==only http mirrors, t==test against 'number' " >&2 |
| 19 | printf "\n\t\t%s" "and urls is space delimited list of URL parameters," >&2 |
| 20 | printf "\n\t\t%s\n" "such as /ganymede/releases/site.xml" >&2 |
| 21 | } |
| 22 | |
| 23 | function checkMirrorsForURL() { |
| 24 | |
| 25 | if [ -z $1 ] |
| 26 | then |
| 27 | echo "Error: internal funtion requires mirror url parameter"; |
| 28 | exit 3; |
| 29 | else |
| 30 | mirrorURL=$1 |
| 31 | fi |
| 32 | if [ -z $2 ] |
| 33 | then |
| 34 | protocol= |
| 35 | pword="http and ftp" |
| 36 | else |
| 37 | protocolarg="&protocol=$2" |
| 38 | pword="$2" |
| 39 | fi |
| 40 | |
| 41 | nMirrors=$(wget -q -O - "http://www.eclipse.org/downloads/download.php?file=${mirrorURL}&format=xml${protocolarg}" | grep \<mirror\ | wc -l) |
| 42 | echo " number of" ${pword} "mirrors: " ${nMirrors} " for" ${mirrorURL} |
| 43 | exit $nMirrors |
| 44 | } |
| 45 | |
| 46 | function minvalue() { |
| 47 | ref=$1 |
| 48 | comp=$2 |
| 49 | result= |
| 50 | #echo "ref: $ref \r"; |
| 51 | #echo "comp: " $comp "\r" |
| 52 | if [ -z $ref ] |
| 53 | then |
| 54 | #echo "no ref\r" |
| 55 | result=$comp |
| 56 | else |
| 57 | if [ $ref -lt $comp ] |
| 58 | then |
| 59 | #echo "ref lt comp\r" |
| 60 | result=$ref |
| 61 | else |
| 62 | #echo "comp <= ref\r" |
| 63 | result=$comp |
| 64 | fi |
| 65 | fi |
| 66 | echo $result |
| 67 | } |
| 68 | |
| 69 | |
| 70 | urls= |
| 71 | ftponly=0 |
| 72 | httponly=0 |
| 73 | protocol= |
| 74 | while getopts 'hvfpt:' OPTION |
| 75 | do |
| 76 | case $OPTION in |
| 77 | h) usage |
| 78 | exit 1 |
| 79 | ;; |
| 80 | f) ftponly=1 |
| 81 | ;; |
| 82 | p) httponly=1 |
| 83 | ;; |
| 84 | v) verbose=1 |
| 85 | ;; |
| 86 | t) testNumber=$OPTARG |
| 87 | ;; |
| 88 | ?) usage |
| 89 | exit 2 |
| 90 | ;; |
| 91 | esac |
| 92 | done |
| 93 | |
| 94 | shift $(($OPTIND - 1)) |
| 95 | |
| 96 | urls="$@" |
| 97 | |
| 98 | if [ $ftponly == 1 ] |
| 99 | then |
| 100 | protocol="ftp" |
| 101 | fi |
| 102 | if [ $httponly == 1 ] |
| 103 | then |
| 104 | protocol="http" |
| 105 | fi |
| 106 | if [ $ftponly == 1 -a $httponly == 1 ] |
| 107 | then |
| 108 | protocol= |
| 109 | fi |
| 110 | |
| 111 | if [ $verbose ] |
| 112 | then |
| 113 | echo "ftponly: " $ftponly " httponly: " $httponly |
| 114 | echo "protocol: " $protocol |
| 115 | echo "urls: " $urls |
| 116 | fi |
| 117 | |
| 118 | minimumMirrors= |
| 119 | if [ "${urls}" ] |
| 120 | then |
| 121 | echo |
| 122 | for mirrorURL in ${urls} |
| 123 | do |
| 124 | nm=checkMirrorsForURL $mirrorURL $protocol |
| 125 | minimumMirrors=min $minimumMirrors $nm |
| 126 | done |
| 127 | echo |
| 128 | else |
| 129 | usage |
| 130 | fi |
| 131 | |
| 132 | if [ -z $testNumber ] |
| 133 | then |
| 134 | if [ $verbose ] |
| 135 | then |
| 136 | echo "no test mode" |
| 137 | fi |
| 138 | exit 0 |
| 139 | else |
| 140 | result=$($testNumber - $minimumMirrors) |
| 141 | if [ $result <= 0 ] |
| 142 | then |
| 143 | if [ $verbose ] |
| 144 | then |
| 145 | echo "minimum mirrors was greater than or equal to criteria" |
| 146 | fi |
| 147 | exit 0 |
| 148 | else |
| 149 | if [ $verbose ] |
| 150 | then |
| 151 | echo "minimum mirrors was not as large as criteria" |
| 152 | fi |
| 153 | exit result |
| 154 | fi |
| 155 | fi |