#!/bin/bash
# Ghetto-ass way of configuring alloywm for the Makefiles, not like
# GNU autoconf though.

# Procedures
fail() {
  echo "Failed"
  exit 1
}

make_makefiles() {
  IFS=","
  for i in $makefiles
  do
    if [ ! -f "${i}.in" ]
    then
      echo "${i}.in does not exist."
      exit
    else
      echo -n "creating $i... "
      ./${i}.in || fail
      echo "OK"
    fi
  done
}

create_help() {
cat <<//END
configure script for alloywm, created by Kensuke Otake.  Not based on GNU
autoconf (but may be soon).

Valid options are:
//END

  IFS=","
  for i in $options
  do
    case $i in
    --help) # Produce help line
      echo "		--help			Produce this smut"
    ;;
    --prefix) # Produce prefix info
      echo "		--prefix=<dir>		Location to install files"
    ;;
    --xroot) # Produce X Window root switch info
      echo "		--xroot=<dir>		Specify location of X11R6"
    ;;
    esac
  done
  IFS=","
  exit
}

check_cache() {
  if [ ! -f "config.cache" ]
  then
    echo "creating cache ./config.cache"
    touch ./config.cache
  else
    rm ./config.cache
    touch ./config.cache
  fi
}

check_log() {
  if [ ! -f "config.log" ]
  then
    echo "creating log ./config.log"
    touch ./config.log
  else
    rm ./config.log
    touch ./config.log
  fi
}

create() {
  echo "creating $1 $2"
  touch $2
}

check_build_environment() {
  if [ "`which gcc`" = "" ]
  then
    if [ "`which cc`" = "" ]
    then
      echo
      echo "Cannot find a C compiler.  Please check to be sure that there is "
      echo "one in the PATH variable."
    fi
  fi
  if [ "`which gmake`" = "" ]
  then
    if [ "`which make`" = "" ]
    then
      echo
      echo "Cannot find either BSD make, or GNU gmake.  Please check to be sure" 
      echo "that these are in the PATH variable."
    fi
  fi
}

check() {
  case $1 in
  "-f") # Check for a file not in PATH
    echo -n "$2... "
    if [ -f "$3" ]
    then
      echo "$3"
      # Check to see if there was a variable passed
      if [ "$4" != "" ]
      then
        echo "$4=$3" >> ./config.cache
      fi 
    else
      echo "Missing"
      echo "$3=Missing" >> ./config.log
    fi
  ;;
  "-d") # Check for a directory
    echo -n "$2... "
    if [ -d "$3" ]
    then
      echo "$3"
      # Check to see if there was a variable passed
      if [ "$4" != "" ]
      then
        echo "$4=$3" >> ./config.cache
      fi
    else
      echo "Missing"
      echo "$3=Missing" >> ./config.log
    fi
  ;;
  *) # Something else
    echo -n "$1... "
    if [ -f "`which $2`" ]
    then
      echo "`which $2`"
      if [ "$3" != "" ]
      then
        echo "$3=$2" >> ./config.cache
      fi
    else
      echo "Missing"
      echo "$2=Missing" >> ./config.log
    fi
  ;;
  esac 
}

cache_var() {
  echo "$1=$2" >> config.cache
}

prepare_build() {
  echo -n "checking for any missing components... "
  for i in `grep Missing config.log`
  do
    case $i in
    "gcc=Missing") # No gcc?
      if [ "`which cc`" = "" ]
      then
        echo
        echo "Cannot find a C compiler in the PATH."
        exit 1
      fi
    ;;
    "/usr/X11R6=Missing") # No X?
      echo
      echo "Could not find /usr/X11R6.  If X Window is installed elsewhere,"
      echo "specify where with the --xroot flag."
      exit 1
    ;;
    "gtk-config=Missing") # Big deal, just don't compile with Gtk+!
      GTKCONFIG=""
    ;;
    esac
  done
  echo "None"

  # Additional variables must be posted here
  cache_var PREFIX $PREFIX
  cache_var XROOT $XROOT
  cache_var BASH $BASH
}

# Local variables
options="--prefix,--help,--xroot"
makefiles="Makefile,goodies/Makefile,src/Makefile,scriptdir/x-panel,scriptdir/mu2-alloywm"
PREFIX="/usr/local"
XROOT="/usr/X11R6"

# Check to see if there were any options passed on the command line
for i in $@
do
  case $i in
  --prefix=*) # A prefix was defined
    PREFIX=${i#--prefix=}
    if [ ! -d "$PREFIX" ]
    then
      echo "$PREFIX does not exist."
      exit 1
    fi
  ;;
  --xroot=*) # X Window directory
    XROOT=${i#--xroot=}
  ;;
  --help) # Help
    create_help
  ;;
  "") # Continue, I guess.
  ;;
  --*) # An oddball
    echo "Invalid option, try $0 --help."
    exit 1
  ;;
  esac
done
  
# Check things
check_cache
check_log
check "checking for a BSD compatibile install" install
check_build_environment
check "checking for make" make MAKE
check "checking for gmake" gmake MAKE
check "checking for cc" cc CC
check "checking for gcc" gcc CC
check "checking to see where bash is" bash BASH
check -d "checking for X11R6" $XROOT XROOT
check -d "checking for X11R6 includes" $XROOT/include XINCL
check -d "checking for X11R6 libraries" $XROOT/lib XLIB
check -f "checking for X11/xpm.h" $XROOT/include/X11/xpm.h
check -f "checking for X11/extensions/shape.h" $XROOT/include/X11/extensions/shape.h SHAPE
check "checking for gtk-config" gtk-config GTKCONFIG
prepare_build
make_makefiles
echo "Your build environment seems sane.  Just execute 'gmake' to build."

