#!/bin/bash
#-----------------------------

# Settings
#---------------------------
DEBUG=0
DBUS=1
BUILDIN_ICONS=1
HELP=0
ASF=1
MP4=1
NLS=1
TAGLIB_EXTRAS=0
TAGLIB_16=0
OLD_REMOTE=0
HAS_XDG_DESKTOP_MENU=1
HAS_XDG_ICON_RESOURCE=1
ICON_DIRECTORY="${ICON_DIRECTORY:-/usr/share/icons}"
ICON_THEME=${ICON_THEME:-Tango}
PREFIX=${PREFIX:-/usr}

# Default Compiler Settings
# Use environment variables to override
#--------------------------
CXX=${CXX:-g++}
LINK=${LINK:-g++}
INCFLAGS=${INCFLAGS:-}
LIBS=${LIBS:-}
LIBPREFIX=${LIBPREFIX:-lib}
LIBEXT=${LIBEXT:-.so}
OUTPUTOBJ=${OUTPUTOBJ:--o }
OUTPUTBIN=${OUTPUTBIN:--o }
OBJEXT=${OBJEXT:-.o}

# Overriding Compiler Flags
# Either override GEN_CFLAGS, OPT_CFLAGS and DEBUG_CFLAGS individually
# or set CFLAGS which overrides all them
#-------------------------------------------------------
GEN_CFLAGS=${GEN_CFLAGS:--Wall -Wextra -Wstrict-aliasing -Wformat=2 -pipe}
OPT_CFLAGS=${OPT_CFLAGS:--O3 }
DEBUG_CFLAGS=${DEBUG_CFLAGS:--g}

# Overriding Linker Flags
# Either override GEN_LDFLAGS, OPT_LDFLAGS and DEBUG_LDFLAGS individually
# or set LDFLAGS which overrides all them
#-------------------------------------------------------
GEN_LDFLAGS=${GEN_LDFLAGS:-}
OPT_LDFLAGS=${OPT_LDFLAGS:--s }
DEBUG_LDFLAGS=${DEBUG_LDFLAGS:-}


# Parse Command Line Arguments
#-----------------------------
for arg in $*; do
  case $arg in
    --enable-debug*)
      DEBUG=1
      ;;
    --with-taglib-extras*)
      TAGLIB_EXTRAS=1
      ;;
    --disable-nls*)
      NLS=0
      ;;
    --without-asf*)
      ASF=0
      ;;
    --without-mp4*)
      MP4=0
      ;;
    --without-dbus*)
      DBUS=0
      ;;
    --without-new-remote*)
      OLD_REMOTE=1
      ;;
    --prefix=*)
      PREFIX=`echo "${arg}" | cut -d= -f2`
      ;;
    --fox-prefix=*)
      FOX_PREFIX=`echo "${arg}" | cut -d= -f2`
      ;;
    --xine-prefix=*)
      XINE_PREFIX=`echo "${arg}" | cut -d= -f2`
      ;;
    --sqlite-prefix=*)
      SQLITE_PREFIX=`echo "${arg}" | cut -d= -f2`
      ;;
    --taglib-prefix=*)
      TAGLIB_PREFIX=`echo "${arg}" | cut -d= -f2`
      ;;
    --taglib-extras-prefix=*)
      TAGLIB_EXTRAS_PREFIX=`echo "${arg}" | cut -d= -f2`
      ;;
    --dbus-prefix=*)
      DBUS_PREFIX=`echo "${arg}" | cut -d= -f2`
      ;;
    --external-icons*)
      BUILDIN_ICONS=0
      ;;
    --external-icon-directory=*)
      BUILDIN_ICONS=0
      ICON_DIRECTORY=`echo "${arg}" | cut -d= -f2`
      ;;
    --external-icon-theme=*)
      BUILDIN_ICONS=0
      ICON_THEME=`echo "${arg}" | cut -d= -f2`
      ;;
    --localedir=*)
      NLSDIR=`echo "${arg}" | cut -d= -f2`
      ;;
    --help)
      HELP=1
      ;;
    -h)
      HELP=1
      ;;
  esac
done


# Print Command Line Options
#---------------------------
if [[ $HELP -eq 1 ]] ; then
  echo "Available options:"
  echo "   --enable-debug                  Compile with debug information"
  echo "   --with-taglib-extras            Use taglib-extras library for ASF and MP4 support."
  echo "   --without-dbus                  Turn off DBus Support"
  echo "   --without-asf                   Turn off ASF Support"
  echo "   --without-mp4                   Turn off MP4 Support"
  echo "   --without-new-remote            Disable new remote and use old mini player."
  echo "   --prefix=<p>                	   Installation prefix (/usr)"
  echo "   --fox-prefix=<p>                FOX prefix path"
  echo "   --xine-prefix=<p>               XINE prefix path"
  echo "   --taglib-prefix=<p>             TagLib prefix path"
  echo "   --taglib-extras-prefix=<p>      TagLib Extras prefix path"
  echo "   --sqlite-prefix=<p>             Sqlite prefix path"
  echo "   --dbus-prefix=<p>               Dbus prefix path"
  echo "   --external-icons                Turn off buildin icons"
  echo "   --external-icon-directory=<dir> Override icon directory (/usr/share/icons)."
  echo "   --external-icon-theme=<theme>   Override icon theme (Tango)."
  echo "   --localedir=<dir>               locale-dependent data (<prefix>/share/locale)"
  echo "   -h,--help                       Show Help"
  exit 0
fi


# Debug / Release Mode
#----------------------
if [[ $DEBUG -eq 1 ]] ; then
  CFLAGS=${CFLAGS:-$GEN_CFLAGS $DEBUG_CFLAGS}
  LDFLAGS=${LDFLAGS:-$GEN_LDFLAGS $DEBUG_LDFLAGS}
  DEFINES+="-DDEBUG "
else
  CFLAGS=${CFLAGS:-$GEN_CFLAGS $OPT_CFLAGS}
  LDFLAGS=${LDFLAGS:-$GEN_LDFLAGS $OPT_LDFLAGS}
  DEFINES+="-DNDEBUG "
fi

# Check for generic config in prefix
#-----------------------------------
check_in_prefix()
{
  echo "    Search for $1 in $2 ... "

  if [[ ! -x $2/bin/$3 ]] ; then
    return 0
  fi

  echo "        Check $1 Config => FOUND"

  CONFIG_LIB=`$2/bin/$3 --libs`
  CONFIG_INCLUDE=`$2/bin/$3 --cflags`
  CONFIG_VERSION=`$2/bin/$3 --version`
  CONFIG_MAJOR=`echo "${CONFIG_VERSION}" | cut -d. -f1`
  CONFIG_MINOR=`echo "${CONFIG_VERSION}" | cut -d. -f2`
  CONFIG_RELEASE=`echo "${CONFIG_VERSION}" | cut -d. -f3`
  export CONFIG_PREFIX=$2

  INCFLAGS="${INCFLAGS} ${CONFIG_INCLUDE} "
  LIBS="${LIBS} ${CONFIG_LIB} "


  # Make sure it is a compatible version
  #--------------------------------------------
  if [[ $CONFIG_MAJOR -lt $4 ]] ; then
      echo "        Check $1 Version => Unsupported ($CONFIG_MAJOR.$CONFIG_MINOR.$CONFIG_RELEASE)"
      return 0
  fi

  if [[ $CONFIG_MINOR -lt $5 ]] ; then
      echo "        Check $1 Version => Unsupported ($CONFIG_MAJOR.$CONFIG_MINOR.$CONFIG_RELEASE)"
      return 0
  fi

  echo "        Check $1 Version => ${CONFIG_VERSION}"
  echo ""
  return 1
}


# Check for package using pkg-config
#-----------------------------------
check_required_pkg()
{
  export PKG_CONFIG_PATH="$2/lib/pkgconfig:$2/lib64/pkgconfig:$2/share/pkgconfig"

  echo "    Search for $1 >= $3 in $2"

#  echo "pkg-config --exists \"$1 >= $3\""
#  RUN=`pkg-config --exists '$1 >= $3'`
  pkg-config --exists "$1 >= $3"
  if [[ "$?" -ne "0" ]] ; then
    echo "\"$?\""
    return 0
  fi

  PKG_VERSION=`pkg-config --modversion $1`
  echo "        Check $1 Version => $PKG_VERSION"

  PKG_CFLAGS=`pkg-config --cflags-only-other $1`
  PKG_INCFLAGS=`pkg-config --cflags-only-I $1`
  PKG_LDFLAGS=`pkg-config --libs-only-L $1`
  PKG_LIBS=`pkg-config --libs-only-l $1`
  PKG_LIBS_OTHER=`pkg-config --libs-only-other $1`

  if [[ -n $PKG_CFLAGS ]] ; then
    CFLAGS+="$PKG_CFLAGS "
  fi

  if [[ -n $PKG_INCFLAGS ]] ; then
    INCFLAGS+="$PKG_INCFLAGS "
  fi

  if [[ -n $PKG_LDFLAGS ]] ; then
    LDFLAGS+="$PKG_LDFLAGS "
  fi

  if [[ -n $PKG_LIBS ]] ; then
    LIBS+=" $PKG_LIBS "
  fi

  if [[ -n $PKG_LIBS_OTHER ]] ; then
    LIBS+=" $PKG_LIBS_OTHER "
  fi

  echo ""
  return 1
}


# Check for the FOX library
#--------------------------
check_fox()
{
  # Gentoo uses a modified fox-config which requires the wanted fox version to be set, otherwise
  # things don't work. We'll just set it to the current stable version of FOX.
  # User may override this variable if needed.
  export WANT_FOX="${WANT_FOX:-1.6}"

  MAJOR=1
  MINOR=6

  if [[ -n "$FOX_PREFIX" ]] ; then
    check_in_prefix "FOX" $FOX_PREFIX "fox-config" $MAJOR $MINOR
    if [[ $? -eq 0 ]] ; then
      echo " Unable to find a compatible FOX library installation. Please make"
      echo " sure the correct version is installed including the header files."
      echo " You can use the \"--fox-prefix\" option to search in an"
      echo " alternative installation directory."
      exit -1
    fi
  else
    check_in_prefix "FOX" "/usr" "fox-config" $MAJOR $MINOR
    if [[ $? -eq 0 ]] ; then
      check_in_prefix "FOX" "/usr/local" "fox-config" $MAJOR $MINOR
      if [[ $? -eq 0 ]] ; then
          echo " Unable to find a compatible FOX library installation. Please make"
          echo " sure the correct version is installed including the header files."
          echo " You can use the \"--fox-prefix\" option to search in an"
          echo " alternative installation directory."
          exit -1
      fi
    fi
  fi

  # Configure Reswrap
  #------------------
  RESCMD=`${CONFIG_PREFIX}/bin/reswrap -v 2>&1`
  RESVERSION=`echo ${RESCMD} | cut -d" " -f2`
  RESWRAP_MAJOR=`echo ${RESVERSION} | cut -d. -f1`

  if [[ -d icons ]] ; then
    if [[ $RESWRAP_MAJOR = "5" ]] ; then
      RESWRAP_H="${CONFIG_PREFIX}/bin/reswrap --keep-ext --header"
      RESWRAP_CPP="${CONFIG_PREFIX}/bin/reswrap --keep-ext --source --extern"
      RESWRAP_TEXT="${CONFIG_PREFIX}/bin/reswrap -t --keep-ext"
    else
      RESWRAP_H="${CONFIG_PREFIX}/bin/reswrap -i -k"
      RESWRAP_CPP="${CONFIG_PREFIX}/bin/reswrap -e -k"
      RESWRAP_TEXT="${CONFIG_PREFIX}/bin/reswrap -t -k"
    fi
  fi

}

