#!/bin/sh
PID=$$
test -z "$1" && { echo "Usage: html '<b>hello</b>"; exit 0; }

source /etc/profile.sh

test -n "$BROWSER" || { alert warning "/dev/browser not active (are you running outside of v86?)"; }
html="$*"

# if we are run as shebang, use the file as input
test -f "$1" && {
  html="$(cat $1 | tail +2)"
}

# below is not ideal
# first I tried /dev/ttyS* https://github.com/copy/v86/issues/530
# and differentiate processes by prefixing output by PID's 
# to parse it later with AWK, but it was very hairy

OUTPUT=/mnt/$PID
echo "$html" > /dev/browser/html
sleep 0.1
test -f $OUTPUT && {
  cat $OUTPUT
  rm $OUTPUT
}


# should we use flock, an awesome way to make processes read/write the same file
# while preventing 1001 concurrency issues?
#
# flock /dev/browser/html -c "echo \"$html\" > /dev/browser/html"