#
# file_image_fdisk IMAGE_FILE
#
file_image_fdisk () {
  #
  local FNAME="$1"
  local FSIZE=0
  local CYLINDERS=0
  local HEADS=0
  local SECTORS=63
  #
  if   [ -z "$FNAME" ]
  then
      echo "[$0] No file name."
      return 1
  elif [ ! -r "$FNAME" ]
  then
      echo "[$0] Cannot read file \"$FNAME\"."
      return 1
  fi
  #
  # Get size
  #
  FSIZE=`du -k "$FNAME" | sed "s/[[:space:]].*$//"`
  #
  # Set geometry
  #
  if [ "$FSIZE" -le "516096" ]
  then
      HEADS="16"
  else
      HEADS="255"
  fi
  #
  CYLINDERS=$(($FSIZE*2/$SECTORS/$HEADS))
  #
  # Run fdisk.
  #
  fdisk -C $CYLINDERS -H $HEADS -S $SECTORS $FNAME
}
#
# file_image_partition_start IMAGE_FILE PART_NUMBER
#
file_image_partition_start () {
  #
  local FNAME="$1"
  local PART_NUMBER="$2"
  local PART_START=0
  #
  # Get partition start
  #
  PART_START=`sfdisk -d $FNAME \
    | grep -F "$FNAME$PART_NUMBER" \
    | sed "s/^.*start=[[:space:]]*//" | sed "s/,.*$//"`
  #
  echo "$PART_START"
}
#
# file_image_partition_size IMAGE_FILE PART_NUMBER
#
file_image_partition_size () {
  #
  local FNAME="$1"
  local PART_NUMBER="$2"
  local PART_SIZE=0
  #
  # Get partition start
  #
  PART_SIZE=`sfdisk -d $FNAME \
    | grep -F "$FNAME$PART_NUMBER" \
    | sed "s/^.*size=[[:space:]]*//" | sed "s/,.*$//"`
  #
  echo "$PART_SIZE"
}
#
# file_image_partition_id IMAGE_FILE PART_NUMBER
#
file_image_partition_id () {
  #
  local FNAME="$1"
  local PART_NUMBER="$2"
  local PART_ID=0
  #
  # Get partition start
  #
  PART_ID=`sfdisk -d $FNAME \
   | grep -F "$FNAME$PART_NUMBER" \
   | sed "s/^.*Id=[[:space:]]*//" | sed "s/,.*$//"`
  #
  echo "$PART_ID"
}
#
# file_image_partition_format IMAGE_FILE PART_NUMBER \
#                             [dos|minix]
#
file_image_partition_format () {
  #
  local FNAME="$1"
  local PART_NUMBER="$2"
  local PART_FORMAT="$3"
  local PART_START=`file_image_partition_start \
                    $FNAME $PART_NUMBER`
  local PART_SIZE=`file_image_partition_size $FNAME \
                   $PART_NUMBER`
  local PART_ID=`file_image_partition_id $FNAME \
                 $PART_NUMBER`
  #
  #
  #
  if [ "$PART_SIZE" -eq "0" ]
  then
      exit
  fi
  #
  # Get partition into a file.
  #
  dd if="$FNAME" \
     of="$FNAME.part$PART_NUMBER.tmp" \
     bs=512 \
     skip="$PART_START" \
     count="$PART_SIZE"
  #
  # Format.
  #
  if [ "$PART_FORMAT" = "dos" ] \
    || [ "$PART_FORMAT" = "msdos" ]
  then
      mkfs.msdos -v "$FNAME.part$PART_NUMBER.tmp"
  else
      mkfs.minix -n 14 "$FNAME.part$PART_NUMBER.tmp"
  fi
  #
  # Put formatted partition into the file.
  #
  dd if="$FNAME.part$PART_NUMBER.tmp" \
     of="$FNAME" \
     bs=512 \
     seek="$PART_START" \
     count="$PART_SIZE" \
     conv=notrunc
  #
  # Remove temporary file.
  #
  rm "$FNAME.part$PART_NUMBER.tmp"
  #
}
#
# file_image_partition_mount IMAGE_FILE PART_NUMBER
#
file_image_partition_mount () {
  #
  local FNAME="$1"
  local PART_NUMBER="$2"
  local PART_START=`file_image_partition_start \
                    $FNAME $PART_NUMBER`
  local PART_SIZE=`file_image_partition_size \
                   $FNAME $PART_NUMBER`
  local PART_ID=`file_image_partition_id \
                 $FNAME $PART_NUMBER`
  local MOUNT_POINT
  #
  #
  #
  if [ "$PART_SIZE" -eq "0" ]
  then
      exit
  fi
  #
  # Find mount point.
  #
  MOUNT_POINT=`basename $FNAME`
  MOUNT_POINT="/mnt/${MOUNT_POINT}.$PART_NUMBER"
  #
  #
  #
  sync
  #
  umount "$MOUNT_POINT" 2> "/dev/null"
  umount "$MOUNT_POINT" 2> "/dev/null"
  umount "$MOUNT_POINT" 2> "/dev/null"
  #
  mkdir -p "$MOUNT_POINT" 2> "/dev/null"
  #
  # Get partition into a file.
  #
  dd if="$FNAME" \
     of="$FNAME.part$PART_NUMBER.tmp" \
     bs=512 \
     skip="$PART_START" \
     count="$PART_SIZE"
  #
  # Check partition.
  #
  fsck -f -v -r "$FNAME.part$PART_NUMBER.tmp"
  #
  # Put formatted partition into the file.
  #
  dd if="$FNAME.part$PART_NUMBER.tmp" \
     of="$FNAME" \
     bs=512 \
     seek="$PART_START" \
     count="$PART_SIZE" \
     conv=notrunc
  #
  # Remove temporary file.
  #
  rm "$FNAME.part$PART_NUMBER.tmp"
  #
  # Mount the partition.
  #
  mount -o loop,offset=$(($PART_START*512)) \
        -t auto "$FNAME" "$MOUNT_POINT"
  #
}
#
# file_image_partition_syslinux IMAGE_FILE PART_NUMBER
#
file_image_partition_syslinux () {
  #
  local FNAME="$1"
  local PART_NUMBER="$2"
  local PART_START=`file_image_partition_start \
                    $FNAME $PART_NUMBER`
  local PART_SIZE=`file_image_partition_size \
                   $FNAME $PART_NUMBER`
  local PART_ID=`file_image_partition_id \
                 $FNAME $PART_NUMBER`
  #
  #
  #
  if [ "$PART_SIZE" -eq "0" ]
  then
      exit
  fi
  #
  # Get partition into a file.
  #
  dd if="$FNAME" \
     of="$FNAME.part$PART_NUMBER.tmp" \
     bs=512 \
     skip="$PART_START" \
     count="$PART_SIZE"
  #
  # Syslinux
  #
  syslinux "$FNAME.part$PART_NUMBER.tmp"
  #
  # Put altered partition into the file.
  #
  dd if="$FNAME.part$PART_NUMBER.tmp" \
     of="$FNAME" \
     bs=512 \
     seek="$PART_START" \
     count="$PART_SIZE" \
     conv=notrunc
  #
  # Remove temporary file.
  #
  rm "$FNAME.part$PART_NUMBER.tmp"
  #
  # Fix MBR
  #
  install-mbr $FNAME
}