# Check for the Xine library
#---------------------------
check_xine()
{

  MAJOR=1
  MINOR=0

  if [[ -n "$XINE_PREFIX" ]] ; then
    export PKG_CONFIG_PATH="$XINE_PREFIX/lib/pkgconfig:$XINE_PREFIX/lib64/pkgconfig:$XINE_PREFIX/share/pkgconfig"
    check_in_prefix "XINE" $XINE_PREFIX "xine-config" $MAJOR $MINOR
    if [[ $? -eq 0 ]] ; then
      echo " Unable to find a compatible XINE library installation. Please make"
      echo " sure the correct version is installed including the header files."
      echo " You can use the \"--xine-prefix\" option to search in an"
      echo " alternative installation directory."
      exit -1
    fi
  else
    export PKG_CONFIG_PATH="/usr/lib/pkgconfig:/usr/lib64/pkgconfig:/usr/share/pkgconfig"
    check_in_prefix "XINE" "/usr" "xine-config" $MAJOR $MINOR
    if [[ $? -eq 0 ]] ; then
      export PKG_CONFIG_PATH="/usr/local/lib/pkgconfig:/usr/local/lib64/pkgconfig:/usr/local/share/pkgconfig"
      check_in_prefix "XINE" "/usr/local" "xine-config" $MAJOR $MINOR
      if [[ $? -eq 0 ]] ; then
          echo " Unable to find a compatible XINE library installation. Please make"
          echo " sure the correct version is installed including the header files."
          echo " You can use the \"--xine-prefix\" option to search in an"
          echo " alternative installation directory."
          exit -1
      fi
    fi
  fi
}

# Check for the Dbus library
#---------------------------
check_dbus()
{
  if [[ $DBUS -eq 1 ]] ; then
    DEFINES+="-DHAVE_DBUS "
    if [[ -n "$DBUS_PREFIX" ]] ; then
      check_required_pkg dbus-1 $DBUS_PREFIX "1.0"
      if [[ $? -eq 0 ]] ; then
        echo " Unable to find a compatible DBUS library installation. Please make"
        echo " sure the correct version is installed including the header files."
        echo " You can use the \"--dbus-prefix\" option to search in an"
        echo " alternative installation directory."
        DBUS=0
        return 0
      fi
    else
      check_required_pkg dbus-1 "/usr" "1.0"
      if [[ $? -eq 0 ]] ; then
        check_required_pkg dbus-1 "/usr/local" "1.0"
        if [[ $? -eq 0 ]] ; then
            echo " Unable to find a compatible DBUS library installation. Please make"
            echo " sure the correct version is installed including the header files."
            echo " You can use the \"--dbus-prefix\" option to search in an"
            echo " alternative installation directory."
		        DBUS=0
            return 0
        fi
      fi
    fi
  fi
}



check_sqlite()
{
  if [[ -n "$SQLITE_PREFIX" ]] ; then
    check_required_pkg sqlite3 $SQLITE_PREFIX "3.4"
    if [[ $? -eq 0 ]] ; then
      echo " Unable to find a compatible SQLITE library installation. Please make"
      echo " sure the correct version is installed including the header files."
      echo " You can use the \"--sqlite-prefix\" option to search in an"
      echo " alternative installation directory."
      exit -1
    fi
  else
    check_required_pkg sqlite3 "/usr" "3.4"
    if [[ $? -eq 0 ]] ; then
      check_required_pkg sqlite3 "/usr/local" "3.4"
      if [[ $? -eq 0 ]] ; then
          echo " Unable to find a compatible SQLITE library installation. Please make"
          echo " sure the correct version is installed including the header files."
          echo " You can use the \"--sqlite-prefix\" option to search in an"
          echo " alternative installation directory."
          exit -1
      fi
    fi
  fi
}



