#!/bin/bash

### Include ymclibnettools
. /usr/lib/lib-fliwi/ymc-networktools.bash

if [ $(ymc_is_chroot) -ne 0 ]; then
  echo "WARNING: $0 can not be run inside a chroot..."
  exit 0
fi

if [ "$1" != '' ]; then
  matched_hostname=$1
  if [ $(ymc_is_valid_cluster_hostname "$matched_hostname") -ne 1 ]; then
    echo "Supplied hostname is invalid!"
    exit 1
  fi
else
  ### Get all real-interface-macs
  macs=$(ymc_get_real_mac_addresses --dashes)

  ## check each available mac for a valid hostname
  found_valid_hostname=0
  matched_mac=""
  for mac in $macs
  do
    possible_hostname=$(ymc_get_hostname_from_dns_txt "$mac" "macs")
    if [ $? -eq 0 ] && [ $(ymc_is_valid_cluster_hostname "$possible_hostname") -eq 1 ]; then
      found_valid_hostname=1
      matched_mac=$mac
      #real_matched_mac=$(echo $matched_mac | sed -r 's/-/:/g')
      matched_hostname=$possible_hostname
      break
    fi
  done
  if [ $found_valid_hostname -ne 1 ]; then
    echo "ERROR: Did not found a valid hostname!" 1>&2
    exit 1
  fi
fi

### Generate a proper /etc/hostname
echo $matched_hostname > /etc/hostname

### Generate a proper /etc/hosts (1)
echo "# Dynamic hosts.conf(5) file generated by $(basename $0)" > /etc/hosts
echo "#     DO NOT EDIT THIS FILE BY HAND -- YOUR CHANGES WILL BE OVERWRITTEN" >> /etc/hosts

### Check if we need fixups in /etc/hosts
/etc/init.d/hostname.sh start
hosts_fix_for_missing_dns_entries=''
hostname -f 2>/dev/null 1>&2
if [ $? -ne 0 ]; then
  hosts_fix_for_missing_dns_entries="127.0.0.2\t$matched_hostname.cluster"
fi
hostname -s 2>/dev/null 1>&2
if [ $? -ne 0 ]; then
  if [ "$hosts_fix_for_missing_dns_entries" != '' ]; then
    hosts_fix_for_missing_dns_entries=$hosts_fix_for_missing_dns_entries' '$matched_hostname
  else
    hosts_fix_for_missing_dns_entries="127.0.0.2\t$matched_hostname"
  fi
fi

### Generate a proper /etc/hosts (2)
echo -e "127.0.0.1\tlocalhost" >> /etc/hosts
if [ "$hosts_fix_for_missing_dns_entries" != '' ]; then
  echo "# Workaround for invalid dns-config for the local hostname. PLEASE FIXUP YOUR DNS-RECORDS! //start" >> /etc/hosts
  echo "# DNS-Entry missing: $matched_hostname.cluster" >> /etc/hosts
  echo -e $hosts_fix_for_missing_dns_entries >> /etc/hosts
  echo "# Workaround for invalid dns-config for the local hostname. PLEASE FIXUP YOUR DNS-RECORDS! //end" >> /etc/hosts
else
  own_lan_ip=$(gethostip -d $(hostname -s))
  echo -e "$own_lan_ip\t$(hostname -f)\tself.internal.cluster" >> /etc/hosts

  ### Insert self-host-entries for each network
  for network in $(ymc_get_hostname_from_dns_txt $(hostname -s) networks)
  do
    configured_ip=$(ymc_get_field_from_ifconfig $network 'inet addr:')
    if [ $? -eq 0 ] && \
       [ "$configured_ip" != "" ]; then
      echo -n -e "$configured_ip\t" >> /etc/hosts
      reverse_lookup_hostname=$(LC_ALL=C host -i $configured_ip)
      if [ $? -eq 0 ]; then
        reverse_lookup_hostname=$(echo $reverse_lookup_hostname | sed -r 's/[[:space:]]+/ /g' | cut -d ' ' -f 3)
        echo -n -e "$reverse_lookup_hostname\t" >> /etc/hosts
      fi
      reverse_lookup_hostname=''
      echo -n -e "$(hostname -s).$network.cluster\t" >> /etc/hosts
      echo -e "self.$network.cluster" >> /etc/hosts
    fi
  done
fi

echo "" >> /etc/hosts
echo "# The following lines are desirable for IPv6 capable hosts" >> /etc/hosts
echo "::1     ip6-localhost ip6-loopback" >> /etc/hosts
echo "fe00::0 ip6-localnet" >> /etc/hosts
echo "ff00::0 ip6-mcastprefix" >> /etc/hosts
echo "ff02::1 ip6-allnodes" >> /etc/hosts
echo "ff02::2 ip6-allrouters" >> /etc/hosts
echo "ff02::3 ip6-allhosts" >> /etc/hosts

### Reinit the hostname
/etc/init.d/hostname.sh start
