#!/bin/sh
# mkramdisk [-t type] [-d device] mountpoint [archive]

dev=/dev/ram
type=minix

while [ "$1" != "" ]; do
 case "$1" in
 -t)
   shift
   if [ "$1" != "" ]; then
    type=$1 ; shift
   fi
   ;;
 -d)
   shift
   if [ "$1" != "" ]; then
    dev=$1 ; shift
   fi
   ;;
 *)
   break
   ;;
 esac
done

if [ "$1" = "" ]; then
  echo "Usage: $0 [-d device] [-t filesystemtype] mountpoint [archive]"
  echo "       create a ramdisk of filesystemtype and mount it on mountpoint"
  echo "       and optional unpack a .tar.gz archive into the ramdisk"
  exit -1
fi

mnt=$1 ; shift

arc=$1

if [ ! -b "$dev" ] ; then
 echo "\"$dev\" is not a block special device."
 exit 1
fi

if [ ! -d "$mnt" ] ; then
 if [ -e "$mnt" ] ; then
   echo Mount Point "$mnt" exists and is not a directory
   echo Giving up
   exit 1
 fi
 echo "Mount point $mnt does not exist."
 echo -n "Create directory \"$mnt\" ? [y/n] "
 read a
 if [ "$a" != "y" ] ; then
  echo "Cannot continue."
  exit 1
 fi
 mkdir $mnt
 if [ $? != 0 ] ; then 
  echo "Could not create \"$mnt\""
  exit 1
 fi
fi
ls -ld $mnt
echo -n ok to "\"mkfs -t $type $dev\" ? [y/n] "
read a
if [ "$a" != "y" ]; then
 echo giving up.
 exit 1
fi
mkfs -t $type $dev
if [ "$?" != "0" ] ; then
 echo \"mkfs\" failed.
 exit 1
fi
echo "doing \"mount $dev $mnt\" ....."
mount $dev $mnt
if [ "$?" != "0" ]; then
 echo \"mount\" failed.
 exit 1
fi

if [ "$arc" = "" ]; then exit 0 ; fi

echo going to unpack the gzipped tar archive \"$arc\"  .....
tar xfzv $arc -C $mnt
exit $?


