#! /bin/sh
#
# $Id$

#
# Default settings
#
SYSTEM=
BEOS_NETSERVER=no
BEOS_OLDCC=no
MATH=no
PTHREAD=no
OPENSSL=
GTK=
PREFIX=/usr/local
CC="${CC-cc}"
CFLAGS="${CFLAGS}"
CXX="${CXX-c++}"
CXXFLAGS="${CXXFLAGS}"
LDFLAGS="${LDFLAGS}"
VERBOSE=no

#
# Functions
#
usage()
{
  cat << EOF

Options:
  --disable-openssl      Disable OpenSSL, use built-in SHA1 implementation
  --disable-gtk          Don't build the GTK+ GUI
  --prefix=PATH          Installation path
  --verbose              Display additional information for debugging

Some influential environment variables:
  CC          C compiler command
  CFLAGS      C compiler flags
  CXX         C++ compiler command
  CXXFLAGS    C++ compiler flags
  LDFLAGS     linker flags

EOF
}

runcmd()
{
  if [ "$VERBOSE" = yes ]
  then
    echo "$@" >&2
    "$@"
  else
    "$@" > /dev/null 2>&1
  fi
  return $!
}

verbose()
{
  if [ "$VERBOSE" = yes ]
  then
    echo "$@"
  fi
}

cc_test()
{
  verbose cc_test
  cat > testconf.c <<EOF
  int main()
  {
    return 0;
  }
EOF
  if ! runcmd $CC -o testconf testconf.c
  then
    rm -f testconf*
    echo "Could not find a working compiler"
    exit 1
  fi
  rm -f testconf*
}

openssl_test()
{
  verbose openssl_test
  cat > testconf.c << EOF
  #include <stdio.h>
  #include <openssl/sha.h>
  int main()
  {
      SHA1( 0, 0, 0 );
  }
EOF
  if runcmd $CC $CFLAGS $LDFLAGS -o testconf testconf.c -lcrypto
  then
    echo "yes"
    OPENSSL=yes
  else
    echo "missing, using built-in SHA1 implementation"
    OPENSSL=no
  fi
  rm -f testconf*
}

lm_test()
{
  verbose lm_test
  cat > testconf.c << EOF
  int main()
  {
    return cos( 42 );
  }
EOF
  if ! runcmd $CC -o testconf testconf.c
  then
    if runcmd $CC -o testconf testconf.c -lm
    then
      LDFLAGS="-lm $LDFLAGS"
    fi
  fi
  rm -f testconf*
}

lrintf_test()
{
  verbose lrintf_test
  cat > testconf.c << EOF
  int main()
  {
    return ( lrintf( 3.14 ) != 3 );
  }
EOF
  if runcmd $CC -o testconf testconf.c $LDFLAGS && runcmd ./testconf
  then
    CFLAGS="$CFLAGS -DHAVE_LRINTF"
  fi
  rm -f testconf*
}

gettext_test()
{
  verbose gettext_test
  cat > testconf.c <<EOF
  #include <libintl.h>
  int main()
  {
    gettext("");
  }
EOF

  if runcmd $CC $CFLAGS_GTK $LDFLAGS_GTK -o testconf testconf.c
  then
    rm -f testconf*
    return 0
  fi

  for intl_testdir in $PREFIX/include \
      /usr/local/include /usr/X11R6/include /usr/pkg/include
  do
    if runcmd $CC $CFLAGS_GTK -I$intl_testdir $LDFLAGS_GTK -o testconf testconf.c
    then
      CFLAGS_GTK="$CFLAGS_GTK -I$intl_testdir"
      rm -f testconf*
      return 0
    fi
  done
  rm -f testconf*
  return 1
}

gtk_test()
{
  verbose gtk_test
  if runcmd pkg-config gtk+-2.0
  then
    if runcmd pkg-config --exists gtk+-2.0 '>=' 2.6.0
    then
      cat > testconf.c << EOF
      #include <gtk/gtk.h>
      int main()
      {
        gtk_main();
      }
EOF
      if runcmd $CC `pkg-config gtk+-2.0 --cflags --libs` -o testconf testconf.c
      then
        CFLAGS_GTK=`pkg-config gtk+-2.0 --cflags`
        LDFLAGS_GTK=`pkg-config gtk+-2.0 --libs`
        if gettext_test
        then
          echo "yes"
          GTK=yes
          LOCALEDIR="$PREFIX/share/locale"
          CFLAGS_GTK="$CFLAGS_GTK -DLOCALEDIR=\\"\""$LOCALEDIR\\"\"
        else
          echo "no (could not find gettext libintl.h)"
          GTK=no
        fi
      else
        echo "no"
        GTK=no
      fi
      rm -f testconf*
    else
      echo "no (2.6.0 or later is required)"
      GTK=no
    fi
  else
    echo "no"
    GTK=no
  fi
}

