#!/bin/sh

#####
## Tries a simple mkdir, but if that fails, sets AFS permissions & tries again!
## $Id: fancy-mkdir,v 1.2 2001/09/27 12:29:01 dpelleg Exp $
#####

noafs_wrapup () {
  # nothing, yet
    dummy=0          # just to keep bash happy about an empty func
}

wrapup () {
    ### finally, give everybody write permission in the new object dir
    ## The arla AFS client doesn't like multiple user:perm pairs on the command line
    if [ -d /afs ] ; then
        fs sa $objdir jab:moore.lab all
        fs sa system:campushost read
    fi
}

objdir=$1
#set user=`whoami`

echo Running: .g/fancy-mkdir $objdir

if [ -d $objdir ]; then
    echo "Hey, directory $objdir already exists!"
    wrapup
    exit
fi

mkdir $objdir && echo "Created object directory $objdir"

if [ ! -d /afs ] ; then
    echo "I don't think this machine has AFS, so I'm done here"
    noafs_wrapup
fi

### if mkdir failed, try giving user temporary write permission
if [ $? -gt 0 ]; then
	fs sa . $USER write
	mkdir $objdir && echo "Created object directory $objdir (the second time's the charm!)"
	fs sa . $USER none
fi
