Skip to content

Instantly share code, notes, and snippets.

@thiagozs
Created September 10, 2013 17:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thiagozs/6513109 to your computer and use it in GitHub Desktop.
Save thiagozs/6513109 to your computer and use it in GitHub Desktop.
Download from apache with check geo targeting
#!/bin/bash
# author: André Kelpe <efeshunderelf at googlemail.com>
# licencse: Apache v2
GRRR_WGET_OPTIONS="--user-agent grrr/1.0"
# find out our region and yes, you can get this as csv file. How cool is that?
GEOIP_REGION=$(wget -qO- freegeoip.net/csv/ | tr '[A-Z]' '[a-z]' | tr -d '"'| awk -F, '{print $2}')
# classic confusion between geoip db and apache mirror list
if [ $GEOIP_REGION == "gb" ]; then
GEOIP_REGION=uk
fi
MIRRORLIST_FILE_NAME=$(mktemp)
# download the latest mirror list from apache. we ignore the last
# sync times and hope for the best...
wget -qO- http://www.apache.org/mirrors/mirrors.list | grep -v '^$' \
| grep http | grep -v ' 0$' | grep -v '^#' > $MIRRORLIST_FILE_NAME
# use US as the default region. apache does the same in their scripts...
REGION=us
# check if there is a mirror in our region
if grep -q " $GEOIP_REGION " $MIRRORLIST_FILE_NAME; then
REGION=$GEOIP_REGION
fi
# finally download it all
wget $GRRR_WGET_OPTIONS $(grep " $REGION " $MIRRORLIST_FILE_NAME | shuf | head -1 | awk '{print $3}')/$*
retval=$?
# clean up after ourselves.
rm $MIRRORLIST_FILE_NAME
exit $retval
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment