#!/bin/sh
#
# ncsend v0.1 - quickly send someone a file using just netcat
#
# works if you have a public IP. Otherwise you need a port forward and other
# mess and this suddenly becomes much less handy. Or just use IPv6.
#
# Copyright 2010 Wilmer van der Gaast <wilmer@gaast.net> GPL2
#

[ -z "$MY_IP" ]   && MY_IP=$(ip -o ro get 8.8.8.8 | sed -e 's/.*src \([^ ]*\) .*/\1/')
[ -z "$MY_PORT" ] && MY_PORT=6886

checkrel()
{
	while [ -n "$1" ]; do
		[ "${1:0:1}" = "/" ] && return 1
		shift 1
	done
	return 0
}

if [ -z "$1" ]; then
	echo "Usage: $0 [file] OR $0 [files/dirs]"
	echo "Will use tar if sending multiple files/one or more directories"
elif [ -z "$2" -a \! -d "$1" ]; then
	quoted=$(echo "$(basename "$1")" | sed -e 's/[^-a-z0-9._]/\\&/gi')
	echo "nc $MY_IP $MY_PORT | pv > $quoted"
	pv "$1" | nc -q 0 -lp $MY_PORT
else
	if ! checkrel "$@"; then
		echo 'Please only use relative paths' >&2
		exit 1
	fi
	echo "nc $MY_IP $MY_PORT | tar xzvv"
	tar cvv "$@" | gzip -9 | nc -q 0 -lp $MY_PORT
fi
