#! /bin/bash
# $Header: /global/src/usr.bin/src/RCS/mkscratchdir.sh,v 1.12 2020/06/23 21:12:23 mas90 Exp $

# *** BEWARE *** this command can be run as root by users, using sudo
# Take great care with user supplied info !

# Script to create a user directory in /local/scratch

# Note that if SUDO_USER isn't set, it will create and remove directories for
# other users.  This is a feature for use under really etc..
# Deletion will only work if the dir is empty, so no files will be lost.

# set a suitable PATH before running any commands
PATH=/bin:/usr/bin:/usr/sbin:/sbin:/usr/etc

USER="${SUDO_USER-${USER-$LOGNAME}}"
quotauser=defquota	# pseudo user just to hold "default quotas".
group=user
getent group $group > /dev/null || group="$USER"
noderef=	# -h ?

case "$USER" in
'')	 echo "$0: error: no USER found" 1>&2; exit 1;;
esac

case "`echo "$USER" | sed 's/[a-z]*[0-9]*//'`" in
?*)	echo "$0: error: unexpected USER '$USER' found" 1>&2; exit 1;;
esac

tr=gnutr; test ! -x /usr/bin/$tr -a -x /usr/bin/tr && tr=tr

remove=
if [ ! "$1" = "" ] ; then
  case "$1" in 
  -r) remove=true; shift;;
  esac
fi

vol=
if [ ! "$1" = "" ] ; then
  case "$1" in 
  [0-9]|1[0-9]) vol=-$1;;
  *) echo "Error. Argument must be one of scratch volume numbers 0-19." 1>&2
     exit
  esac
fi

scratch=/local/scratch$vol

case "`uname`" in
Linux|SunOS|OSF1) df_fldno=6 ;;
*)     echo "$0 doesn't work on `uname` systems. Mail sys-admin for help." 1>&2
       exit
       ;;
esac

if [ -d $scratch ] ; then
   mtpt=`df -k $scratch | grep -v '^File' | awk "BEGIN {FS=\" \"} {print \\$$df_fldno}"`
else
   echo "$0: error: $scratch doesn't exist." 1>&2
   echo "Please mail sys-admin for help." 1>&2
   exit
fi

if [ "$mtpt" = "/" ] ; then
  echo "$0: error: $scratch is on the / partition." 1>&2
  echo "Please mail sys-admin for help." 1>&2
  exit
fi

if [ ! "$UID" = "0" ] ; then
  echo "$0: error: this script needs to run as root.  Try 'sudo $0'." 1>&2
  exit
fi

# cd there to allow relative names throughout
cd $scratch || exit 1

case "$remove" in
?*)	if	test ! -r "$USER"
	then	echo "$0: directory $scratch/$USER does not exist" 1>&2
		exit 1
	fi
	if	test ! -d "$USER"
	then	echo "$0: directory $scratch/$USER is not a directory" 1>&2
		exit 1
	fi
	exec rmdir "$USER"
esac

# Does it already exist ?
if	test -r "$USER" 
then	echo "$0: directory $scratch/$USER already exists" 1>&2
	exit 1
fi

# Only allow lowercase letters and digits (does not enforce alpha then num)
case "`echo \"$USER\" | $tr -d a-z0-9`" in
'')	: OK;;
*)	echo "$0: invalid character in USER '$USER'" 1>&2; exit 1;;
esac

# Check user is in the password file
if	getent passwd $USER > /dev/null
then	: OK
else	echo "$0: user $USER not in password file"
	exit 1
fi

# trap any problem so that the directory is not left "by mistake"
trap 'rmdir "$USER"' EXIT

# Do it !
mkdir "$USER" || exit 1

# Wait til now to set the quota, to avoid re-setting it if dir already exists
if	getent passwd $quotauser /dev/null
then	if	VISUAL=true edquota -p $quotauser $USER
	then	: OK
	else	echo "Failed to set quota." 1>&2
	fi
fi

# chown it -- rmdir if it fails.
# Consider using the -h
if	chown $noderef "$USER" "$USER"
then	: OK
else	echo "$0: failed to chown $USER $USER" 1>&2
	rmdir "$USER"
	trap - EXIT
	exit 1
fi

if      chgrp $noderef "$group" "$USER" || chgrp $noderef "$USER" "$USER"
then    : OK
else    echo "$0: failed to chgrp $group / $USER" 1>&2
        rmdir "$USER"
        trap - EXIT
        exit 1
fi

trap - EXIT

# All done -- tell the user ...
cat << EOF

$0: Directory $scratch/$USER created

NOTE THAT THIS IS SCRATCH FILESPACE AND AS SUCH IT IS NOT ARCHIVED
AT ALL, so the contents may be lost at any time. *YOU* ARE RESPONSIBLE
FOR ENSURING BACKUPS ARE MADE OF ANY OF THE DATA IN THIS SPACE.

On some systems, the partition may contain system critical files as
well, so take care that it does not fill up -- use the command 

   df $scratch/

to check how much space is left.

EOF
