# assorted functions common to cron jobs

cronloop_mk_TMPDIR () {
  local umask=$(umask)
  local tmp=/tmp/cron.$$.$RANDOM
  # how can we get it less predictable?  (DoS-Attack!)
  # without too much bloat and external proggies...
  if [ -x /usr/bin/mcookie ]; then
    tmp=/tmp/cron.$$.$(/usr/bin/mcookie)
  fi
  
  umask 077
  if ! mkdir $tmp; then
    # is not fooled by symlinks
    echo "Error creating uniq temporary directory. Aborting..." 1>&2;
    # should we warn about a possible attack here?
    exit 1
  fi
  umask $umask

  echo $tmp
}

cronloop_rm_TMPDIR () {
  
  case $TMPDIR in
   /tmp/cron.$$.*)
    ;;
   *)
    echo "Invalid TMPDIR='$TMPDIR'. Aborting..." 1>&2
    # should we warn about a possible attack here?
    exit 1
    ;;
  esac

  if [ ! -d $TMPDIR ]; then
    echo "No such directory: '$TMPDIR'. Aborting..." 1>&2
    # should we warn about a possible attack here?
    exit 1
  fi

  #echo rm -rf $TMPDIR 1>&2
  if ! rm -rf $TMPDIR; then
    echo "Trouble ahead..." 1>&2
    # should we warn about a possible attack here?
    exit 1
  fi
}

cronloop_signal () {
  echo "catched signal..." 1>&2
  exit 1
}
