#!/bin/bash
set -e

# trap cleanup SIGTERM EXIT ERR

## To do setup your local environment do the following:
# 1. Make a local directory
# 2. Set permissions, so only you can edit files inside.
# 3. Create a directory in which to place your disk images.
# 4. Copy the already downloaded and extracted disk image from the provided directory.
# 5. Setup marss path variable

  # http://bertha.cs.binghamton.edu/downloads
  URL="http://people.cs.uchicago.edu/~kauffman/courses/marssx86"

  # git://github.com/avadhpatel/marss.git
  #GITURL="http://vcs.cs.uchicago.edu/kauffman/marss-hw5.git"
  GITURL="git://github.com/avadhpatel/marss.git"
  DISKIMAGE="ubuntu-natty-lab1"
  DISKMD5SUM="a01940a904fb4f517e18dd87ff09b855"

  # DO NOT MODIFY ANYTHING BELOW THIS LINE
  GIT=/usr/bin/git
  SCONS=/usr/bin/scons

  MARSS="$(pwd)/marss"
  LOCAL_DIR='/local'
  MARSSX86_INSTALL_DIR="/usr/local/marssx86"
  MARSSX86_LOCAL_DISK_IMAGE_DIR="$LOCAL_DIR/$USER/marssx86/disk-images"

  checkmd5(){
    md5="$1"
    file="$2"
    computedmd5=`md5sum $file | awk '{print $1}'`
    if [ "$computedmd5" == "$md5" ]; then
      echo 0
    else
      echo $computedmd5
    fi
  }
  # checkmd5 "$DISKMD5SUM" "${MARSSX86_LOCAL_DISK_IMAGE_DIR}/${DISKIMAGE}.qcow2"

  echo "Cloning marssx86 git repository into a folder in current directory..."
  if [[ ! -d $MARSS && ! -d $MARSS/.git ]]; then
    $GIT clone $GITURL marss
  else
    echo "The directory ${MARSS}/.git already exists moving on..."
    echo "  Meaning the repo was previously cloned... probably."
  fi

  echo "Entering directory and building marssx86..."
  cd $MARSS
  if [ -f $MARSS/qemu/qemu-system-x86_64 ]; then
    echo "It seems marss has already been compiled."
    echo "  To clean and recompile run the following:"
    echo "  cd $MARSS && scons -Q -c # to clean binaries"
    # echo "  cd $MARSS && scons -Q    # to compile marssx86"
    # $SCONS -Q -c # ~= make clean
  else
    $SCONS -Q    # ~= make
  fi

  echo "creating local directories and chmoding /local/$USER to 700"
  mkdir -p ${LOCAL_DIR}/${USER}
  chmod 700 ${LOCAL_DIR}/${USER}
  mkdir -p $MARSSX86_LOCAL_DISK_IMAGE_DIR

  if [ ! -f $MARSSX86_LOCAL_DISK_IMAGE_DIR/${DISKIMAGE}.qcow2 ]; then

      # if package not installed download the disk image
      if [ -d $MARSSX86_INSTALL_DIR ]; then
        echo "The package 'marssx86-uofc-cs' was installed and the disk image exists at $MARSSX86_LOCAL_DISK_IMAGE_DIR/${DISKIMAGE}.qcow2"
        echo "  syncing from install directory to ${MARSSX86_LOCAL_DISK_IMAGE_DIR}"
        rsync -avz --progress "${MARSSX86_INSTALL_DIR}/disk-images/${DISKIMAGE}.qcow2" ${MARSSX86_LOCAL_DISK_IMAGE_DIR}/
      else
        echo "The package 'marssx86-uofc-cs' is not installed. Downloading the disk image from the internet."
        wget -c -O ${MARSSX86_LOCAL_DISK_IMAGE_DIR}/${DISKIMAGE}.tar.bz2 ${URL}/${DISKIMAGE}.tar.bz2
        echo "Extracting image..."
        tar -xjvf ${MARSSX86_LOCAL_DISK_IMAGE_DIR}/${DISKIMAGE}.tar.bz2 -C ${MARSSX86_LOCAL_DISK_IMAGE_DIR}/ && \
        rm ${MARSSX86_LOCAL_DISK_IMAGE_DIR}/${DISKIMAGE}.tar.bz2
      fi
  else
    echo "Disk image already exists."
    echo "  Testing md5sum: $DISKMD5SUM"

    md5=`checkmd5 "$DISKMD5SUM" "$MARSSX86_LOCAL_DISK_IMAGE_DIR/${DISKIMAGE}.qcow2"`
    if [ $md5 == "0" ]; then
      echo "  The md5sum matches!"
    else
      echo "The md5sum of $MARSSX86_LOCAL_DISK_IMAGE_DIR/${DISKIMAGE}.qcow2 does not match the original."
      echo "  Original:  $DISKMD5SUM"
      echo "  Your disk: $md5"
      echo "  This could be due to an incomplete download/sync or"
      echo "  an intentional change you made to the virtual disk."
    fi
  fi




## Running marssx86
# http://marss86.org/~marss86/index.php/Getting_Started#Running_MARSS
  cd $MARSS
# usage: qemu/qemu-system-x86_64 -m [memory_size] -hda [path-to-qemu-disk-image]
#  qemu/qemu-system-x86_64 -m 512 -hda ${MARSSX86_LOCAL_DISK_IMAGE_DIR}/${DISKIMAGE}.qcow2
