#!/bin/sh
Usage() {
   cat <<EOF
Usage: euca-psuedo-grub-probe
   this is a wrapper around grub-probe to provide the answers for an ec2 guest
EOF
}
bad_Usage() { Usage 1>&2; fail "$@"; }

short_opts=""
long_opts="device-map:,target:,device"
getopt_out=$(getopt --name "${0##*/}" \
   --options "${short_opts}" --long "${long_opts}" -- "$@") &&
   eval set -- "${getopt_out}" ||
   bad_Usage

device_map=""
target=""
device=0
arg=""

while [ $# -ne 0 ]; do
   cur=${1}; next=${2};
   case "$cur" in
      --device-map) device_map=${next}; shift;;
      --device) device=1;;
      --target) target=${next}; shift;;
      --) shift; break;;
   esac
   shift;
done
arg=${1}

case "${target}:${device}:${arg}" in
   device:*:/*) echo "/dev/sda1"; exit 0;;
   fs:*:*) echo "ext2"; exit 0;;
   partmap:*:*)
      # older versions of grub (lucid) want 'part_msdos' written
      # rather than 'msdos'
      legacy_pre=""
      grubver=$(dpkg-query --show --showformat '${Version}\n' grub-pc 2>/dev/null) &&
         dpkg --compare-versions "${grubver}" lt 1.98+20100804-5ubuntu3 &&
         legacy_pre="part_"
      echo "${legacy_pre}msdos";
      exit 0;;
   abstraction:*:*) echo ""; exit 0;;
   drive:*:/dev/sda) echo "(hd0)";;
   drive:*:/dev/sda*) echo "(hd0,1)";;
   fs_uuid:*:*) exit 1;;
esac
