#!/bin/sh

#       NAME
#               mktemp - Create a unique filename.
#
#       SYNOPSIS
#               mktemp [ -c ] [ -d directory_name ] [ -p the_prefix ]
#
#       DESCRIPTION
#               The mktemp command makes a name for the pathname of a temporaryp
#               returns the string "/tmp/username.DDHHMMSSP...P" where username
#               is the user's username, DD is the day of the month, HH is the
#               hour of the day (locally), MM is the minute of the hour, SS is
#               the second of the minute, and P...P is the process id of the
#               user's shell.
#
#       OPTIONS
#
#       -c
#               Create an empty file whose filename is the generated name.
#
#       -d directory_name
#               Force directory_name to be used as the directory portion of
#               the pathname.  If this option is absent, /tmp is used by
#               default.
#
#       -p the_prefix
#               Use string the_prefix as the filename's prefix.  If this option
#               is absent, the user's username is used by default.
#
#       AUTHOR
#               Tyler Perkins                   perkins@colorado.edu
#               Boulder, CO

dir="/tmp"
prfx="$USER"
create=false

while [ "$1" != "" ]
do      case $1 in
        -d)     shift
                dir="`echo $1 | sed 's!\(.*\)/$!\1!'`"
                shift
                continue
                ;;
        -p)     shift
                prfx=$1
                shift
                continue
                ;;
        -c)     shift
                create=true
                continue
                ;;
        *)      echo 'mktemp usage: mktemp [-d directory] [-p prefix] [-c]'
                exit 1
                ;;
        esac
done

name="$dir/$prfx.`date | sed 's/[^0-9]//g
                              s/\(.*\)..../\1/'`$$"

# If file with name $name already exists, recurse until not.
if [ -f $name  -o  -d $name ]
then    name="`mktemp`"
fi

if $create
then    >$name
else    echo $name
fi

