# This file must be installed in /etc/profile.d/ in order to automatically enable 
# the user to use the function "usage" after its login. 

# This function choose a non existing filename in /tmp, create it and print its name 
# on stdout. If given, the first argument will be used as a prefix of the filename.
# The other (optional) arguments will be used as seed for generate a random suffix.
# Need the programs "sum" and "cut".
function filealloc { 
  local PREFIX BASE FILENAME
  PREFIX=${1:-filealloc}
  BASE=/tmp/$PREFIX
  FILENAME=$BASE.$(echo "$PREFIX$@$RANDOM" | sum | cut -d" " -f1);
  if [[ -f $FILENAME ]]; 
  then 
   filealloc "$PREFIX" "$@$RANDOM"; 
  else
   >$FILENAME 
   echo $FILENAME; 
  fi
}


# Wrapper for the program synopsis. 
# This function simplify the use of the program "synopsis" with the 
# bash internal command "source". 
# ATTENTION version lisant sur l'éntree standard
function match {
 local CODE=1 
 local TMPFILE=$(filealloc match "$@") 
 xargs synopsis >$TMPFILE -n "$0" parse && source $TMPFILE         
 CODE=$? 
 rm -f $TMPFILE
 return $CODE 
}

# Wrapper for the program synopsis. 
# This function simplify the use of the program "synopsis" with the 
# bash internal command "source". 
function usage {
 local CODE=1 
 local TMPFILE=$(filealloc match "$@") 
 synopsis >$TMPFILE -n "$0" "$@" && source $TMPFILE         
 CODE=$? 
 rm -f $TMPFILE
 return $CODE 
}

export -f filealloc
export -f match
export -f usage
