#! /bin/sh

# configure script for Gnome Xsu by freax

echo > config.help
exec 5>./config.log

echo "\
This file contains any messages produced by compilers while
running configure, to aid debugging if configure makes a mistake.
" 1>&5

name=xsu
version=0.2.3
author=freax
help=no
prefix="/usr/local"
root_user="root"
root_group="root"
CPP="cc"
gnome_config_prefix=""
xsu_location="${prefix}/bin"
man_base="${prefix}/share/man"
doc_path="${prefix}/share/doc"
su_pwd_out="Password: "
max_su_delay=30000

ac_ext=c
ac_prev=

for ac_option
do
  if test -n "$ac_prev"; then
    eval "$ac_prev=\$ac_option"
    ac_prev=
    continue
  fi

  case "$ac_option" in
  -*=*) ac_optarg=`echo "$ac_option" | sed 's/[-_a-zA-Z0-9]*=//'` ;;
  *) ac_optarg= ;;
  esac    
  
  case "$ac_option" in
    -mandir=* | --mandir=*)
    man_base="$ac_optarg" ;;

    -man-base=* | --man-base=*)
    man_base="$ac_optarg" ;;

    -doc-path=* | --doc-path=*)
    doc_path="$ac_optarg" ;;

    -prefix=* | --prefix=*)
    prefix="$ac_optarg" 
    xsu_location="${prefix}/bin"
    man_base="${prefix}/share/man"
    doc_path="${prefix}/share/doc";;

    -su-pwd-out=* | --su-pwd-out=*)
    su_pwd_out="$ac_optarg" ;;

    -max-su-delay=* | --max-su-delay=*)
    max_su_delay="$ac_optarg" ;;

    -gnome-config-prefix=* | --gnome-config-prefix=*)
    gnome_config_prefix="$ac_optarg/" ;;

    -debug| --debug)
    ccaddflag="-DDEBUG -ggdb" ;;
    
    -help | --help)
    help=yes

   
cat << EOF
Usage: configure [--option=value]

Help:
  --help                  	print this message
  
Directory and file names:
  --man-base=PATH	        Set path to man pages [$man_base]
  --doc-path=PATH           	Set path for documentation [$doc_path]
  --gnome-config-prefix=PATH	Where to find gnome-config [$gnome_config_prefix]
  --prefix=PATH			Build base [$prefix]
  --su-pwd-out=STRVALUE		Set what su outputs while waiting for password.
				Gnome Xsu will wait for this string to send the
  				password through the faked terminal. So this string
  				MUST be correct ! (test with "su - root -c test") 
				[$su_pwd_out]
  --max-su-delay=INTVALUE	Delay before su fails [$max_su_delay]
  --debug			Enables debugging mode (only for developpers)
  						
EOF
;;

  esac

done



if test "$help" = no; then

echo -n "Searching installed C compilers..."

# Search for cl
set dummy cl; ac_word=$2
ac_dummy="$PATH"
IFS="${IFS=   }"; ac_save_ifs="$IFS"; IFS=":"
 for ac_dir in $ac_dummy; do
    test -z "$ac_dir" && ac_dir=.
    if test -f $ac_dir/$ac_word; then
      ac_cv_prog_CC="cl"
      break
    fi
  done
CC="$ac_cv_prog_CC"
if test -n "$CC"; then
  echo -n "$CC "
else
  echo -n ""
fi
#Search for gcc
set dummy gcc; ac_word=$2
ac_dummy="$PATH"
IFS="${IFS=   }"; ac_save_ifs="$IFS"; IFS=":"
 for ac_dir in $ac_dummy; do
    test -z "$ac_dir" && ac_dir=.
    if test -f $ac_dir/$ac_word; then
      ac_cv_prog_CC="gcc"
      break
    fi
  done
CC="$ac_cv_prog_CC"
if test -n "$CC"; then
  echo -n "$CC "
else
  echo -n ""
fi
# Search for cc
set dummy cc; ac_word=$2
ac_dummy="$PATH"
IFS="${IFS=   }"; ac_save_ifs="$IFS"; IFS=":"
 for ac_dir in $ac_dummy; do
    test -z "$ac_dir" && ac_dir=.
    if test -f $ac_dir/$ac_word; then
      ac_cv_prog_CC="cc"
      break
    fi
  done
CC="$ac_cv_prog_CC"
if test -n "$CC"; then
  echo -n "$CC "
else
  echo -n ""
fi

test -z "$CC" && { 
	echo "none, check config.help"

cat >> config.help <<EOF
    *** ERROR: No acceptable cc found in \$PATH
    *** ERROR: You can get a free C compilercalled gcc
    *** ERROR: at http://www.gnu.org  

EOF
  cat config.help
  echo "This message is also stored in config.help"
  exit 1
}
echo



