#!/bin/sh

if test "x$UNSAFE_TO_TRANSLATE" = "xyes" ; then
  LANG=C
  unset LC_ALL
  unset LC_MESSAGES
fi

if test -x "$LIBEXECDIR/mdmtranslate" ; then
  MDMTRANSLATE="$LIBEXECDIR/mdmtranslate"
else
  MDMTRANSLATE=
fi

# Set the console to UTF-8 if needed.
CHARMAP=`locale charmap`
if test "$CHARMAP" = "UTF-8"
then
  unicode_start 2> /dev/null || true
else
  unicode_stop 2> /dev/null || true
fi

gettextfunc () {
  if test "x$MDMTRANSLATE" != "x" ; then
    "$MDMTRANSLATE" "$1"
  else
    echo "$1"
  fi
}

OLD_IFS=$IFS

mdmwhich () {
  COMMAND="$1"
  OUTPUT=
  IFS=:
  for dir in $PATH
  do
    if test -x "$dir/$COMMAND" ; then
      if test "x$OUTPUT" = "x" ; then
        OUTPUT="$dir/$COMMAND"
      fi
    fi
  done
  IFS=$OLD_IFS
  echo "$OUTPUT"
}

# X has a bad habit of not restoring the vt settings on exit.  Make sure that
# the keyboard is restored to xlate mode, since we're going to try to interact
# with the user
# (http://bugzilla.ubuntu.com/show_bug.cgi?id=6286)
KBD_MODE=`mdmwhich kbd_mode`
if test "x$KBD_MODE" != "x" ; then
  kbd_mode -a
fi

MSG3=`gettextfunc "Failed to start the X server (your graphical interface).  It is likely that it is not set up correctly.  You will need to log in on a console and reconfigure the X server.  Then restart MDM."`
MSG4=`gettextfunc "Would you like to try to configure the X server?  Note that you will need the root password for this."`
MSG5=`gettextfunc "Please type in the root (privileged user) password."`
MSG6=`gettextfunc "Trying to restart the X server."`
MSG7=`gettextfunc "The X server is now disabled.  Restart MDM when it is configured correctly."`
MSG8=`gettextfunc "Failed to start the X server (your graphical interface).  It is likely that it is not set up correctly.  Would you like to view the X server output to diagnose the problem?"`
MSG9=`gettextfunc "Would you like to view the detailed X server output as well?"`

# there are some env vars defined:
# XLOG = the log file for the X server
# BINDIR = location of mdm binaries
# SBINDIR = location of mdm system binaries
# LIBEXECDIR = location of mdm libexec binaries (mdmaskpass, mdmopen)

# return values are
# 0 = try again, runs this server again
# 1 = abort this display, removes this server from the list
# 32 = something went very wrong, things will just get logged.
#      this means this script didnt work so do alternative things
#      to tell the user if possible

#
# Now we check for dialog and mdmaskpass
#
DIALOG_OPTS=
DIALOG=`mdmwhich dialog`
if test "x$DIALOG" = "x" ; then
  DIALOG=`mdmwhich whiptail`
  DIALOG_OPTS="--scrolltext"
fi
if test -x "$LIBEXECDIR/mdmaskpass" ; then
  ASKPASS="$LIBEXECDIR/mdmaskpass"
else
  ASKPASS=
fi

# we require 'mdmopen', to open a console, because we really dont
# have one.  Perhaps someone should try to figure out some shell
# black magic to get this to work on other then linux systems
if test ! -x "$LIBEXECDIR/mdmopen" ; then
  exit 32
fi

# when we run ourselves from the open we will pass a -noopen argument
if test "x$1" = "x-noopen" ; then
  shift
else
  #
  # We do a lot of work wastefully over again, but oh well,
  # perhaps this needs fixing.
  #
  "$LIBEXECDIR/mdmopen" -l /bin/sh -c "$0 -noopen $@"
  exit $?
fi

clear

# Note, dialog required, though this script could be fixed to not require it
# I suppose
if test "x" = "x$DIALOG" ; then
  echo =======================================================================
  echo 
  cat "$XLOG"
  echo 
  echo =======================================================================
  echo
  if test -x /usr/bin/fmt ; then
    echo  "$MSG3" | /usr/bin/fmt
  else
    echo  "$MSG3"
  fi
  echo
  echo =======================================================================
  read dummy
  exit 1
fi

if "$DIALOG" --yesno "$MSG8" 10 50 ; then 
  "$DIALOG" $DIALOG_OPTS --textbox "$XLOG" 22 76
  # Support XFree86 v4 logfile thingie
  if grep '^(..) Log file: "' "$XLOG" 2> /dev/null ; then
    XLOG_DETAIL=`grep '^(..) Log file: "' "$XLOG" | sed 's/^[^"]*"\([^"]*\)".*$/\1/'`
    if "$DIALOG" --yesno "$MSG9" 10 50 ; then 
      "$DIALOG" $DIALOG_OPTS --textbox "$XLOG_DETAIL" 22 76
    fi
  fi
fi

"$DIALOG" --msgbox "$MSG7" 8 50
exit 1
