--- wurfl.module.orig	2010-05-06 02:54:31.000000000 +0100
+++ wurfl.module	2010-06-22 16:41:24.000000000 +0100
@@ -28,21 +28,22 @@
 function wurfl_menu() {
    $items['admin/settings/wurfl'] = array(
     'title' => 'Wurfl', 
-    'page callback' =>  'wurfl_configuration',
-    'access arguments' => array('configure siruna'),
+    'page callback' =>  'drupal_get_form',
+    'page arguments' => array('wurfl_configuration_form'),
+    'access arguments' => array('configure wurfl'),
     'type' => MENU_NORMAL_ITEM,
     'description' => t('The WURFL module integrates Drupal with WURFL.')
   );
   $items['admin/settings/wurfl/configuration'] = array(
     'title' => 'WURFL configuration',
-    'access arguments' => array('configure siruna'),
+    'access arguments' => array('configure wurfl'),
     'type' => MENU_DEFAULT_LOCAL_TASK,
     'weight' => -10,
   );
   $items['admin/settings/wurfl/test'] = array(
     'title' => 'WURFL test',
     'page callback' => 'wurfl_test_page',
-    'access arguments' => array('configure siruna'),
+    'access arguments' => array('configure wurfl'),
     'type' => MENU_LOCAL_TASK,
     'weight' => 1,
   );  
@@ -57,51 +58,100 @@ function wurfl_perm() {
 } 
 
 /**
- * Initialisation of the $requestingDevice used to get to the
+ * Initialisation of the $requestingDevice used to access the site.
  */ 
 function wurfl_get_requestingDevice() {
-  require_once dirname(__FILE__) . '/lib/wurfl/WURFL/WURFLManagerProvider.php';
-  $wurflConfigFile = dirname(__FILE__) . "/lib/wurfl/resources/wurfl-config.xml";
-  $wurflManager = WURFL_WURFLManagerProvider::getWURFLManager($wurflConfigFile);	
-  $requestingDevice =  $wurflManager->getDeviceForHttpRequest($_SERVER);
+  global $wurflManager;
+  if ($wurflManager) {
+    $requestingDevice =  $wurflManager->getDeviceForHttpRequest($_SERVER);
+  }
   return $requestingDevice; 
 }
 
+
+/**
+* Implementation of hook_boot().
+**/
+function wurfl_boot() {
+  $wurfl_data_path = variable_get('wurfl_data_path', dirname(__FILE__) . '/lib/wurfl');
+  if (is_file($wurfl_data_path . '/WURFL/WURFLManagerProvider.php')) {
+    require_once $wurfl_data_path . '/WURFL/WURFLManagerProvider.php';
+    $wurflConfigFile = $wurfl_data_path . "/resources/wurfl-config.xml";
+    $wurflConfig = new WURFL_Configuration_XmlConfig($wurflConfigFile);
+    $wurflManagerFactory = new WURFL_WURFLManagerFactory($wurflConfig);
+    global $wurflManager;
+    $wurflManager = $wurflManagerFactory->create();
+  }
+}
+
+
+/**
+ * Function to return available capabilities for devices.
+ */
+function wurfl_get_capabilities() {
+  global $wurflManager;
+  if ($wurflManager) {
+    $groups =  $wurflManager->getListOfGroups();
+    foreach ($groups as $group) {
+      $capabilities[$group] = $wurflManager->getCapabilitiesNameForGroup($group);
+    }
+  }
+  return (array) $capabilities;
+}
+
+/**
+ * Function to return available groups for attributes.
+ */
+function wurfl_get_groups() {
+  global $wurflManager;
+  if ($wurflManager) {
+    $groups =  $wurflManager->getListOfGroups();
+  }
+  return (array) $groups;
+}
+
+
 function wurfl_test_page() {
-  
- 
-  $output .= t('Access this page with your mobile device, or use the !switcher module to test different user agents', array('!switcher' => l( 'Firefox Agent Switcher extension', 'https://addons.mozilla.org/en-US/firefox/addon/59')));
   $requestingDevice = wurfl_get_requestingDevice();
-  $output .= '<h2>' . t('Device information') . '</h2>';
-  $output .= t('This is a short collection of parameters that can be retrieved using WURFL');
-  $output .= 'Requesting Browser User Agent: <br/><b> ' . $_SERVER["HTTP_USER_AGENT"] . '</b>';
- 
-  $output .= '<h2>product_info</h2>';
-  $output .= '<ul>';
-  $output .= '<li>' . $requestingDevice->getCapability("is_wireless_device") . '</li>';
-  $output .= '<li>ID: ' . $requestingDevice->id  . '</li>';
-  $output .= '<li>Brand Name: ' . $requestingDevice->getCapability("brand_name")  . '</li>';
-  $output .= '<li>Model Name: ' . $requestingDevice->getCapability("model_name")  . '</li>';
-  $output .= '</ul>';
-  $output .= '<h2>display</h2>';
-  $output .= '<ul>';
-  $output .= '<li>Resolution Width: ' . $requestingDevice->getCapability("resolution_width")  . '</li>';
-  $output .= '<li>Resolution Height: ' . $requestingDevice->getCapability("resolution_height")  . '</li>';
-   $output .= '</ul>';
-  $output .= '<h2>Ajax</h2>';
+  if ($requestingDevice) {
+    $output .= t('Access this page with your mobile device, or use the !switcher module to test different user agents', array('!switcher' => l( 'Firefox Agent Switcher extension', 'https://addons.mozilla.org/en-US/firefox/addon/59')));
+    $output .= '<h2>' . t('Device information') . '</h2>';
+    $output .= t('This is a short collection of parameters that can be retrieved using WURFL');
+    $output .= 'Requesting Browser User Agent: <br/><b> ' . $_SERVER["HTTP_USER_AGENT"] . '</b>';
+   
+    $output .= '<h2>product_info</h2>';
+    $output .= '<ul>';
+    $output .= '<li>Is wireless device: ' . $requestingDevice->getCapability("is_wireless_device") . '</li>';
+    $output .= '<li>ID: ' . $requestingDevice->id  . '</li>';
+    $output .= '<li>Brand Name: ' . $requestingDevice->getCapability("brand_name")  . '</li>';
+    $output .= '<li>Model Name: ' . $requestingDevice->getCapability("model_name")  . '</li>';
+    $output .= '</ul>';
+    $output .= '<h2>display</h2>';
     $output .= '<ul>';
-  $output .= '<li>Ajax/Javascript support: ' . $requestingDevice->getCapability("ajax_support_javascript") . '</li>'; 
-  $output .= '<li>Mobile Browser: ' . $requestingDevice->getCapability("mobile_browser") . '</li>';
-  $output .= '</ul>';
-  $output .= '<h2>' . t('wml_ui') . '</h2>';
-  $output .= '<ul>';
-  $output .= '<li>Prefix for phonecall: ' . $requestingDevice->getCapability("wml_make_phone_call_string") . '</li>';
-  $output .= '</ul>';
-  $output .= '<h2>' . t('xhtml_ui') . '</h2>';
-  $output .= '<ul>';
-  $output .= '<li>Prefix for phonecall: ' . $requestingDevice->getCapability("wml_make_phone_call_string") . '</li>';
-  $output .= '<li>Xhtml Preferred Markup: ' . $requestingDevice->getCapability("preferred_markup")  . '</li>';
-  $output .= '</ul>';
+    $output .= '<li>Resolution Width: ' . $requestingDevice->getCapability("resolution_width")  . '</li>';
+    $output .= '<li>Resolution Height: ' . $requestingDevice->getCapability("resolution_height")  . '</li>';
+     $output .= '</ul>';
+    $output .= '<h2>Ajax</h2>';
+      $output .= '<ul>';
+    $output .= '<li>Ajax/Javascript support: ' . $requestingDevice->getCapability("ajax_support_javascript") . '</li>'; 
+    $output .= '<li>Mobile Browser: ' . $requestingDevice->getCapability("mobile_browser") . '</li>';
+    $output .= '</ul>';
+    $output .= '<h2>' . t('wml_ui') . '</h2>';
+    $output .= '<ul>';
+    $output .= '<li>Prefix for phonecall: ' . $requestingDevice->getCapability("wml_make_phone_call_string") . '</li>';
+    $output .= '</ul>';
+    $output .= '<h2>' . t('xhtml_ui') . '</h2>';
+    $output .= '<ul>';
+    $output .= '<li>Prefix for phonecall: ' . $requestingDevice->getCapability("wml_make_phone_call_string") . '</li>';
+    $output .= '<li>Xhtml Preferred Markup: ' . $requestingDevice->getCapability("preferred_markup")  . '</li>';
+    $output .= '</ul>';
+  }
+  else {
+    $wurfl_data_path = variable_get('wurfl_data_path', dirname(__FILE__) . '/lib/wurfl');
+    $output .= '<h2>' . t('Error') . '</h2>';
+    $output .= '<p>' . t('Could not initialise the WURFL database.  Check your configuration - is the path to the WURFL data correct?') . '</p>';
+    $output .= '<p>' . t('The module searches for, and may have failed to find, the following php file:') . '<br />' . $wurfl_data_path . '/WURFL/WURFLManagerProvider.php' . '</p>';
+  }
   return $output;
 }
 /**
@@ -109,16 +159,34 @@ function wurfl_test_page() {
  */ 
 function wurfl_is_mobile_device() {
   $requestingDevice = wurfl_get_requestingDevice();
-  $result = ($requestingDevice->getCapability("is_wireless_device") == 'true') ? 'mobile' : 'desktop'; // This is strange, return value is string, not boolean
+  if ($requestingDevice) {
+    $result = ($requestingDevice->getCapability("is_wireless_device") == 'true') ? 'mobile' : 'desktop'; // This is strange, return value is string, not boolean
+  }
   return $result;
 }
 
 
-function wurfl_configuration() {
-  $output .= t('No configuration possible yet. To test the module click on !test_module.', array('!test_module' => l('WURFL test','admin/settings/wurfl/test')));
-  $output .= '<br>';
-  $output .= t('Advanced configurations can be done in the wurfl-config.xml file, located in the resources folder of your WURFL project.');
-  return $output;
+function wurfl_configuration_form($form_state) {
+  $form['lib_path'] = array(
+    '#type' => 'textfield',
+    '#title' => t('Path to WURFL data'),
+    '#description' => t("Absolute path on this server to your installation of the WURFL database.  Module will look for directories called <b>resources</b> and <b>WURFL</b> using this path."),
+    '#default_value' => variable_get('wurfl_data_path', dirname(__FILE__) . '/lib/wurfl'),
+  );
+  $form['advanced'] = array(
+    '#type' => 'markup',
+    '#value' => '<p>' . t('Advanced configurations can be done in the wurfl-config.xml file, located in the resources folder of your WURFL project.') . '</p>',
+  );
+  $form['submit'] = array(
+    '#type' => 'submit',
+    '#value' => t('Save'),
+  );
+  return $form;
+}
+
+function wurfl_configuration_form_submit($form, &$form_state) {
+  variable_set('wurfl_data_path', $form_state['values']['lib_path']);
+  drupal_set_message(t('The settings have been saved.'));
 }
 
 /**
@@ -127,5 +195,7 @@ function wurfl_configuration() {
  */ 
 function wurfl_devicecapability($capability) {
   $requestingDevice = wurfl_get_requestingDevice();
-  return $requetingDevice->getCapability($capability);
+  if ($requestingDevice) {
+    return $requestingDevice->getCapability($capability);
+  }
 }
\ No newline at end of file
