#!/bin/sh USAGE="$0 [-v] [--paste] []" usage() { echo "$USAGE" echo "publish stdin (or pasteboard) to ipfs" exit 0 } die() { echo >&2 "error: $@" exit 1 } log() { if [ $verbose ]; then printf >&2 "$@" fi } # todo: make this cross-platform case $(uname) in Darwin) clicopy="pbcopy" clipaste="pbpaste" ;; Linux) if type xsel >/dev/null; then clicopy="xsel -i" clipaste="xsel -o" else if type xclip >/dev/null; then clicopy="xclip -i" clipaste="xclip -o" else die "xsel or xclip is needed on Linux" fi fi ;; *) ;; esac # get user options while [ $# -gt 0 ]; do # get options arg="$1" shift case "$arg" in -h|--help) usage ;; -v|--verbose) verbose=1 ;; --paste) paste=1 ;; --*) die "unrecognised option: '$arg'\n$USAGE" ;; *) if [ "$name" = "" ]; then name="$arg" else die "too many arguments\n$USAGE" fi ;; esac done if [ "$name" = "" ]; then name=paste fi if [ $paste ]; then test -n "$clipaste" || die "clipboard paste unkown for $(uname)" log "paste to ipfs..." date=$(date +"%Y-%m-%dT%H:%M:%SZ") fpath=$(mktemp "/tmp/paste.$date.XXXXXX.txt") || die "could not 'mktemp /tmp/paste.$date.XXXXXX.txt'" $clipaste >"$fpath" || die "could not paste using '$clipaste'" fhash=$(ipfs add -q <"$fpath") || die "could not 'ipfs add -q' content from '$fpath'" else log "stdin to ipfs..." fhash=$(cat | ipfs add -q) || die "could not 'ipfs add -q'" fi log " $fhash\n" log "constructing dir..." dir=$(ipfs object new unixfs-dir) || die "could not 'ipfs object new unixfs-dir'" dir=$(ipfs object patch "$dir" add-link "$name" "$fhash") || die "could not 'ipfs object patch $dir add-link $name $fhash'" pin=$(ipfs pin add -r "$dir") || die "could not 'ipfs pin add -r $dir'" log " $dir\n" log "copying url to clipboard..." out="https://ipfs.io/ipfs/$dir/$name" echo "$out" | $clicopy || die "could not copy using '$clicopy'" log " copied\n" echo "$out" log "preloading on the gateways..." (curl "$out" >/dev/null 2>&1 && log " ok\n") || log " n/a\n"