#/usr/share/lessdisks/cloner-functions
# common functions between lessdisks-cloner and
# lessdisks-cloner-create

# copyright 2004 vagrant@freegeek.org, distributed under the terms of the
# GNU General Public License version 2 or any later version.

# TODO put the "create" segment of requestModel into it's own
# function

readVersion() {
  local version
  # look for the version file and get the updated line out of it
  [ -r $1/etc/lessdisks-cloner/version ] && \
  version=$(grep updated $1/etc/lessdisks-cloner/version | sed s/updated=//g)
  # some version files don't have an updated line
  [ -z "$version" ] && version="unknown date"
  echo $version
}

requestModel() {
  while [ -z "$model" ] ; do
    # build up the available models list, look for the updated line in the
    # models version file to give the user a little bit of a hint about which
    # model to use.  of course, the name should be enough
    local -a availablemodels
    local i=0
    for m in $(find $clonepath -mindepth 1 -maxdepth 1 | \
                 egrep -v cloner-ld.config); do
      # build up the array to pass to the radioBox
      availablemodels[$((i++))]=$(basename $m)
      availablemodels[$((i++))]=$(readVersion $m)
      availablemodels[$((i++))]=off
    done

    # create a radio box for the user to choose models from.  one special 
    # model "create" is used to allow the user to create a brand new model
    if [ "create" = "$1" ]; then
      radioBox "Select Prototype" \
        "Choose one of the existing prototypes or 'create' to build a new prototype." \
        create "Create a new Prototype" on \
        "${availablemodels[@]}" || exit $?
    else
      radioBox "Select Prototype" \
        "Choose which prototype you would like to clone" \
        "${availablemodels[@]}" || exit $?
    fi
    model=$REPLY
  done
}

testRootDevice () {
    mount -t auto $rootdevice $TARGET
    # TODO more intelligent test for validity?
    if [ -r $TARGET/etc/fstab ]; then
      fstab="$TARGET/etc/fstab"
    else
      umount $TARGET
      msgBox "Invalid Root Device" \
        "root device invalid, or autodetected filesystem type may be incorrect"
      [ $? = 0 ] || exit $?
      rootdevice=
    fi
}

requestRootDevice() {
  # give the user a list of partions and ask them to provide us
  # with the root device
  # TODO if updatemodel="yes" determine root from fstab....
  rootdevice=
  while [ -z "$rootdevice" ]; do
    local -a devices
    local device
    local size
    i=0
    for f in $(sfdisk -l -uM | egrep "Linux" | egrep -v "swap" | \
               sed s/\*//g | awk '{print "device="$1";size="$4}'); do
      # i tried about ten different ways to make this work and this was
      # the only way that i could get devices populated and in the right
      # scope.  sometimes bash really cheeses me off.
      eval "$f"
      devices[$((i++))]=$device
      devices[$((i++))]=$(echo $size | tr -dc 0-9)
      devices[$((i++))]=off
    done
    radioBox "Choose a root partition" \
       "Choose a root partition from the following list." \
       "${devices[@]}" || exit $?
    [ -z "$REPLY" ] && continue
    rootdevice=$REPLY
    testRootDevice
  done
}

guessRootDevice () {
  # attempts to determine root device from fstab,
  # and failing that, asks the user with requestModel
  if [ ! -z "$fstab" ]; then
    rootdevice=$(awk '{print $1" "$2" "}' $fstab | egrep " / " | cut -d " " -f 1)
      if [ ! -z "$rootdevice" ]; then
        if [ "yes" = "$update" ] || [ "yes" = "$updatemodel" ]; then
          testRootDevice
        fi
      fi
  fi
  if [ -z "$rootdevice" ]; then
    requestRootDevice
  fi
}
