#!/bin/bash

# Copyright (C) 2007, 2008, 2009 Google Inc.
#
# 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

if [ "$GENERATE_CACHE" = "yes" -a ! -d "$CACHE_DIR" ]; then
  mkdir -p "$CACHE_DIR"
fi

ARCH=${ARCH:-`arch`}
CACHE_FILE="$CACHE_DIR/cache-${SUITE}-${ARCH}.tar"

# 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

if [ "$PARTITION_STYLE" = "none" ]; then
  filesystem_dev=$blockdev
elif [ "$PARTITION_STYLE" = "msdos" ]; then
  # Create one big partition, and make it bootable
  format_disk0 $blockdev
  filesystem_dev=$(map_disk0 $blockdev)
  CLEANUP+=("unmap_disk0 $blockdev")
else
  echo "Unknown partition style $PARTITION_STYLE"
  exit 1
fi

mke2fs -Fjq $filesystem_dev
root_uuid=$($VOL_ID $filesystem_dev )

if [ -n "$swapdev" ]; then
  mkswap $swapdev
  swap_uuid=$($VOL_ID $swapdev || true )
fi

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

mount $filesystem_dev $TMPMNTDIR || exit 1
CLEANUP+=("umount $TMPMNTDIR")

# remove the cache file if it's old (> 2 weeks) and writable by the owner (the
# default due to the standard umask)
if [ "$CLEAN_CACHE" -a -d "$CACHE_DIR" ]; then
  find "$CACHE_DIR" -name 'cache-*.tar' -type f \
    -daystart -mtime "+${CLEAN_CACHE}" -print0 | \
    xargs -r0 rm -f
fi

if [ -f "$CACHE_FILE" ]; then
  tar xf "$CACHE_FILE" -C $TMPMNTDIR
else
  if [ "$PROXY" ]; then
    export http_proxy="$PROXY"
  fi
  TMPCACHEFILE=`mktemp --tmpdir=/var/cache/ganeti-instance-altbootstrap/` || exit 1
  CLEANUP+=("rm -f $TMPCACHEFILE")

  chown $GNTUSER $TMPCACHEFILE
  ls -la $TMPCACHEFILE
  su - $GNTUSER -c "mkve-cache --create-fake-devices --output $TMPCACHEFILE interactivesystem udev udev-rules openssh-server etcnet-defaults-server apt dhcpcd rsync wget openssh-clients glibc-locales unzip vim-console $EXTRA_PKGS " || exit 1
  pushd $TMPMNTDIR
  tar -xf $TMPCACHEFILE || exit 1
  popd

  if [ "$GENERATE_CACHE" = "yes" ] && [ ! -z "$CACHE_FILE" ]; then
    mv -f $TMPCACHEFILE "$CACHE_FILE"
    chown root $CACHE_FILE
  fi
fi

cp -p /etc/hosts $TMPMNTDIR/etc/hosts
cp -p /etc/resolv.conf $TMPMNTDIR/etc/resolv.conf
echo "NETWORKING=yes
HOSTNAME=\"$instance\"
" > $TMPMNTDIR/etc/hostname

mkdir -p $TMPMNTDIR/etc/net/ifaces/eth0
echo "TYPE=eth
BOOTPROTO=dhcp
" > $TMPMNTDIR/etc/net/ifaces/eth0/options


cat > $TMPMNTDIR/etc/fstab <<EOF
# /etc/fstab: static file system information.
#
# <file system>   <mount point>   <type>  <options>       <dump>  <pass>
UUID=$root_uuid   /               ext3    defaults        0       1
proc              /proc           proc    defaults        0       0
EOF

sed -i 's/root:x:/root::/' $TMPMNTDIR/etc/tcb/root/shadow

sed -i "s/localhost.localdomain/$instance/" $TMPMNTDIR/etc/sysconfig/network

if [ $OSVARIANT = "xen-pvm" ]; then
 if ! grep "^hvc0$" /etc/securetty > /dev/null; then
  echo "hvc0" >> $TMPMNTDIR/etc/securetty
 fi
 sed -i 's/tty1/hvc0/' $TMPMNTDIR/etc/inittab
fi

[ -n "$swapdev" -a -n "$swap_uuid" ] && cat >> $TMPMNTDIR/etc/fstab <<EOF
UUID=$swap_uuid   swap            swap    defaults        0       0
EOF

RUN_PARTS=`which run-parts`

if [ -n "$RUN_PARTS" -a -n "$CUSTOMIZE_DIR" -a -d "$CUSTOMIZE_DIR" ]; then
  TARGET=$TMPMNTDIR
  BLOCKDEV=$blockdev
  FSYSDEV=$filesystem_dev
  export TARGET SUITE ARCH PARTITION_STYLE EXTRA_PKGS BLOCKDEV FSYSDEV
  $RUN_PARTS $CUSTOMIZE_DIR
fi

# execute cleanups
cleanup
trap - EXIT

exit 0