check_taglib_16()
{
  if [[ -n "$TAGLIB_PREFIX" ]] ; then
    check_required_pkg taglib $TAGLIB_PREFIX "1.6"
    if [[ $? -eq 0 ]] ; then
      TAGLIB_16=0
      return
    fi
  else
    check_required_pkg taglib "/usr" "1.6"
    if [[ $? -eq 0 ]] ; then
      check_required_pkg taglib "/usr/local" "1.6"
      if [[ $? -eq 0 ]] ; then
        TAGLIB_16=0
        return
      fi
    fi
  fi
  TAGLIB_16=1
}


check_taglib()
{
  if [[ -n "$TAGLIB_PREFIX" ]] ; then
    check_required_pkg taglib $TAGLIB_PREFIX "1.4"
    if [[ $? -eq 0 ]] ; then
      echo " Unable to find a compatible TAGLIB library installation. Please make"
      echo " sure the correct version is installed including the header files."
      echo " You can use the \"--taglib-prefix\" option to search in an"
      echo " alternative installation directory."
      exit -1
    fi
  else
    check_required_pkg taglib "/usr" "1.4"
    if [[ $? -eq 0 ]] ; then
      check_required_pkg taglib "/usr/local" "1.4"
      if [[ $? -eq 0 ]] ; then
          echo " Unable to find a compatible TAGLIB library installation. Please make"
          echo " sure the correct version is installed including the header files."
          echo " You can use the \"--taglib-prefix\" option to search in an"
          echo " alternative installation directory."
          exit -1
      fi
    fi
  fi
}


check_taglib_extras()
{
  if [[ -n "$TAGLIB_EXTRAS_PREFIX" ]] ; then
    check_required_pkg taglib-extras $TAGLIB_EXTRAS_PREFIX "0.1"
    if [[ $? -eq 0 ]] ; then
      echo " Unable to find a compatible TAGLIB-EXTRAS library installation. Please make"
      echo " sure the correct version is installed including the header files."
      echo " You can use the \"--taglib-extras-prefix\" option to search in an"
      echo " alternative installation directory."
      exit -1
    fi
  else
    check_required_pkg taglib-extras "/usr" "0.1"
    if [[ $? -eq 0 ]] ; then
      check_required_pkg taglib-extras "/usr/local" "0.1"
      if [[ $? -eq 0 ]] ; then
          echo " Unable to find a compatible TAGLIB-EXTRAS library installation. Please make"
          echo " sure the correct version is installed including the header files."
          echo " You can use the \"--taglib-extras-prefix\" option to search in an"
          echo " alternative installation directory."
          exit -1
      fi
    fi
  fi
}


# Which icons to build in
#---------------------------
check_icons()
{
  if [[ $BUILDIN_ICONS -eq 0 ]] && [[ -d "$ICON_DIRECTORY" ]] ; then
    DEFINES+="-DHAVE_EXTERNAL_ICONS -DDEFAULT_ICON_PATH=\"${ICON_DIRECTORY}/${ICON_THEME}\" "
  else
    BUILDIN_ICONS=1
    DEFINES+="-DHAVE_INTERNAL_ICONS "
  fi
}

check_remote()
{
  if [[ $OLD_REMOTE -eq 1 ]] ; then
    DEFINES+="-DHAVE_COMPACT_REMOTE "
  fi
}

# Find Libraries
#------------------------
echo ""
echo "Goggles Music Manager:"
check_fox
check_xine
check_dbus
check_taglib_16
if [[ $TAGLIB_16 -eq 1 ]] ; then
  ASF=0
  MP4=0
  DEFINES+="-DHAVE_TAGLIB_16 "
else
  check_taglib
  if [[ $TAGLIB_EXTRAS -eq 1 ]] ; then
    check_taglib_extras
    DEFINES+="-DHAVE_TAGLIB_EXTRAS "
    ASF=0
    MP4=0
  fi
fi
check_sqlite
check_remote

# Check Icons
#------------------------
check_icons

