#!/bin/bash

USER_DATA=/mnt/user-data
EBS_DEV=/dev/sdf
EBS_MNT=/mnt/ebs0
PERSIST_LIST=/etc/instpersist.cfg

# Create a single parition
fdiskdev () {
fdisk $1 <<EOF
n
p
1


p
w
EOF
}

# Transplant data to the EBS volume
transplant () {

  PTH=$1
  EBS=$2

  echo "Checking transplant of $PTH to $EBS"

  if [ ! -e ${EBS}${PTH} ]; then

    echo "Relocating ${PTH} ${EBS}${PTH}"

    PDIR=`dirname ${EBS}${PTH}`

    # Create a mysql directory on the EBS volume.
    mkdir -p $PDIR

    # Copy the current data to the EBS volume.
    rsync -a $PTH $PDIR
  fi

  if [ ! -e ${PTH}.EPHEMERAL ]; then

    echo "Replacing ${PTH} with link to ${EBS}${PTH}"

    # Move the old path aside.
    mv ${PTH} ${PTH}.EPHEMERAL

    # Create symbolic link to the new place.
    ln -s ${EBS}${PTH} ${PTH}

  fi
}

# Is this a reboot?
if [ -e $USER_DATA ]; then
  echo "instbootstrap saw REBOOT"
  exit 0
fi

# It's not a reboot, fetch the user-data
wget http://169.254.169.254/latest/user-data -O $USER_DATA

if [ ! -s $USER_DATA ]; then

  # Started in ephemeral state.
  echo "instbootstrap started in EPHEMERAL"

else

  # Which mode are we in?
  if grep -qi INIT_EBS $USER_DATA; then

    # We are INITIALIZING an EBS volume.
    echo "instbootstrap saw INIT_EBS"

    # Shutdown services.
    service httpd stop
    service mysqld stop

    # Wait for the unformatted EBS volume.
    echo "instbootstrap waiting for EBS on ${EBS_DEV} ..."
    while [ ! -e $EBS_DEV ]; do
      inotifywait -qq /dev
    done

    # Hate this.
    sleep 5

    # Write a parition table.
    echo "Creating partion table on $EBS_DEV"
    fdiskdev $EBS_DEV

    # Create a filesystem.
    echo "Making filesystem on $EBS_DEV"
    mkfs -t ext3 ${EBS_DEV}1

    # Create a mount point.
    mkdir ${EBS_MNT}

    # Append line to /etc/fstab.
    echo "${EBS_DEV}1   ${EBS_MNT}  ext3    defaults 1 2" >> /etc/fstab

    # Mount the filesystem.
    echo "Mounting ${EBS_MNT}"
    mount ${EBS_MNT}

    # Transplant things.
    for path in `cat $PERSIST_LIST`; do
        transplant $path ${EBS_MNT}
    done

    # Restart services.
    service mysqld start
    service httpd start

    # Make sure we don't repeat the init.
    echo USE_EBS > $USER_DATA

  elif grep -qi USE_EBS $USER_DATA; then

    # We are USING an existing EBS volume.
    echo "instbootstrap saw USE_EBS"

    # Shutdown services.
    service httpd stop
    service mysqld stop

    # Wait for the unformatted EBS volume.
    echo "instbootstrap waiting for EBS on ${EBS_DEV} ..."
    while [ ! -e ${EBS_DEV}1 ]; do
      inotifywait -qq /dev
    done

    # Hate this.
    sleep 5

    # Create a mount point.
    mkdir ${EBS_MNT}

    # Append line to /etc/fstab.
    echo "${EBS_DEV}1   ${EBS_MNT}  ext3    defaults 1 2" >> /etc/fstab

    # Mount the filesystem.
    echo "Mounting ${EBS_MNT}"
    mount ${EBS_MNT}

    # Link things.
    for path in `cat $PERSIST_LIST`; do
        transplant $path ${EBS_MNT}
    done

    # Restart services.
    service mysqld start
    service httpd start

  else

    # Fallback to ephemeral mode.
    echo "instbootstrap fallback to EPHEMERAL"

  fi

fi