#!/bin/bash

# Copyright (C) 2010, 2011, 2012 Oregon State University
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301, USA.

set -e

. common.sh

debug set -x

if [ "${NOMOUNT}" = "yes" ] ; then
    # do nothing if NOMOUNT is enabled
    exit 0
fi

TARGET=`mktemp -d` || exit 1
CLEANUP+=("rmdir $TARGET")

# If the target device is not a real block device we'll first losetup it.
# This is needed for file disks.
if [ ! -b $blockdev ]; then
  ORIGINAL_BLOCKDEV=$blockdev
  blockdev=$($LOSETUP -sf $blockdev)
  CLEANUP+=("$LOSETUP -d $blockdev")
fi

filesystem_dev=$(map_disk0 $blockdev)
CLEANUP+=("unmap_disk0 $blockdev")
root_dev=$(map_partition $filesystem_dev root)
boot_dev=$(map_partition $filesystem_dev boot)

mount_disk0 $TARGET
get_os_type $TARGET

case "${OS_TYPE}" in
    debian)
        HNAME="${TARGET}/etc/hostname"
        OLD_HNAME="$(cat $HNAME)"
        ;;
    redhat)
        HNAME="${TARGET}/etc/sysconfig/network"
        OLD_HNAME="$(grep HOSTNAME ${HNAME} | \
            sed -e 's/HOSTNAME=//' | \
            sed -e s/\"//g)"
        ;;
    gentoo)
        HNAME="${TARGET}/etc/conf.d/hostname"
        # baselayout-2.x support
        if [ -d ${TARGET}/usr/share/openrc ] ; then
            OLD_HNAME="$(grep hostname ${HNAME} | \
                sed -e 's/hostname=//' | \
                sed -e s/\"//g)"
        else
            OLD_HNAME="$(grep HOSTNAME ${HNAME} | \
                sed -e 's/HOSTNAME=//' | \
                sed -e s/\"//g)"
        fi
        ;;
    suse)
        HNAME="${TARGET}/etc/HOSTNAME"
        OLD_HNAME="$(cat $HNAME)"
        ;;
esac

if [ "$OLD_HNAME" = "$old_name" \
        -o "${OLD_HNAME}.$(echo $old_name | cut -d . -f 2,3)" \
        = "$old_name" ] ; then
    case "${OS_TYPE}" in
        debian|suse)
            echo $instance > $HNAME
            ;;
        redhat)
            sed -ie "s/HOSTNAME=${OLD_HNAME}/HOSTNAME=${instance}/" $HNAME
            ;;
        gentoo)
            if [ -d ${TARGET}/usr/share/openrc ] ; then
                sed -ie "s/hostname.*/hostname=\"${instance}\"/" $HNAME
            else
                sed -ie "s/HOSTNAME.*/HOSTNAME=\"${instance}\"/" $HNAME
            fi
            ;;
    esac

    if [ -x ${CUSTOMIZE_DIR}/zz_ddns ] ; then
        TARGET=$TARGET
        export TARGET
        ${CUSTOMIZE_DIR}/zz_ddns -r
    fi
else
    log_error "Cannot rename from $old_name to $instance:"
    log_error "Instance has a different hostname ($OLD_HNAME)"
    exit 1
fi

# execute cleanups
cleanup
trap - EXIT

exit 0