# Byte Order Detection
#------------------------
. build/byteorderdetect

# Features
#------------------------
if [[ $TAGLIB_EXTRAS -eq 0 ]] && [[ $TAGLIB_16 -eq 0 ]] ; then
  if [[ $ASF -eq 0 ]] ; then
    DEFINES+="-DDISABLE_ASF_SUPPORT "
  fi
  if [[ $MP4 -eq 0 ]] ; then
    DEFINES+="-DDISABLE_MP4_SUPPORT "
  fi
fi

if [[ $NLS -eq 1 ]] ; then
  NLSDIR=${NLSDIR:-$PREFIX/share/locale}
  DEFINES+="-DHAVE_NLS -DLOCALEDIR=\"${NLSDIR}\" "
fi

# Summary
#-------------------------
echo "Features:"
if [[ $DBUS -eq 0 ]] ; then
  echo "    DBUS Support => no"
else
  echo "    DBUS Support => yes"
fi

if [[ $TAGLIB_EXTRAS -eq 1 ]] ; then
  echo "    Taglib-Extras => yes"
  echo "    MP4 Support => yes"
  echo "    ASF Support => yes"
elif [[ $TAGLIB_16 -eq 1 ]] ; then
  echo "    Taglib 1.6 => yes"
  echo "    Taglib-Extras => no"
  echo "    MP4 / ASF supported if buildin into TagLib."
else
  echo "    Taglib-Extras => no"
  if [[ $ASF -eq 0 ]] ; then
    echo "    ASF Support => no"
  else
    echo "    ASF Support => yes"
  fi
  if [[ $MP4 -eq 0 ]] ; then
    echo "    MP4 Support => no"
  else
    echo "    MP4 Support => yes"
  fi
fi


if [[ $NLS -eq 0 ]] ; then
  echo "    NLS Support => no"
else
  echo "    NLS Support => yes"
  echo "    Locale Directory => $NLSDIR"
fi
if [[ $OLD_REMOTE -eq 1 ]] ; then
  echo "    Old Remote => yes"
else
  echo "    New Remote => yes"
fi
if [[ $BUILDIN_ICONS -eq 0 ]] ; then
  echo "    Icons => external"
  echo "    Icon Path => $ICON_DIRECTORY/$ICON_THEME"
else
  echo "    Icons => buildin"
fi
echo "    Install Path => $PREFIX"

# Write out configuration
#--------------------------------------
echo "#Generated by configure utility"  > config.make
echo "CXX=$CXX"                        >> config.make
echo "LINK=$LINK"                      >> config.make
echo "CFLAGS=$CFLAGS"                  >> config.make
echo "CPPFLAGS=$DEFINES $INCFLAGS"     >> config.make
echo "LDFLAGS=$LDFLAGS"                >> config.make
echo "LIBS=$LIBS"                      >> config.make
echo "OUTPUTOBJ=$OUTPUTOBJ"            >> config.make
echo "OUTPUTBIN=$OUTPUTBIN"            >> config.make
echo "OBJEXT=$OBJEXT"                  >> config.make
echo "RESWRAP_CPP=$RESWRAP_CPP"        >> config.make
echo "RESWRAP_H=$RESWRAP_H"            >> config.make
echo "RESWRAP_TEXT=$RESWRAP_TEXT"      >> config.make
echo "MP4=$MP4"                        >> config.make
echo "ASF=$ASF"                        >> config.make
echo "NLS=$NLS"                        >> config.make
echo "DBUS=$DBUS"                      >> config.make
echo "BUILDIN_ICONS=$BUILDIN_ICONS"    >> config.make
echo "OLD_REMOTE=$OLD_REMOTE"          >> config.make
echo "PREFIX=$PREFIX"                  >> config.make
echo "LOCALEDIR=$NLSDIR"               >> config.make

# Success
#--------------------------------------
echo "Done."
echo "Please run \"make\" and \"make install\" to compile and install GMM."
exit 0

