--- /dev/null
+#!/bin/bash
+
+set -e
+set -u
+
+BASE_NAME="$( basename ${0} )"
+MY_REAL_NAME=$( readlink -f $0 )
+BIN_DIR=$( dirname "${MY_REAL_NAME}" )
+BASE_DIR=$( dirname "${BIN_DIR}" )
+
+if [[ -f "${BIN_DIR}/functions.rc" ]] ; then
+ . "${BIN_DIR}/functions.rc"
+else
+ echo "Bash resource file '${BIN_DIR}/functions.rc' not found" >&2
+ exit 5
+fi
+
+detect_color
+
+IP=
+HAS_IPV6=
+NO_IPV6=
+
+REAL_HOSTSFILE='/etc/hosts'
+FAKE_HOSTSFILE='/tmp/hosts'
+USED_HOSTSFILE="${REAL_HOSTSFILE}"
+
+SIMULATE="y"
+
+#------------------------------------------------------------------------------
+get_ip() {
+ local iface=
+
+ IP=
+
+ for iface in $( ip -o link show | tail -n +2 | awk '{print $2}' | sed -e 's/:$//' ); do
+
+ IP=$( ip -o addr show dev ${iface} | grep -w inet | head -n 1 | awk '{print $4}' | sed -e 's|/.*||' )
+ if [[ -n "${IP}" ]] ; then
+ break
+ fi
+
+ done
+
+}
+
+#------------------------------------------------------------------------------
+has_ipv6() {
+ if ip -o addr show dev lo | grep -q -w inet6 ; then
+ HAS_IPV6='yes'
+ fi
+}
+
+
+NODENAME=$( hostname --short )
+FQDN=$( hostname --fqdn )
+get_ip
+has_ipv6
+
+#------------------------------------------------------------------------------
+DESCRIPTION=$( cat <<-EOF
+ Writes valid lines in /etc/hosts.
+
+ EOF
+)
+
+#------------------------------------------------------------------------------
+usage() {
+ cat <<-EOF
+ Usage: ${BASE_NAME} ${STD_USAGE} [-N|--no-ipv6]
+ ${BASE_NAME} [-h|--help]
+ ${BASE_NAME} [-V|--version]
+
+ Options:
+ -N|--no-ipv6 Don't insert a line for '::1' in /etc/hosts, even if IPv6 seems to be enabled.
+ EOF
+
+ echo "${STD_USAGE_MSG}"
+}
+
+#------------------------------------------------------------------------------
+get_options() {
+
+ local tmp=
+ local short_options="N${STD_SHORT_OPTIONS}"
+ local long_options="no-ipv6,${STD_LONG_OPTIONS}"
+
+ set +e
+ tmp=$( getopt -o "${short_options}" --long "${long_options}" -n "${BASE_NAME}" -- "$@" )
+ if [[ $? != 0 ]] ; then
+ echo "" >&2
+ usage >&2
+ exit 1
+ fi
+ set -e
+
+ # Note the quotes around `$TEMP': they are essential!
+ eval set -- "${tmp}"
+ eval_common_options "$@"
+ if [[ "${DEBUG}" == 'y' ]] ; then
+ declare -p REMAINING_OPTS
+ declare -p REMAINING_ARGS
+ fi
+
+ local len="${#REMAINING_OPTS[*]}"
+ local i="0"
+ local j=
+ local arg=
+ while [[ "$i" -lt "${len}" ]] ; do
+
+ arg="${REMAINING_OPTS[$i]}"
+
+ case "${arg}" in
+ -N|--no-ipv6)
+ NO_IPV6='y'
+ i=$(( i + 1 ))
+ ;;
+ *) echo -e "Internal error - option '${RED}${arg}${NORMAL} was wrong!"
+ exit 1
+ ;;
+ esac
+ done
+
+ if [[ "${SIMULATE}" == "y" ]] ; then
+ local msg=$( cat <<-EOF
+
+ Using '${CYAN}${FAKE_HOSTSFILE}${NORMAL}' instead of '${CYAN}${REAL_HOSTSFILE}${NORMAL}'.
+ EOF
+ )
+ echo -e "${msg}"
+ USED_HOSTSFILE="${FAKE_HOSTSFILE}"
+ empty_line
+ fi
+
+ check_for_root
+
+}
+
+#------------------------------------------------------------------------------
+
+generate() {
+
+ local tabs=
+ local len_ip=
+
+ empty_line
+ info "Generating new '${CYAN}${USED_HOSTSFILE}${NORMAL}' ..."
+
+ echo "127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4" > "${USED_HOSTSFILE}"
+
+ if [[ "${HAS_IPV6}" && -z "${NO_IPV6}" ]] ; then
+ echo "::1 localhost localhost.localdomain localhost6 localhost6.localdomain6" >> "${USED_HOSTSFILE}"
+ fi
+
+ echo '' >> "${USED_HOSTSFILE}"
+
+ if [[ "${IP}" ]] ; then
+ tabs=' '
+ len_ip=$( printf "${IP}" | wc -c )
+ if [[ "${len_ip}" -lt 8 ]] ; then
+ tabs+=" "
+ fi
+
+ echo "${IP}${tabs}${FQDN} ${NODENAME}" >> "${USED_HOSTSFILE}"
+
+ fi
+
+ if [[ "${QUIET}" != 'y' ]] ; then
+ empty_line
+ info "Generated '${CYAN}${USED_HOSTSFILE}${NORMAL}':"
+ cat "${USED_HOSTSFILE}"
+ echo "<--------"
+ fi
+
+}
+
+################################################################################
+##
+## Main
+##
+################################################################################
+
+#------------------------------------------------------------------------------
+main() {
+
+ get_options "$@"
+ set_locale "en_US.utf8"
+
+ empty_line >&2
+
+ generate
+
+}
+
+main "$@"
+
+exit 0
+
+# vim: list