CPP=$CC
ac_cpp="$CPP"



echo -n "Checking whether the C compiler ($CPP) works..."
cat > conftest.$ac_ext <<EOF
int main() { return 0; }
EOF

if (eval "$ac_cpp -E conftest.$ac_ext" 2>&5 > /dev/null); then
  rm -rf conftest*
  echo "yes"
else
  rm -rf conftest*
  echo "the C compiler failed, check config.help"

cat >> config.help <<EOF
    *** ERROR: The C compiler failed to build a simple test.
    *** ERROR: Maybe there is no C compiler installed ?
    *** ERROR: If so, you can get a free C compiler called gcc 
    *** ERROR: at http://www.gnu.org

EOF
cat config.help

  echo "This message is also stored in config.help"
  exit 1
fi

echo -n "Checking whether we are using the GNU C Library""..."
cat > conftest.$ac_ext <<EOF
#include <features.h>
#ifdef __GNU_LIBRARY__
  Thanks for using GNU
#endif
EOF

if (eval "$ac_cpp -E conftest.$ac_ext | egrep \"Thanks for using GNU\"" 2>&5 > /dev/null); then
  rm -rf conftest*
  ac_cv_gnu_library=yes
  echo "yes"
else
  rm -rf conftest*
  ac_cv_gnu_library=no
  echo "no"
  echo "*** WARNING: The GNU C Library was not found"
fi  
rm -f conftest*


echo -n "Checking for version 2 of the GNU C Library""..."
cat > conftest.$ac_ext <<EOF
#include <features.h>
#ifdef __GLIBC__  
  Thanks for using GNU too
#endif
EOF
  
if (eval "$ac_cpp -E conftest.$ac_ext | egrep \"Thanks for using GNU too\"" 2>&5 > /dev/null); then
  rm -rf conftest*
  ac_cv_glibc=yes
  echo "yes"
else
  rm -rf conftest*
  ac_cv_glibc=no
  echo "no"
  echo "*** WARNING: The second version of the GNU C Library was not found"
fi
rm -f conftest*

echo -n "Finding out how to set the DISPLAY environment variable..."

	set CONFIG_SET_TEST "gnome xsu rules" 2>&5
	setenv CONFIG_SETENV_TEST "gnome xsu rules" 2>&5
	export CONFIG_EXPORT_TEST="gnome xsu rules" 2>&5

	if test "$CONFIG_SET_TEST" = "gnome xsu rules"; then
		disenv="set DISPLAY "
	fi
	if test "$CONFIG_SETENV_TEST" = "gnome xsu rules"; then
		disenv="setenv DISPLAY "
	fi
	
	if test "$CONFIG_EXPORT_TEST" = "gnome xsu rules"; then
		disenv="export DISPLAY="
	fi

	if test "$disenv" = ""; then
		echo "Could not determine wheter to use export or setenv"
		echo "Please type in how you would have to set the DISPLAY"
		echo "environment variable on your system below"
		echo "(example, type: 'export DISPLAY=' for bash or"
		echo "'setenv DISPLAY ' when you have the setenv command) :"
		read disenv
	fi

echo "done"

echo -n "Searching su command..."
# Search for su
set dummy su; ac_word=$2
ac_dummy="$PATH"
IFS="${IFS=   }"; ac_save_ifs="$IFS"; IFS=":"
 for ac_dir in $ac_dummy; do
    test -z "$ac_dir" && ac_dir=.
    if test -f $ac_dir/$ac_word; then
		su_found="1"
		SU=$ac_dir/$ac_word
      break
    fi
  done
