#!/bin/sh
#
# NAME
#       T - make temporaryfiles
#
# SYNOPSIS
#       T [-#] [filename]
#
# DESCRIPTION
#       T can be used in the following ways:
#       
#       To save data to a temporary file:
#           If placed last in a pipe T will take standard output from a 
#           command and save it in a temporary file. 
#       Equivalent to: cat >tempfile
#       Example:
#           cat file | T
#       
#       To type an existing temporary file:
#           If placed first in a pipe T will type the last temporary 
#           file's contents to standard output.
#       Equivalent to: cat tempfile
#       Example:
#           T | cat 
#
#       To save to and type a temporary file:
#           If placed in the middle of a pipe T will copy standard output 
#           from a command and save it in a temporary file. 
#       Equivalent to: tee tempfile
#       Example:
#           cat | T | cat 
#
#       If given a filename T saves info to that file in the .tmp-directory
#       instead of tempfile
#
#       If given option -# which # is a number T will use the #'th last 
#       version.
#
# FILES
#       ~/.tmp/tempfile
#	or
#	~/.tmp/[filename]         - the most recent temporary file created
#       ~/.tmp/[filename]-YYYY-MM-DD.hh:mm:ss-pid
#                                 - the temporary file created at YYYY/MM/DD
#                                   hh:mm:ss with process id pid
#
# BUGS
#       You will have to clean up your .tmp-dir once in a while
#
# AUTHOR
#       Ole Tange (ole@tange.dk)
#	Version: 1996-JAN-18, 1996-JUL-24, 2007-JUN-11, 2007-NOV-08
#
# LICENSE
#       GPL
#
# SEE ALSO
#       cat(1), tee(1)

T_DIR=$HOME/.tmp

#DEBUG_LOG=/tmp/T-debug
DEBUG_LOG=/dev/null

if echo $1 | egrep '^-[0-9]+$' >/dev/null; then
  T_BACK_NO=$(echo $1 | sed -e 's:-::')
  shift
  if [ -z "$1" ]; then
    # No filename given. Default = tempfile
    T_NO_SLASH=tempfile
  else
    # Map / to __ to avoid creating dirs in ~/.tmp
    T_NO_SLASH=$(echo $1 | sed -e 's:/:__:g')
  fi
  T_TEMPFIL=$T_DIR/$T_NO_SLASH
  T_BACK_FILENAME=$(find $T_DIR -name "$T_NO_SLASH"-'[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9].[0-9][0-9]:[0-9][0-9]:[0-9][0-9]-[0-9]*' \
  | sort | tail -n $T_BACK_NO | head -n 1)
  if [ -z "$T_BACK_FILENAME" ]; then
    T_TEMPFIL=$T_DIR/$T_NO_SLASH
  else
    T_TEMPFIL=$T_BACK_FILENAME
  fi
else
  if [ -z "$1" ]; then
    # No filename given. Default = tempfile
    T_TEMPFIL=$T_DIR/tempfile
  else
    # Map / to __ to avoid creating dirs in ~/.tmp
    T_NO_SLASH=$(echo $1 | sed -e 's:/:__:g')
    T_TEMPFIL=$T_DIR/$T_NO_SLASH
  fi
fi


T_NY_FIL="$T_TEMPFIL"-`date +"%Y-%m-%d.%H:%M:%S"`-$$

mkdir $T_DIR 2>/dev/null

if tty -s ; then
  # STDIN is terminal
  # Don't care what STDOUT is
  # T | cat         => T= cat file
  # T               => T= cat file
  echo cat "$T_TEMPFIL" >> $DEBUG_LOG
  cat "$T_TEMPFIL"
else
  # STDIN redir'ed

  if [ -t 1 ] ; then
    # STDOUT = terminal
    # cat | T       => T= >file
    echo cat '>' "$T_NY_FIL" >> $DEBUG_LOG
    cat > "$T_NY_FIL"
  else
    # STDOUT redir'ed
    # cat | T | cat => T= tee file
    echo tee "$T_NY_FIL" >> $DEBUG_LOG
    tee "$T_NY_FIL"
  fi

  # Remove hardlink to most recent file
  rm "$T_TEMPFIL" 2> /dev/null

  # make a hardlink to the most recent file
  ln "$T_NY_FIL" "$T_TEMPFIL"
fi
