#!/bin/bash
# Copyright 2010 Novell, Inc.
# Author: Peter Bowen <pzb@novell.com> as a work made for hire
#
#  Licensed under the Apache License, Version 2.0 (the "License");
#  you may not use this file except in compliance with the License.
#  You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
#  Unless required by applicable law or agreed to in writing, software
#  distributed under the License is distributed on an "AS IS" BASIS,
#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#  See the License for the specific language governing permissions and
#  limitations under the License.

##function: isValidHost
function isValidHost
{
  getent hosts "$1" &>/dev/null
}

##function: ec2_instance_data
ec2_instance_data() {
  local ver="latest"
  [ "$2" ] && ver="$2"
  curl --retry 3 --retry-delay 0 --silent --fail "http://169.254.169.254/$ver/$1"
  [ $? -eq 0 ] && echo
}

##function: ec2_meta_data
ec2_meta_data() {
  ec2_instance_data "meta-data/$1" "$2"
}

##function: ec2_region
function ec2_region() {
    local az=$(ec2_meta_data placement/availability-zone 2008-02-01)
    if [ "$az" ]; then
        echo ${az%?}
    else
        echo "default"
    fi
}

##function: system_flavour
function system_flavour() {
    local sf=/etc/ec2/system-flavour
    if [ ! -f $sf -o ! -s $sf ]; then
        echo
        return
    fi
    echo -n -
    head -n 1 $sf
}

# Note, the result of the function is not verified
##function find-update-host
function find_update_host() {
    local region=$(ec2_region)
    local flavour=$(system_flavour)
    local rhost="default-ec2-update${flavour}.$EC2_UPDATE_DOMAIN"
    isValidHost "${region}-ec2-update${flavour}.$EC2_UDPATE_DOMAIN"
    if [ $? -eq 0 ]; then
      rhost="${region}-ec2-update${flavour}.$EC2_UPDATE_DOMAIN"
    fi
    # No HTTPS, because ELB+HTTPS has issues
    local ts=$(date +'%s')
    local realhost=$(curl --location --retry 3 --retry-delay 0 --silent --fail "http://$rhost/2010-08-29/ec2/findupdatehost/storable?ts=$ts")
    [ -n "$realhost" ] && echo "$realhost" || echo "$rhost"
}

[ -f /etc/sysconfig/amazon ] && . /etc/sysconfig/amazon
[ "$EC2_UPDATE_DOMAIN" ] || EC2_UPDATE_DOMAIN="susecloud.net"
UPDATE_REGEX=$(echo "$EC2_UPDATE_DOMAIN" | sed -e 's/\./\\./g')

UPDATEHOST=$(find_update_host)

[ -d /etc/zypp/repos.d ] && find /etc/zypp/repos.d -type f -print | while read FN; do
  sed -i -r -e 's@(baseurl[[:space:]]*=[[:space:]]*.*://)[^/]+-ec2-update(-[a-z]+)?\.'"$UPDATE_REGEX"'/@\1'"$UPDATEHOST"'/@' "$FN"
done

[ -d /etc/zypp/services.d ] && find /etc/zypp/services.d -type f -print | while read FN; do
  sed -i -r -e 's@(url[[:space:]]*=[[:space:]]*.*://)[^/]+-ec2-update(-[a-z]+)?\.'"$UPDATE_REGEX"'/@\1'"$UPDATEHOST"'/@' "$FN"
done

[ -d /var/lib/zypp/db/sources ] && find /var/lib/zypp/db/sources -type f -print | while read FN; do
  sed -i -r -e 's@(<url>[[:space:]]*.*://)[^/]+-ec2-update(-[a-z]+)?\.'"$UPDATE_REGEX"'/@\1'"$UPDATEHOST"'/@' "$FN"
done

[ -f /etc/suseRegister.conf ] \
  && sed -i -r -e 's@(url[[:space:]]*=[[:space:]]*.*://)[^/]+-ec2-update(-[a-z]+)?\.'"$UPDATE_REGEX"'/@\1'"$UPDATEHOST"'/@' /etc/suseRegister.conf