#
# Parse options
#
while [ $# -ne 0 ]; do
  param=`expr "opt$1" : 'opt[^=]*=\(.*\)'`

  case "x$1" in
    x--disable-openssl|x--without-openssl)
      OPENSSL=no
      ;;
    x--disable-gtk|x--without-gtk)
      GTK=no
      ;;
    x--prefix=*)
      PREFIX="$param"
      ;;
    x--verbose)
      VERBOSE=yes
      ;;
    x--help)
      usage
      exit 0
      ;;
  esac
  shift
done

#
# System-specific flags
#
SYSTEM=`uname -s`
case $SYSTEM in
  BeOS)
    RELEASE=`uname -r`
    case $RELEASE in
      6.*|5.0.4) # Zeta or R5 / BONE beta 7
        ;;
      5.0*)       # R5 / net_server
        BEOS_NETSERVER=yes
        ;;
      *)
        echo "Unsupported BeOS version"
        exit 1 
        ;;
    esac
    GCCVER=`$CC -dumpversion`
    case $GCCVER in
      2.95.3*|3*|4*)
        ;;
      2.9*)
      	BEOS_OLDCC=yes
      	;;
      *)
        echo "Unsupported gcc version"
        exit 1
        ;;
    esac
    ;;

  Darwin)
    # Make sure the Universal SDK is installed
    if [ ! -d /Developer/SDKs/MacOSX10.4u.sdk ]; then
      cat << EOF
You need to install the Universal SDK in order to build Transmission:
  Get your Xcode CD or package
  Restart the install
  When it gets to "Installation Type", select "Customize"
  Select "Mac OS X 10.4 (Universal) SDK" under "Cross Development"
  Finish the install.
EOF
      exit 1
    fi
    PTHREAD=yes
    ;;

  FreeBSD|NetBSD|OpenBSD|Linux|skyos)
    PTHREAD=yes
    ;;

  CYGWIN*)
    ;;

  *)
    echo "Unsupported operating system"
    exit 1 ;;
esac
echo "System:  $SYSTEM"

#
# First things first, check to see if there's a compiler installed
#
cc_test

#
# OpenSSL settings
#
echo -n "OpenSSL: "
if [ "$OPENSSL" = no ]; then
  echo "disabled, using built-in SHA1 implementation"
else
  openssl_test
fi 
if [ "$OPENSSL" = no ] && [ "$BEOS_OLDCC" = yes ]; then
  echo "built-in SHA1 implementation not supported with your gcc version"
  echo "you must either install OpenSSL or gcc 2.95.3 or newer"
  exit 1
fi

#
# GTK+ settings
#
echo -n "GTK+:    "
if [ "x$GTK" = xno ]; then
  echo "disabled"
else
  gtk_test
  if [ "x$GTK" = xno ] && [ "x$SYSTEM" = xLinux ]; then
      cat <<EOF

*** GTK+ was not found, transmission-gtk will *NOT* be built. If you
*** wish to use the graphical version of Transmission then you will
*** need to install GTK+ headers and libraries. Try using your
*** distribution's package management utility to find a package called
*** libgtk2.0-dev, gtk2-devel, or something similar; then install it
*** and re-run this script.
EOF
  fi
fi

#
# Math functions
#
lm_test
lrintf_test

#
# Generate config.mk
#
rm -f mk/config.mk
cat > mk/config.mk << EOF
SYSTEM         = $SYSTEM
PREFIX         = $PREFIX
LOCALEDIR      = $LOCALEDIR
BEOS_NETSERVER = $BEOS_NETSERVER
PTHREAD        = $PTHREAD
OPENSSL        = $OPENSSL
GTK            = $GTK
CC             = $CC
CFLAGS         = $CFLAGS
CXX            = $CXX
CXXFLAGS       = $CXXFLAGS
LDFLAGS        = $LDFLAGS
CFLAGS_GTK     = $CFLAGS_GTK
LDFLAGS_GTK    = $LDFLAGS_GTK
EOF

echo
echo "Now use GNU make to build Transmission."
echo "It may be called 'make' or 'gmake' depending on your system."
