Index: code/branches/buildsystem3/cmake/FindENet.cmake
===================================================================
--- code/branches/buildsystem3/cmake/FindENet.cmake	(revision 2663)
+++ code/branches/buildsystem3/cmake/FindENet.cmake	(revision 2664)
@@ -3,7 +3,8 @@
 #
 #  ENET_FOUND - system has enet
-#  ENET_INCLUDE_DIR - the enet include directory
-#  ENET_LIBRARIES - the libraries needed to use enet
-#  ENET_DEFINITIONS - Compiler switches required for using enet
+#  ENet_INCLUDE_DIR - the enet include directory
+#  ENet_LIBRARY - the library needed to link against enet
+#
+# $ENETDIR is an environment variable used for finding enet.
 #
 #  Borrowed from The Mana World
@@ -11,55 +12,60 @@
 #
 # Several changes and additions by Fabian 'x3n' Landau
+# Lots of simplifications by Adrian Friedli and Reto Grieder
+# Version checking by Reto Grieder
 #                 > www.orxonox.net <
 
-IF (ENet_INCLUDE_DIR AND ENet_LIBRARY)
-   SET(ENet_FIND_QUIETLY TRUE)
-ENDIF (ENet_INCLUDE_DIR AND ENet_LIBRARY)
+INCLUDE(FindPackageHandleAdvancedArgs)
+INCLUDE(HandleLibraryTypes)
 
-FIND_PATH(ENet_INCLUDE_DIR enet/enet.h
-    /usr/include
-    /usr/local/include
-    /usr/pack/enet-2007-sd/include
-    ../libs/enet-1.1/include
-    ${DEPENDENCY_DIR}/enet-1.2/include
-    )
+FIND_PATH(ENET_INCLUDE_DIR enet/enet.h
+  PATHS $ENV{ENETDIR}
+  PATH_SUFFIXES include
+)
+FIND_LIBRARY(ENET_LIBRARY_OPTIMIZED
+  NAMES enet
+  PATHS $ENV{ENETDIR}
+  PATH_SUFFIXES lib
+)
+FIND_LIBRARY(ENET_LIBRARY_DEBUG
+  NAMES enetd enet_d enet_D
+  PATHS $ENV{ENETDIR}
+  PATH_SUFFIXES lib
+)
 
-FIND_LIBRARY(ENet_LIBRARY
-    NAMES enet
-    PATHS /usr/lib /usr/local/lib /usr/pack/enet-2007-sd/i686-debian-linux3.1/lib/
-    ../libs/enet-1.1
-    ${DEPENDENCY_DIR}/enet-1.2/lib
-    )
+# Try to determine the version. Note that enet only stores the major
+# version in the header file. So we check for existing functions.
+# Hence the this script only distinguishes between 1.0, 1.1 and 1.2
+FILE(STRINGS ${ENET_INCLUDE_DIR}/enet/enet.h _enet_header REGEX "ENET_")
+IF(_enet_header MATCHES "ENET_VERSION[ \t]*=[ \t]*1")
+  IF(_enet_header MATCHES "enet_socket_set_option")
+    SET(ENET_VERSION 1.2)
+  ELSEIF(_enet_header MATCHES "enet_peer_disconnect_later")
+    SET(ENET_VERSION 1.1)
+  ELSE()
+    SET(ENET_VERSION 1.0)
+  ENDIF()
+ELSE()
+  SET(ENET_VERSION 0) # Script doesn't support versions below 1.0
+ENDIF()
 
-SET(ENET_FOUND FALSE)
-IF (ENet_INCLUDE_DIR AND ENet_LIBRARY)
-    SET(ENET_FOUND TRUE)
-    SET(ENET_INCLUDE_DIR ${ENet_INCLUDE_DIR})
+# Handle the REQUIRED argument and set ENET_FOUND
+# Also check the the version requirements
+FIND_PACKAGE_HANDLE_ADVANCED_ARGS(ENet DEFAULT_MSG ${ENET_VERSION}
+  ENET_LIBRARY_OPTIMIZED
+  ENET_INCLUDE_DIR
+)
 
-    IF(WIN32)
-        SET(WINDOWS_ENET_DEPENDENCIES "ws2_32;winmm")
-        SET(ENet_LIBRARY ${ENet_LIBRARY} ${WINDOWS_ENET_DEPENDENCIES})
-    ENDIF(WIN32)
+# Collect optimized and debug libraries
+IF(NOT LINK_ENET_DYNAMIC AND WIN32)
+  # ENet is linked statically, hence we need to add some windows dependencies
+  HANDLE_LIBRARY_TYPES(ENET ws2_32 winmm)
+ELSE()
+  HANDLE_LIBRARY_TYPES(ENET)
+ENDIF()
 
-    SET(ENET_LIBRARIES ${ENet_LIBRARY})
-ENDIF (ENet_INCLUDE_DIR AND ENet_LIBRARY)
-
-IF (ENET_FOUND)
-    IF (NOT ENet_FIND_QUIETLY)
-        MESSAGE(STATUS "ENet was found.")
-        IF (VERBOSE_FIND)
-            MESSAGE (STATUS "  include path: ${ENet_INCLUDE_DIR}")
-            MESSAGE (STATUS "  library path: ${ENet_LIBRARY}")
-            MESSAGE (STATUS "  libraries:    enet")
-        ENDIF (VERBOSE_FIND)
-    ENDIF (NOT ENet_FIND_QUIETLY)
-ELSE (ENET_FOUND)
-    IF (NOT ENet_INCLUDE_DIR)
-        MESSAGE(SEND_ERROR "ENet include path was not found.")
-    ENDIF (NOT ENet_INCLUDE_DIR)
-    IF (NOT ENet_LIBRARY)
-        MESSAGE(SEND_ERROR "ENet library was not found.")
-    ENDIF (NOT ENet_LIBRARY)
-ENDIF (ENET_FOUND)
-
-MARK_AS_ADVANCED(ENet_INCLUDE_DIR ENet_LIBRARY)
+MARK_AS_ADVANCED(
+  ENET_INCLUDE_DIR
+  ENET_LIBRARY_OPTIMIZED
+  ENET_LIBRARY_DEBUG
+)
