#!/bin/sh

# DO NOTE! This script may get more arguments in the future, if you set up
# a custom one keep that in mind.  You should only ever rely on the first
# and second argument.  Maybe I should figure out a better way to do the
# translations

# This script gets some arguments, they are translated messages like (not
# word by word obviously)
#
# The first argument is the X setup proggie to run
# The second argument is a temporary file we can use
#
# $3 = 'Your X server is b0rk I will disable this server'
# $4 = 'Would you like to try to reconfigure your server?"
# $5 = 'Please type in the root password'
# $6 = 'I will now restart the X server again'
# $7 = 'I will disable this X server'
# $8 = 'Your X server is b0rk, do ya wanna view X logs?'

# there are some env vars defined:
# XLOG = the log file for the X server
# BINDIR = location of gdm binaries
# SBINDIR = location of gdm system binaries

# 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 didn't work so do alternative things
#      to tell the user if possible

# we require "gdmopen", to open a console, because we really don't
# 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 $SBINDIR/gdmopen ; 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
  $SBINDIR/gdmopen $0 -noopen "$@"
  exit $?
fi

SETUP=`which $1`
DIALOG=`which dialog`
if test x$DIALOG = x ; then
  DIALOG=`which gdialog`
  if test x$DIALOG = x ; then
    DIALOG=`which whiptail`
  fi
fi
if test -x $SBINDIR/gdmaskpass ; then
  ASKPASS=$SBINDIR/gdmaskpass
else
  ASKPASS=
fi

clear

# Note, dialog required, though this script could be fixed to not require it
# I suppose
if test x = x$SETUP -o x = x$DIALOG ; then
  echo =======================================================================
  echo 
  cat $XLOG
  echo 
  echo =======================================================================
  echo
  echo  "$3"
  echo
  echo =======================================================================
  read
  exit 1
fi

if $DIALOG --yesno "$8" 10 50 ; then 
  $DIALOG --textbox $XLOG 22 76
fi

if $DIALOG --yesno "$4" 10 50 ; then 
  clear
  echo
  echo "$5"
  if test x$ASKPASS = x ; then
    # dirty trick to fool su into asking the root password even if we're
    # root
    su nobody -c "su -c $SETUP"
  else
    if $ASKPASS ; then
      $SETUP
    fi
  fi
  clear
  $DIALOG --msgbox "$6" 8 50
  exit 0
else
  $DIALOG --msgbox "$7" 8 50
  exit 1
fi
