GNU nano 2.5.3                        File: ipfs-publish.sh                                                      

#!/bin/bash
source ~/.profile
if [ "$#" -ne 1 ]; then
  echo "usage:"
  echo "  ipfs-publish.sh <dir>"
  echo "  ipfs-publish.sh -- (read .tar.bzip2 archive from stdin)"
  echo "    example: tar -cjvf - dist | ./ipfs-publish.sh --"
  exit 1
fi
DIR=$1

echo "ipfs-publish.sh $DIR"
if [ "$DIR" = "--" ]; then
  echo "archive from stdin"
  TMPDIR=$(mktemp -d)
  cd "$TMPDIR"
  if ! tar -xjf /dev/stdin ; then
    echo "unable to decompress archive from stdin in gzip format"
    exit 5
  fi
  trap "{ rm -rf $TMPDIR; }" EXIT
  DIR="$TMPDIR"
fi

if [ -d "$DIR" ]; then
  echo "adding directory to ipfs recursively. please be patient..."
  CMD="ipfs add -r -Q $DIR"
  if ! HASH=`$CMD`; then
    echo "error executing $CMD. Is the daemon running?"
    exit 3
  fi
  echo "completed. Hash is $HASH"

  CMD="ipfs pin add -r /ipfs/$HASH"
  if ! $CMD ; then
    echo "error executing $CMD."
    exit 4
  fi
else
  echo "$DIR is not a directory"
  exit 2
fi
echo "<hash>$HASH</hash>"
exit 0