#!/bin/bash
#
# Activate a partition of a bochs disk-image.
# 
# $Author: bablokb $
# $Revision: 1.2 $
#
# License: GPL2
# -----------------------------------------------------------------------------

. `dirname $0`/bxtfuncs.inc

# usage message   -------------------------------------------------------------

usage() {
  echo -e "\n`basename $0`: activate a partition of a bochs-hd-image\n\
  \nusage: `basename $0` [options] image-name:partition\n\
  possible options:\n\
    -V       show version\n\
    -h       show this help\n\
    -q       run quiet (default)\n\
    -v       verbose messages\n\
    -s       only simulate\n\
"
  exit 3
}

# set system defaults   -------------------------------------------------------

setDefaults() {
  quiet=1; simulate=0
  partition=""
}

# parse arguments and set variables -------------------------------------------

parseArguments() {

  while getopts ":Vhqvs" opt; do
    case $opt in
      V) showCopyright `basename $0` "activate a partition of a bochs-hd-image";;
      h) usage;;
      q) quiet=1;;
      v) quiet=0;;
      s) simulate=1;;
      ?) echo "error: illegal option: $OPTARG" >&2
           usage;;
    esac
  done
  if [ $# -eq $OPTIND ]; then
    imageName=${!OPTIND%:*}
    partition=${!OPTIND#*:}
  elif [ $# -gt $OPTIND ]; then
    echo "error: illegal argument ${!#}" >&2
    usage
  else
    echo "error: missing argument image-name:partition" >&2
    usage
  fi
}

# check arguments   -----------------------------------------------------------

checkArguments() {
  if [ ! -f $imageName ]; then
    echo "error: image $imageName does not exist!" >&2
    exit 1
  fi
  if [ -z $partition ]; then
    echo "error: no partition number specified!" >&2
    exit 1
  fi
}

# activate the given partition   ----------------------------------------------

executeActivate() {
  cmd="sfdisk -q -L -A$partition -C $cyl -H $heads -S $spt $imageName"
  if ! do_cmd "$cmd"; then
    echo "error: could not activate partition $partition of $imageName" >&2
    exit 1
  fi
}

# main program   --------------------------------------------------------------

setDefaults
parseArguments "$@"
checkSfdisk
checkArguments
setGeometry $imageName
executeActivate
exit 0
