#!/bin/sh
# mkIzoFs. Modelled on the mke3fs script.

MNT=/tmp/.izomtpt.$$
PROG=`basename $0`

usage () {
#   echo "$PROG [-t <fstype>] -r <rootfset> [-l <fset>,...] [-D dir1 [-D dir2]] [mkfs-flags] device [blocks-count]"
   echo "$PROG [-t <fstype>] -r <rootfset> [mkfs-flags] device [block-count]"
   echo "   fstype        => type of cache filesystem: ext2/ext3/reiserfs"
   echo "     default: ext3 if mkfs.ext3/mke3fs is found, otherwise ext2"
   echo "   rootfset      => name of the root fileset"
#   echo "   fset,...      => list of one or more fileset names"
#   echo "                    mountpoints are specified relative to root   "
#   echo "   dir1,dir2     => directories to create within root fileset"
   echo "   mkfs-flags additional flags that mkfs.<fstype> accepts"
}

quit () {
    [ -d $MNT ] && rmdir $MNT
    MOUNTED=`mount | grep "on $MNT "`
    [ "$MOUNTED" ] && umount $MNT

    exit $1
}

[ $# -eq 0 ] && usage && exit 1

DIRLIST=""

while [ $# -gt 0 ]; do
    [ -w "$1" ] && DEV=$1 && break
    case $1 in
    -h|--help)    usage; exit 0;;
    -t)    FSTYPE=$2; shift;;
    -t*)    FSTYPE="`echo $1 | cut -c 3-`";;
    -r)    ROOTFSET=$2; shift;;
    -r*)    ROOTFSET="`echo $1 | cut -c 3-`";;
#    -l)    FSETLIST=$2; shift;;
#    -l*)    FSETLIST="`echo $1 | cut -c 3-`";;
#    -D)    DIRLIST="$DIRLIST:$2"; shift;;
#    -D*)    DIRLIST="$DIRLIST:`echo $1 | cut -c 3-`";;
    *)    OPT="$OPT $1";;
    esac
    shift
done

[ $# -gt 1 ] && SIZE=$2

if [ -z $FSTYPE ]; then
 # ext3 might be a module and not be loaded yet
 modprobe ext3 2>/dev/null
 if (grep ext3 /proc/filesystems 1>/dev/null 2>&1); then
  FSTYPE=ext3
  MKFSOPTS="-j"
  MKFSPROG="mke2fs -q"
 else
  FSTYPE=ext2
  MKFSPROG="mke2fs -q"
  MKFSOPTS=""
 fi
else
# ext3 still uses mke2fs with journalling options
 if [ "$FSTYPE" = "ext3" ]; then
  MKFSOPTS="-j"
  MKFSPROG="mke2fs -q"
 else
  MKFSPROG="mkfs.$FSTYPE -q"
 fi
fi

if [ -z $ROOTFSET ]; then
 echo "You must specify a fileset name with the \`-r' option." 1>&2
 exit 1
fi

EXCLUDE_GID=`sed -n -e "s/^intermezzo:x:\([0-9]*\):/\1/gp" /etc/group`
if [ -z $EXCLUDE_GID ]; then
    echo "The \`intermezzo' group does not exist; please create it in /etc/group"
    exit 1
fi

echo "Cache file system type is $FSTYPE"
echo "Root fileset name is $ROOTFSET"

echo
echo "Using $MKFSPROG to create cache file system"
$MKFSPROG $MKFSOPTS $OPT $DEV $SIZE || exit $?

mkdir -p $MNT || exit 101
[ -f $DEV ] && MNTOPT="-o loop" 
mount -t $FSTYPE $MNTOPT $DEV $MNT
if [ $? -ne 0 ]; then 
    echo "$PROG: $FSTYPE mount failure" 1>&2
else
    echo "Creating InterMezzo journal files"
    echo "Processing root file set $ROOTFSET"
    chgrp -R intermezzo $MNT/lost+found

    mkdir -p $MNT/.intermezzo/$ROOTFSET
    chmod -R go-rwx,g+s $MNT/.intermezzo/$ROOTFSET
    touch $MNT/.intermezzo/$ROOTFSET/kml
    touch $MNT/.intermezzo/$ROOTFSET/lml
    touch $MNT/.intermezzo/$ROOTFSET/last_rcvd
    chown -R intermezzo.intermezzo $MNT/.intermezzo
    ln -s ../.. $MNT/.intermezzo/$ROOTFSET/ROOT
    #chattr +i $MNT/.intermezzo/$ROOTFSET

#     if [ ! -z "$FSETLIST" ]; then
#         for f in `echo $FSETLIST|sed 's/,/ /g'`; do
#             echo "Processing file set $f"
#             mkdir -p $MNT/$f
#             chmod -R go-rwx,g+s $MNT/$f

#             touch $MNT/$f/kml
#             touch $MNT/$f/lml
#             touch $MNT/$f/last_rcvd
#             chown -R intermezzo.intermezzo $MNT/$f
#             mkdir $MNT/$f/ROOT
#             if [ "$FSTYPE" = "ext3" -o "$FSTYPE" = "ext2" ]; then
#                 chattr +i $MNT/$f
#             fi
#         done
#     fi
#    # FIXME: does this make any sense these days?
#    if [ ! -z "$DIRLIST" ]; then
#        for d in `echo $DIRLIST|sed 's/:/ /g'`; do
#            echo "creating directory $d"
#            mkdir -p $MNT/$d
#            if [ "$FSTYPE" = "ext3" -o "$FSTYPE" = "ext2" ]; then
#                chattr +u $MNT/$d
#            fi
#        done
#    fi
    umount $MNT
fi

rmdir $MNT
