#!/bin/bash

ARGS=
HW=
DPY=

while [ "$1" ]; do
  case "$1" in
    -nohw|-hw=* )
      HW="$HW $1";
      ;;
    -twin@* )
      DPY="${1:5}";
      ;;
    -* )
      ARGS="$ARGS $1";
      ;;
    *:[0-9a-fA-F]* )
      DPY="$1";
      ;;
  esac
  shift
done

if [ -z "$HW" ]; then
  if [ "$DISPLAY" ]; then
    HW="-hw=X,font=9x19"
  elif [ "$TWDISPLAY" ]; then
    HW="-hw=twin@$TWDISPLAY"
  else
    HW="-hw=tty"
  fi
fi

if [ "$DPY" ]; then
  # check it is a valid TWDISPLAY!
  DPY=`twfindtwin "$DPY"`
else
  DPY=`twfindtwin`
fi

if [ "$DPY" ]; then
  echo twstart: detected twin running on "$DPY"
  exec twdisplay -twin@"$DPY" $ARGS $HW
else
  exec twin $ARGS $HW
fi

