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

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

usage () {
   echo "$PROG [-t <fstype>] -r <rootfset> [-l <fset>=<mtpt>,...] [mkfs-flags] device [blocks-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 "   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

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-`";;
    *)    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 "Group InterMezzo do 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 for creating cache Filesystem"
$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"
    mkdir -p $MNT/.intermezzo/$ROOTFSET/db
    touch $MNT/.intermezzo/$ROOTFSET/kml
    touch $MNT/.intermezzo/$ROOTFSET/lml
    touch $MNT/.intermezzo/$ROOTFSET/last_rcvd
    chgrp -R InterMezzo $MNT/.intermezzo
    chgrp -R InterMezzo $MNT/lost+found
    chmod -R go-rwx,g+s $MNT/.intermezzo
    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/.intermezzo/$f/db
            touch $MNT/.intermezzo/$f/kml
            touch $MNT/.intermezzo/$f/lml
            touch $MNT/.intermezzo/$f/last_rcvd
            chgrp -R InterMezzo $MNT/.intermezzo/$f
            chmod -R go-rwx,g+s $MNT/.intermezzo/$f
            chattr +i $MNT/.intermezzo/$f
        done
    fi
    umount $MNT
fi

rmdir $MNT
