#!/bin/sh

# This script runs qodem under X11.  It looks for qodem-x11 first.  If
# that is not found, it then looks for uxterm, konsole, gnome-terminal
# and x-terminal-emulator (in that order) and starts it up with qodem
# inside it.
#
# CREDITS: this script is based on the 'xminicom' script from minicom.
#
# Written 2003-2017 by Kevin Lamonte
#
# To the extent possible under law, the author(s) have dedicated all
# copyright and related and neighboring rights to this software to the
# public domain worldwide. This software is distributed without any
# warranty.
#
# You should have received a copy of the CC0 Public Domain Dedication
# along with this software. If not, see
# <http://creativecommons.org/publicdomain/zero/1.0/>.

# Qodem needs a Unicode font that includes the CP437 glyphs.  Terminus
# does the job nicely.  bolkhov-vga (uni_vga) does even better, but is
# a bit small on large displays.  If uxterm is found, then the
# following fonts are searched in order to see if any is present, if
# not found then a default Terminus is used.  For any other terminal
# (gnome-terminal, x-terminal-emulator, ...), we do not try to set the
# font.
FONTS="-*-terminus-*-r-normal-*-20-*-*-*-*-*-iso10646-* -bolkhov-vga-medium-r-normal--*-*-*-*-*-*-iso10646-*"

findcmd()
{
  IFS=:
  for i in $PATH
  do
	if [ -x $i\/$1 ]
	then
		result=$i\/$1
		IFS=
		return 0
	fi
  done
  result=
  IFS=
  return 1
}

if findcmd qodem-x11
  then
  exec $result
  exit 1
fi

if findcmd uxterm
  then
  uxtermbin=$result
  if findcmd xlsfonts && findcmd wc
    then
    IFS=\ 
    for i in $FONTS
      do
      count=`xlsfonts -fn $i 2>/dev/null | wc -l`
      if test ! "X$count" = "X0"
        then
        echo "Using font $i"
        exec $uxtermbin -fn $i -T qodem -bg black -fg white -n qodem -geometry 80x25 -e "if ! qodem ; then echo Press ENTER to continue...; read; fi"
      fi
    done
  fi
  # None of the fonts was found, go with the default and see what
  # xterm finds.
  exec $uxtermbin -fn -*-terminus-*-r-normal-*-24-*-*-*-*-*-iso10646-* -T qodem -bg black -fg white -n qodem -geometry 80x25 -e "if ! qodem ; then echo Press ENTER to continue...; read; fi"
  exit 1
fi

if findcmd gnome-terminal
  then
  exec $result --title qodem --geometry 80x25 -e "if ! qodem ; then echo Press ENTER to continue...; read; fi"
  exit 1
fi

if findcmd x-terminal-emulator
  then
  exec $result -T qodem -e "if ! qodem ; then echo Press ENTER to continue...; read; fi"
  exit 1
fi

echo "xqodem: qodem-x11, uxterm, gnome-terminal, and x-terminal-emulator NOT found!" 1>&2
exit 1
