#!/bin/bash

# A nifty little tool by Magnus Lie Hetland

# XXX Non-ASCII characters mightn't work yet (depends on encoding)
# XXX %-coded files don't work yet
# XXX In case of error, the program should really exit...
# XXX In some cases, apostrophes cause errors (even when quoted)

# $Revision: 10.10 $
# $Date: 2006/12/25 20:55:08 $

# To make a file publicly available (temporarily), simply use

# $ mpub <file>

# Directories are automatically turned into archives using zip. The
# public Web directory can be configured to lie on a remote server.

# Possible features (subject to public demand):

# - Multiple files (as directory with index.html)
# - Verbose switch (for zip, mostly)

# Small fixes:

# - Create zip archives in /tmp (or the like) to avoid the need to add
#   numbering when a local name collision occurs (in the source
#   directory)
# - Start numbering with 1 instead of $$

# -- Customize here --------------------------------------------------

# The Web server where used in the URLs. Defaults to www.your.domain
WEBHOST=""

# The server where your Web directory lies. Defaults to localhost. If
# this is set, scp and ssh will be used for the file handling. For the
# most convenient use, you should set up key authentication, so you
# don't have to specify passwords all over the place.
FILEHOST=""

# The username you use at $FILEHOST. Defaults to your local username.
FILEUSER=""

# The absolute path to the directory where you keep your public HTML
# files. Defaults to ${HOME}/public_html (works both locally and
# remotely).
DIR=""

# The prefix used in the public file names
PUB_PREFIX="tmp"

# -- No need to edit below here --------------------------------------

if [ -z "$WEBHOST" ]
then
  WEBHOST=www.${HOSTNAME#*.}
fi

if [ -z "$FILEUSER" ]
then
  FILEUSER=$(whoami)
fi

if [ -z "$DIR" ]
then
    if [ -z "$FILEHOST" ]
    then
        DIR="${HOME}/public_html"
    else
        DIR=$(ssh $FILEUSER@$FILEHOST "echo \$HOME")/public_html
    fi
fi

if [ -z "$1" ]
then
  echo "Usage: mpub [-t time-spec] <file>"
  exit
fi

TSPEC=""
if [ "$1" = "-t" ]
then
  shift
  TSPEC=$1
  shift
fi

SRC="$1"
ARCH=""
if [ -d "$SRC" ]
then
    ARCH=${SRC%/}.zip
    NUM=$(($$-1))
    while [ -e $ARCH ]
    do
      NUM=$((NUM+1))
      ARCH=$SRC$NUM.zip
    done
    ZIPOPT=""
    if [ $(uname) = "Darwin" ]
    then
      ZIPOPT="-df"
    fi
    zip -r $ARCH $SRC > /dev/null
    SRC=$ARCH
fi

FULLDESTNAME=${SRC// /+}
DESTNAME=$(basename "$FULLDESTNAME")
BASE=$DESTNAME
NUM=$(($$-1))
if [ -z "$FILEHOST" ]
then
    while [ -e "$DIR/$DESTNAME" ]
    do 
        NUM=$((NUM+1))
        DESTNAME=$PUB_PREFIX$NUM$BASE
    done
else
    # Network version of collision prevention:
    DESTNAME=$(
    ssh $FILEUSER@$FILEHOST "
    # Carry over local vars:
    DESTNAME=$DESTNAME
    NUM=$NUM
    
    while [ -e \"$DIR/\$DESTNAME\" ]
    do
        NUM=\$((NUM+1))
        DESTNAME=$PUB_PREFIX\${NUM}$BASE
    done
    echo \$DESTNAME")
fi

DEST=$DIR/$DESTNAME

if [ -z "$FILEHOST" ]
then
  cp "$SRC" "$DEST"
  chmod a+r "$DEST"
else
  #if [ "$DEFAULTDIR" = true ]
  #then
  #  DEST="~/public_html/$DESTNAME"
  #fi
  scp "$SRC" "$FILEUSER@$FILEHOST:$DEST"
  ssh "$FILEUSER@$FILEHOST" "chmod a+r $DEST"
fi

if [ ! -z $ARCH ]
then
    echo "Directory $1 made public as zip archive at:"
else
    echo "File $1 made public at:"
fi
URL="http://$WEBHOST/~$USER/$DESTNAME"
echo $URL

if [ `uname` = "Darwin" ]
then
    echo -n $URL | pbcopy
    echo -n "URL copied to pasteboard. "
fi

WAITFORIT=""
if [ -z "$TSPEC" ]
then
  WAITFORIT=true
else
  ATCMD="echo \"rm -f $DEST\" | at $TSPEC > /dev/null 2>&1"
  if [ -z "$FILEHOST" ]
  then
    eval $ATCMD &&
    echo "It will be removed at $TSPEC" ||
    (echo "Error in time specification: $TSPEC" &&
    WAITFORIT=true)
  else
    ssh $FILEUSER@$FILEHOST "$ATCMD" &&
    echo "It will be removed at $TSPEC" ||
    (echo "Error in time specification: $TSPEC" &&
    WAITFORIT=true)
  fi
fi

if [ ! -z "$WAITFORIT" ]
then
  echo "Press <Enter> to remove file"
  read
  if [ -z "$FILEHOST" ]
  then
    rm -f $DEST
  else
    ssh "$FILEUSER@$FILEHOST" "rm -f $DEST"
  fi
fi

# ARCH is either a created archive or ""
rm -f $ARCH
