#!/bin/bash
# mountavfs -- program to mount avfs file system
# and load avfsd daemon.
# companion program to umountavfs
# will check to see if avfs is mounted and then
# load the avfsd daemon which will mount avfs.
#
# you can source this file in the login script to automatically make
# AVFS available. The base directory mount point is exported as
# AVFSBASE.
#

# if unset or not pointing to a directory, revert to default mount point
if [ ! -d "$AVFSBASE" ]; then
    AVFSBASE="${HOME}/.avfs"
fi

grep -qE "avfsd ${AVFSBASE}" /proc/mounts || {
   if [ ! -e "$AVFSBASE" ]; then
      mkdir -p "$AVFSBASE"
   fi
   if [ ! -d "$AVFSBASE" ]; then
      echo "$AVFSBASE exists but is no directory"
      exit 1
   fi
   echo Mounting AVFS on $AVFSBASE...
   avfsd "$AVFSBASE"
   while test ! -e "$AVFSBASE/#avfsstat/symlink_rewrite"
   do sleep 0.5 ; done
   echo "1" >| "$AVFSBASE/#avfsstat/symlink_rewrite"

   export AVFSBASE
}