test -z "$su_found" && { 
	echo "none, check config.help"

cat >> config.help <<EOF
    *** ERROR: No acceptable su found in \$PATH
    *** ERROR: You should install the sh-utils package
    *** ERROR: of GNU and make sure the su command is
    *** ERROR: in the PATH. (http://www.gnu.org)

EOF
  cat config.help
  echo "This message is also stored in config.help"
  exit 1
}
echo "ok"

if test -d "bin"; then
    echo "Creating directory bin...skipped"
else
    echo -n "Creating directory bin..."
    mkdir bin
    echo "done"
fi


gnomelibsv=`"$gnome_config_prefix"gnome-config --version 2>&5 | cut -b 12-17`
echo -n "Checking Gnome-libs version..."

if test "$gnomelibsv" = ""; then
    echo "gnome-config not found, check config.help"

cat >> config.help <<EOF
    *** ERROR: Xsu needs the gnome development libraries
    *** ERROR: However, this configure script can't find the 
    *** ERROR: gnome-config script and so cannot determine
    *** ERROR: how to create the Makefile for xsu
    *** ERROR: 
    *** ERROR: Also check config.log for the exact error
    *** ERROR:
    *** ERROR: Solution for this problem if you don't have gnome-libs-devel :
    *** ERROR: Install gnome-libs-devel 
    *** ERROR: These urls might help you finding packages for your system: 
    *** ERROR: 	 http://www.gnome.org
    *** ERROR:   http://rpmfind.net/linux/rpm2html/search.php?query=gnome%2Dlibs%2Ddevel
    *** ERROR: 
    *** ERROR: Solution for this problem if you do have gnome-libs-devel :
    *** ERROR: Use ./configure --gnome-config-prefix=PATH
    *** ERROR: Also checkout ./configure --help

EOF
cat config.help
    echo "This message is also stored in config.help"
    exit 1
else
    echo $gnomelibsv
fi


echo -n "Writing src/config.h..."
echo 								>  src/config.h
echo "#define	EXIT_ERROR	1" 				>> src/config.h
echo "#define	EXIT_CORRECT	0"				>> src/config.h
echo "#define	VERSION		\"$version\""			>> src/config.h
echo "#define	AUTHOR		\"$author\""			>> src/config.h
echo "#define	NAME		\"$name\""			>> src/config.h
echo "#define	SET_DISPL_ENV	\"$disenv\""			>> src/config.h
echo "#define	SU_PWD_OUT	\"$su_pwd_out\""		>> src/config.h
echo "#define	SU_DELAY	$max_su_delay"			>> src/config.h
echo "#define	SU_PATH		\"$SU\""			>> src/config.h
echo "done"


echo -n "Writing Makefile..."

echo "# Makefile for xsu-$version by freax" > Makefile

cat >> Makefile <<EOF

CC = $CPP
CFLAGS01 = src/gui/gui.c -o bin/xsu \`gnome-config --cflags --libs gnomeui zvt\` $ccaddflag
INSTALL = install -c
DOCFILES = README AUTHORS \
	doc/html/xsu_doc.html doc/html/xsu_example.jpg doc/html/xsu_example2.png  doc/html/xsu_example3.png doc/html/xsu_in_gmenu.jpg

all:
	@echo "- C o m p i l i n g ----------------------------------------"
	\$(CC) \$(CFLAGS01)
	@echo "- C o m p i l i n g   c o m p l e t e ----------------------"
	@echo "Now do 'make install' as root"
	@echo

install:
	@echo "- I n s t a l l i n g --------------------------------------"
	install -d $xsu_location
	\$(INSTALL) -m 755 bin/xsu $xsu_location
	@echo "- I n s t a l l i n g   D o c u m e n t a t i o n ----------"
	install -d $man_base/man8
	install -d $doc_path/$name
	gzip -c9 doc/man/xsu.8 > $man_base/man8/xsu.8.gz
	\$(INSTALL) -m 644 \$(DOCFILES) $doc_path/$name/
	@echo "- I n s t a l l a t i o n   c o m p l e t e ----------------"
	@echo "Now read the README file"
	@echo ""

clean:
	@echo "- c l e a n   ----------------------------------------------"
	rm -rf bin/*
	@echo "- c l e a n i n g   c o m p l e t e ------------------------"

distclean:
	@echo "- D i s t c l e a n   --------------------------------------"
	rm -f src/config.h
	rm -rf bin
	rm -f config.log
	rm -f config.help
	rm -f Makefile
	@echo -n "Writing Makefile..."
	@echo "install:" > Makefile
	@echo -e "\t@echo \"Run ./configure first!\"" >> Makefile
	@echo "done"
	@echo "- D i s t c l e a n i n g   c o m p l e t e ----------------"
uninstall:
	@echo "- U n i n s t a l l   --------------------------------------"
	rm -f $xsu_location/xsu
	rm -f $man_base/man8/xsu.8.gz
	rm -rf $doc_path/$name
	@echo "- U n i n s t a l l i n g   c o m p l e t e ----------------"
EOF
echo "done"
echo
echo "- I n s t a l l a t i o n    N o t e s --------------------------"
echo "If any of these are incorrect, correct them first by rerunning"
echo "the ./configure script (check ./configure --help)"
echo
echo "The following items will be installed :"
echo
echo "Gnome Xsu binary  : $xsu_location/xsu"
echo "Manual page       : $man_base/man8/xsu.8.gz"
echo "Documentation     : $doc_path/$name"
echo
echo "The following settings will be used :"
echo
echo "How to set the DISPLAY environment : \"$disenv:0\""
echo "What the Unix tool 'su' must output before the password is send : \"$su_pwd_out\""
echo "Max delay before Gnome Xsu thinks that su has failed : $max_su_delay"
echo "Path to the su binary (warning, this must be correct) : $SU"
echo "If the PATH to the su binary is not correct, then this might be"
echo "a major security problem on your system !"
echo "- I n s t a l l a t i o n    N o t e s --------------------------"
echo



    echo "Now do 'make' to proceed with the installation"
    echo ""
fi

