function library_update_6201() {
  $items=array();

  if (module_exists('profile')) {
    $items[]=update_sql("INSERT INTO {profile_fields} (title, name, explanation, category, type, page, visibility) VALUES ('Last name','profile_library_name_last','Imported from the patron module.', 'Library', 'textfield', '', 1)");
    $items[]=update_sql("INSERT INTO {profile_fields} (title, name, explanation, category, type, page, visibility) VALUES ('First name','profile_library_name_first','Imported from the patron module.', 'Library', 'textfield', '', 1)");
    $items[]=update_sql("INSERT INTO {profile_fields} (title, name, explanation, category, type, page, visibility) VALUES ('Barcode','profile_library_barcode', 'Imported from the paton module.', 'Library', 'textfield', '', 1)");
    if (variable_get('patron_is_user', FALSE)) {
      $items[]=update_sql("INSERT INTO {profile_fields} (title, name, explanation, category, type, page, visibility) VALUES ('ID','profile_library_id', 'Imported from the patron module.', 'Library', 'textfield', '', 1)");
    }
  }
  
  if (module_exists('content_profile')) {
    //this is hard  
  }
  
  return $items;
}

function library_update_6202() {
  $fin=array();

  if (db_column_exists('library_transactions','uid')) {
    return $fin;
  }
  
  cache_clear_all();
  menu_rebuild();

  $list=db_query("SELECT * FROM {library_patrons} p JOIN {node} n ON p.nid=n.nid WHERE uid IS NOT NULL");
  while ($patron=db_fetch_array($list)) {
    if (module_exists('profile')) {
      $u=user_load($patron['uid']); 
      $data=array(
        'profile_library_name_last' => $patron['name_last'],
        'profile_library_name_first' => $patron['name_first'],
        'profile_library_barcode' => $patron['barcode'],
        'profile_library_id' => $patron['title'],
      );
      $n=user_save($u,$data,'Library');
      if (!$n) 
        watchdog('Library','Patron conversion of @name failed', array('@name' => $name));
    }
    else if (module_exists('content_profile')) {
      $node=content_profile_load('profile', $patron['uid']);
      if ($node) {
        $node->field_library_name_last[0]['value']=$patron['name_last'];
        $node->field_library_name_first[0]['value']=$patron['name_first'];
        $node->field_library_barcode[0]['value']=$patron['barcode']; //make optional?
        $node->field_library_id[0]['value']=$patron['id']; //make optional?
        node_save($node);
      }
      else {  
        $node=new stdClass();    
        $node->title=$patron['title'];
        $node->body=''; 
        $node->type='profile'; //default name, adjust if you changed yours
        $node->created=$patron['created'];
        $node->uid=$patron['uid'];
        $node->field_library_name_last[0]['value']=$patron['name_last'];
        $node->field_library_name_first[0]['value']=$patron['name_first'];
        $node->field_library_barcode[0]['value']=$patron['barcode']; //make optional?
        $node->field_library_id[0]['value']=$patron['id']; //make optional?
        if ($patron['disabled']==0)
          $status=1;
        else
          $status=0;
        $node->status=$status;
        node_save($node);
      }
    }
  }
  
  $list=db_query("SELECT * FROM {library_patrons} p JOIN {node} n ON p.nid=n.nid WHERE uid IS NULL");
  while ($patron=db_fetch_array($list)) {
    if ($patron['disabled']==0)
      $status=1;
    else
      $status=0;
    $data=array(
      'name'    => $patron['title'], 
      'mail'    => $patron['email'], 
      'init'    => $patron['email'], 
      'created' => $patron['created'],
      'status'  => $status, 
    ); 
    if (module_exists('profile')) {
      $data['profile_library_name_last']  = $patron['name_last'],
      $data['profile_library_name_first'] = $patron['name_first'],
      $data['profile_library_barcode']    = $patron['barcode'],
    }
    $u=user_save(NULL,$data,'Library');
    if (!$u) 
      watchdog('Library','Patron conversion of @name failed', array('@name' => $name));
    if (module_exists('content_profile')) {
      $node=new stdClass;   
      $node->title=$patron['title'];
      $node->body=''; 
      $node->type='profile'; //default name, adjust if you changed yours
      $node->created=$patron['created'];
      $node->uid=$patron['uid'];
      $node->field_library_name_last[0]['value']=$patron['name_last'];
      $node->field_library_name_first[0]['value']=$patron['name_first'];
      $node->field_library_barcode[0]['value']=$patron['barcode']; //make optional?
      $node->field_library_id[0]['value']=$patron['id']; //make optional? / relevant here?
      node_save($node);
    }
  }
  return $fin;
}

function library_update_6203() {
  $items = array();
  $items[] = update_sql("UPDATE {library_transactions} lt JOIN {library_patrons} lp on lt.patron_id=lp.nid SET lt.patron_id=lp.patron_uid");
  $items[] = update_sql("ALTER TABLE {library_transactions} CHANGE patron_id  uid int(10)");
  return $items;
}

function library_update_6204() {
//Copied from patron/patron.install and added dropping the library_patrons table
  variable_del('patron_is_user');
  variable_del('patron_use_barcodes');
  variable_del('patron_autocreate');

  $result = db_query("SELECT nid FROM {node} WHERE type = 'patron'");

  if ($result) {
    while ($row = db_fetch_object($result)) {
      set_time_limit(5);
      node_delete($row->nid);
    }
  }

//  patron code not present uninstall by hand
//  drupal_uninstall_schema('patron');
//  db_query("DROP TABLE {library_patrons}");

  // Clear the cache tables.
  cache_clear_all(null, 'cache');
  cache_clear_all(null, 'cache_filter');
  cache_clear_all(null, 'cache_menu');
  cache_clear_all(null, 'cache_page');

  drupal_set_message(t('Library patron module successfully uninstalled'));
  return array();
}

