#!/bin/sh
#$Id: netbsl,v 1.2 2005/09/16 17:38:43 cssharp Exp $

# @author Cory Sharp <cssharp@eecs.berkeley.edu>

[ x"$1" = x ] && echo -e "usage: netbsl [host:port] [file|-|.] [args...]\na filename of . means no file" && exit 0

HOSTPORT="$1"
shift

: ${NETBSL_HOSTPORT_PREFIX:=192.168.1.}
if [ x"${HOSTPORT%:*}" = x"$HOSTPORT" ]
then
  POSTFIX=`perl -e '$_=$ARGV[0]; s/(\d+)\.(\d+)/"$1:".($2+10000)/e; print;' "$HOSTPORT"`
  HOSTPORT="$NETBSL_HOSTPORT_PREFIX$POSTFIX"
fi

HOST=${HOSTPORT%:*}
PORT=${HOSTPORT##*:}

FILE="$1"
shift

if [ x"$FILE" = x. ]
then
  echo msp430-bsl "$@" | /usr/bin/nc $HOST $PORT
else
  (echo msp430-bsl "$@"; sed 's/\015//g' "$FILE"; echo) | /usr/bin/nc $HOST $PORT
fi

