#!/bin/sh
# Usage: xresprobe driver
# Copyright (C) 2004 Canonical Ltd
# Author: Daniel Stone <daniel.stone@canonical.com>
# 
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; version 2 of the License.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License with
#  the Debian GNU/Linux distribution in file /usr/share/common-licenses/GPL-2;
#  if not, write to the Free Software Foundation, Inc., 59 Temple Place,
#  Suite 330, Boston, MA  02111-1307  USA
#
# On Debian systems, the complete text of the GNU General Public
# License, version 2, can be found in /usr/share/common-licenses/GPL-2.

DATADIR="/usr/share/xresprobe"

DRIVER="$1"
SCREENTYPE="$2"
RETCODE=0

if [ -z "$DRIVER" ]; then
  echo "Driver must be specified."
  exit 1
fi

if [ -n "$SCREENTYPE" ]; then
  if [ "$SCREENTYPE" = "laptop" ]; then
    LAPTOP="yes"
  elif [ "$SCREENTYPE" = "crt" ]; then
    DDC="yes"
  elif [ "$SCREENTYPE" = "lcd" ]; then
    DDC="yes"
  fi
else
  if which laptop-detect >/dev/null 2>&1; then
    if laptop-detect >/dev/null 2>&1; then
      LAPTOP="yes"
    else
      DDC="yes"
    fi
  else
    echo "laptop-detect must be installed for xresprobe to work properly. If"
    echo "you really want to continue without it, call xresprobe like:"
    echo "$0 drivername [laptop|crt|lcd]"
    echo
    echo "Note that laptop LCDs are different from standard desktop LCDs, so"
    echo "be careful how you call it."
  fi
fi

if [ -n "$XRESPROBE_DEBUG" ]; then
  echo "laptop: $LAPTOP; ddc: $DDC" >&2
fi

doddc() {
  if [ -n "$XRESPROBE_DEBUG" ]; then
    echo "attempting DDC detection" >&2
  fi
  DDCOUT="$("$DATADIR/ddcprobe.sh")"
  RETCODE="$?"
  RES="$(echo "$DDCOUT" | grep "^res:" | sed -e 's/^res: *//;')"
  IDENTIFIER="$(echo "$DDCOUT" | grep "^name:" | sed -e 's/^name: *//;')"
  FREQ="$(echo "$DDCOUT" | grep "^freq:" | sed -e 's/^freq: *//;')"
}

doprobe() {
  if [ -n "$XRESPROBE_DEBUG" ]; then
    echo "attempting an X probe" >&2
  fi
  XPROBEDIR="$("$DATADIR/xprobe.sh" "$DRIVER")"
  RETCODE="$?"
  LOGFILE="$XPROBEDIR/xfree86.log"
  RES="$("$DATADIR/lcdsize.sh" $DRIVER $LOGFILE)"
  if [ -n "$XRESPROBE_DEBUG" ]; then
    echo "not removing temporary xprobe directory $XPROBEDIR; please do this \
          by hand" >&2
  else
    rm -rf "$XPROBEDIR"
  fi
}

if [ "x$LAPTOP" = "xyes" ]; then
  if [ "$(uname -m)" = "ppc" ] || [ "$(uname -m)" = "ppc64" ]; then
    doddc
  fi
  if [ -z "$RES" ]; then
    doprobe
  fi
elif [ "x$DDC" == "xyes" ]; then
  doddc
fi

if [ -n "$XRESPROBE_DEBUG" ]; then
  echo "id: $IDENTIFIER" >&2
  echo "res: $RES" >&2
  echo "freq: $FREQ" >&2
fi

echo "id: $IDENTIFIER"
echo "res: $RES"
echo "freq: $FREQ"
exit $RETCODE
