#!/bin/sh
#
# ($Id: build,v 1.9 1998/08/14 22:00:42 schmitzj Exp $)
#

SYSTEM=`uname`
echo "Running on $SYSTEM"


if [ "$1" = "" ]; then

  echo "Usage:"
  echo " $0 help     - more usage information"
  echo " $0 config   - makes config file"
  echo " $0 make     - makes library"
  echo " $0 install  - copy library and includes"
  echo " $0 examples - makes programs"
  echo " $0 all (req source code) config & make"
  echo " $0 backup (req source code)"
fi

if [ "$1" = "help" ]; then
  cat <<EOF
                                              Xclasses build
------------------------------------------------------------

config:
 scans system for installed and required components,
 builds configuration file (reqired by following options)

make:
 compiles library

install:
 copies library, include files and default config file

examples:
 compiles example programs in example directory
 (requires a compiled library)

all:
 calls 'config' and 'make', neither 'install' nor 'examples'

backup:
 only for Xclasses author!

EOF
fi

if [ "$1" = "install" ]; then
  MAKE=`cat Rules.make`
  $MAKE install
fi

if [ "$1" = "backup" ]; then
  MAKE=`cat System/make.$SYSTEM`
  $MAKE pack;$MAKE code
fi

if [ "$1" = "all" -o "$1" = "config" ]; then
  rm -f Rules
  rm -f Rules.make

  INCLUDESDIR=${XCLASSESINCLUDE:-"/usr/local/include"}
  LIBSDIR=${XCLASSESLIB:-"/usr/local/lib"}
  CONFIGSDIR=${XCLASSESCONFIG:-"/usr/lib/X11/Xclasses"}
  ROOTID=0

  echo -n "includes dir [$INCLUDESDIR] :"
  read R
  if [ -n "$R" ]; then
    INCLUDESDIR="$R"
  fi
  echo -n "libs dir [$LIBSDIR] :"
  read R
  if [ -n "$R" ]; then
    LIBSDIR="$R"
  fi
  echo -n "configs dir [$CONFIGSDIR] :"
  read R
  if [ -n "$R" ]; then
    CONFIGSDIR="$R"
  fi   
  echo -n "root id (Xclasses owner) [$ROOTID] :"
  read R
  if [ -n "$R" ]; then
    ROOTID="$R"
  fi   

# check for gnumake
  if [ -n "`make -v 2>/dev/null|grep GNU`" ]; then
    MAKE="make"
  else
    if gnumake -v 2>/dev/null 1>&2; then
      MAKE="gnumake"
    else
      echo "Can't find GNU make (looked for 'make' and 'gnumake')."
      echo "Falling back to 'make' - let's hope it works...."
      MAKE="make"
    fi
  fi

# look for installed XPM lib
  MAKEXPM="y"
  for N in /usr/include/X11 /usr/local/include/X11
  do
    if [ -f "$N/xpm.h" ]; then
     MAKEXPM="n"
    fi
  done
  if [ "$MAKEXPM" = "y" ]; then
    echo "Couldn't find XPM library! Please check or install."
    MAKEXPM="n"
  fi

# look for shapes
  if [ -f "/usr/include/X11/extensions/shape.h" ]; then
    USESHAPE="y"
  else
    USESHAPE="n"
  fi

# check for micro sleep - newer faster systems sleep in
# nano seconds (nanosleep), olders in micro seconds (usleep)
# if we can't find usleep we belive to have nanosleep - or we 
# should forget it at all! ;-)
# (old AIX has nsleep but this is handles in the source code)
  if [ -n "`grep usleep /usr/include/unistd.h`" ]; then
    SLEEPER="NO_NANOSLEEP"
  else
    SLEEPER="NANOSLEEP"
  fi

# developed with "g++" - so we should use it if available.
# Sun and others uses CC for C++ but mostly AT&T C++ not ANSI....
  if g++ --version 2>/dev/null 1>&2; then
    CC="g++"
    CCVERS=`gcc --version`
  else
    CC="CC"
  fi
  if [ ! -f "System/ARules.$SYSTEM" ]; then
    SYSTEM="default"
  fi

  LDFLAGS="-lXclasses -lX11 -lXext -lXpm -lm"
  if [ "$SYSTEM" = "SunOS" ]; then
    LDFLAGS="$LDFLAGS -lposix4"
  fi

  cat >/tmp/build$$.c <<EOF
#ifdef __${SYSTEM}__
int main(){return 1;}
#else
int main(){return 0;}
#endif
EOF
  $CC -o /tmp/build$$.x /tmp/build$$.c
  if /tmp/build$$.x ; then
    SYSDEFINE=-D__${SYSTEM}__
  fi
  rm -f /tmp/build$$.x /tmp/build$$.c

# hack to preevent corrupted code/.version file when using cvs
cat code/.version | if read a1 && read a2 && read a3 && read a4 ; then
  if [ -n "$a2" ] && [ -n "$a4" ] ; then
    if [ $a2 -gt $a4 ] ; then
      echo $a2 >code/.version
    else
      echo $a4 >code/.version
    fi
  fi
fi

# Now writing all knowledge in a file... and send it to Billy....
  echo "$MAKE" >Rules.make
  cat >Rules <<eof
##############################################################
##
## This is a generated file
##

INCLUDESDIR=$INCLUDESDIR
LIBSDIR=$LIBSDIR
CONFIGSDIR=$CONFIGSDIR
ROOTID=$ROOTID

CC=$CC
MAKE=$MAKE
AR=ar
TAR=tar

MAKEXPM=$MAKEXPM
USESHAPE=$USESHAPE
DEFINES=-D$SLEEPER $SYSDEFINE -DCONFIGSDIR=\\"\$(CONFIGSDIR)\\" -DROOTID=\$(ROOTID) -DCCVERSION=\\"$CCVERS\\"

LIBMAJOR=0
LIBMINOR=8
LIBPATCH=1

VERSION=0.40.1

`cat System/ARules.$SYSTEM`

eof
rm -f setenvxc
cat >setenvxc <<eof
#!/bin/sh
#
# Xclasses setenv script
#
export XCLASSESLIB="$LIBSDIR"
export XCLASSESINCLUDE="$INCLUDESDIR"
export XCLASSESCONFIG="$CONFIGSDIR"
export XCLASSESLDFLAGS="$LDFLAGS"

eof
fi

if [ "$1" = "all" -o "$1" = "make" ]; then
 MAKE=`cat Rules.make`
 $MAKE code
 echo "Now install by '$0 install' as root or what ever UID"
 echo "you specified."
 echo "Execute ". ./setenvxc" before making Xclasses programs or"
 echo "place this script in your profile."
 echo
 echo "Then start '$0 examples' to make example programs."
fi

if [ "$1" = "examples" ]; then
 MAKE=`cat Rules.make`
 $MAKE examples_dir
fi
