#!/bin/bash

# this script is called by the lessdisks-cloner script to
# generate an fstab based on the values in the $schemefile, 
# possibly named diskschemes.

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

if [ -z "$schemefile" ]
then
  schemefile="/etc/lessdisks/diskschemes"
fi  

if [ -e "$schemefile" ]
then
  echo "setting default schemefile to $schemefile"
else
  echo "schemefile $schemefile not found!"
  echo "can't do much without it..."
  echo "exiting..."
  exit 1
fi


GetDisks() {
  # TODO need to ignore CDROMs and get real disks only...
  # TODO real scsi disk support
  # TODO use /proc/ide/hd*
  #dmesg="cat /var/log/dmesg"
  dmesg=dmesg
  alldisks=$($dmesg | egrep "^hd|^sd" | egrep CHS | cut -d : -f 1 | sort -u)
  if [ -z "$alldisks" ]
  then
    echo "no disks found... exiting..."
    exit 1
  fi
}

MemSizeMultiply() {
  memsize=$(free -m | egrep ^Mem | awk '{print $2}')
  multiplier=$(echo $size | sed s/mem//g)
  size=$(($memsize*$multiplier))
}

AddToFstab() {
  local disk=$3
  if [ "" != "$disk" ]
  then
    local min=$1
    local max=$2
    shift 3
    if [ "remaining" = "$max" ]
    then
      size="remaining"
    else
      size="$min"
    fi
    if [ "" != "$(echo $size | egrep mem)" ]
    then
      MemSizeMultiply
    fi
    # TODO determine if size will be handled by this script or the
    # partitioning script
    # TODO actually use $min and $max values...
    #echo "/dev/$disk $@ #min=$min max=$max" >> $fstab
    #echo "/dev/$disk $@ #min=$min max=$max"
    echo "/dev/$disk $@ #size=$size" >> $fstab
    #echo "/dev/$disk $@ #size=$size"

  fi
}

GenerateFstab() {
  i=0
  # grubdisks are called that because they use similar zero-indexing to grub
  for disk in $alldisks
  do
    disksize=`dmesg | egrep $disk | egrep CHS | awk '{print $4}' | cut -d "(" -f 2`
    #declare -a disk"$i"=(/dev/$disk $disksize)
    partitions=$(egrep ^disk"$i" $schemefile | awk '{print $1}' | cut -d _ -f 2)
    for p in $partitions
    do
      mount=$(egrep ^disk"$i"_"$p" $schemefile | awk '{print $2}')
      fstype=$(egrep ^disk"$i"_"$p" $schemefile | awk '{print $3}')
      type=$(egrep ^disk"$i"_"$p" $schemefile | awk '{print $4}')
      min=$(egrep ^disk"$i"_"$p" $schemefile | awk '{print $5}')
      max=$(egrep ^disk"$i"_"$p" $schemefile | awk '{print $6}')

      case $type in
        swap) options="sw 0 0";;
        root)  options="defaults,errors=remount-ro 0 1";;
        normal|boot) options="defaults 0 2";;
        empty|compaq) options="defaults,noauto 0 0";;
      esac

      AddToFstab $min $max $disk$((p+1)) $mount $fstype $options
    done
    i=$(($i+1))
  done
  NonDiskEntries
}

NonDiskEntries() {
  for nondisk in floppy0 cdrom0 proc
  do
    mount=$(egrep ^"$nondisk" $schemefile | awk '{print $2}')
    fstype=$(egrep ^"$nondisk" $schemefile | awk '{print $3}')
    type=$(egrep ^"$nondisk" $schemefile | awk '{print $4}')
    case $type in
      floppy) options="defaults,user,noauto 0 0"
            device="/dev/fd0" ;;
      proc) options="defaults 0 0"
            device="proc" ;;
      cdrom) options="defaults,ro,user,noauto 0 0"
            device="/dev/cdrom" ;;
    esac
    echo "$device $mount $fstype $options" >> $fstab
    #echo "$device $mount $fstype $options"
  done
}

GetDisks

if [ "yes" = "$fstab_interactive" ]
then
  fstab_interactive_script=/usr/share/lessdisks/fstab-interactive
  if [ -r "$fstab_interactive_script" ]
  then
    . $fstab_interactive_script
  fi
fi

fstab=/tmp/fstab
# if $fstab exits, remove it
[ -e $fstab ] && rm $fstab

GenerateFstab
