--- schoolserver.py.orig	2009-08-03 13:51:45.684391809 +0800
+++ schoolserver.py	2009-07-29 15:27:02.278125458 +0800
@@ -1,3 +1,19 @@
+# Copyright (C) 2007, 2008 One Laptop Per Child
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+
 import logging
 from gettext import gettext as _
 from xmlrpclib import ServerProxy, Error
@@ -5,26 +21,126 @@
 import os
 import gconf
 
+import string, random, time
+import subprocess
+
+import time
+import datetime
+
 from sugar.profile import get_profile
 
-REGISTER_URL = 'http://schoolserver:8080/'
+# HAM : SoaS
+# (ham@solutiongrove.com)
+# defs to enable registration on SoaS
+
+# use whatever is specified in the jabber server field for REGISTER_URL
+# REGISTER_URL = 'http://schoolserver:8080/'
+client = gconf.client_get_default()
+JABBER_SERVER = client.get_string('/desktop/sugar/collaboration/jabber_server')
+REGISTER_URL = 'http://'+JABBER_SERVER+':8080/'
+
+# utilities ********************************************************************
+
+def getEpoch():
+  t = datetime.datetime.now()
+  e = time.mktime(t.timetuple())  
+  return e
+
+def getMacAddress():
+  for line in os.popen("/sbin/ifconfig"):
+    if line.find('Ether') > -1:
+      mac = line.split()[4]
+      break
+  return mac 
+
+# generates the serial and uuid ************************************************
+
+# to create a unique serial
+# - we randomly get 3 letters
+# - concat the above with the last 8 numbers from epoch seconds
+
+def gen_soas_serial():
+  s1 = ''.join([random.choice(string.ascii_uppercase) for y in range(3)])
+  s2 = str(int(getEpoch()))[-8:]
+  serial = s1 + s2 
+  return serial
+
+# to create a unique uuid
+# - create uuid using random hexdigits up to 40 characters
+
+def gen_soas_uuid():
+  u1_count = 40 
+  uuid = ''.join([random.choice(string.hexdigits + '-') for y in range(int(u1_count))])
+  return uuid
+
+# write serial and uuid to file ************************************************
+
+def write_soas_info(sn,uuid,backup_url):
+  # we presume that there is a /home/liveuser/.sugar directory
+  soas_dir = '/home/liveuser/.sugar/soas/'
+  # create the directory where we will put the files
+  if not os.path.exists(soas_dir):
+    os.mkdir(soas_dir)
+  # check if a serial file exists, it might be from a failed registration
+  # let's delete it first
+  if os.path.exists(os.path.join(soas_dir, 'sn')):
+    os.remove(os.path.join(soas_dir, 'sn'))
+  # write the serial into a file
+  serial_file = open(os.path.join(soas_dir, 'sn'),'w')
+  serial_file.write(sn)
+  serial_file.close()
+  # check if a uuid file exists, it might be from a failed registration
+  # let's delete it first
+  if os.path.exists(os.path.join(soas_dir, 'uuid')):
+    os.remove(os.path.join(soas_dir, 'uuid'))
+  # write the uuid into a file
+  uuid_file = open(os.path.join(soas_dir, 'uuid'),'w')
+  uuid_file.write(uuid)
+  uuid_file.close()
+  if os.path.exists(os.path.join(soas_dir, 'backup_url')):
+    os.remove(os.path.join(soas_dir, 'backup_url'))
+  # write the backup_url to a file
+  bu_file = open(os.path.join(soas_dir, 'backup_url'),'w')
+  bu_file.write(backup_url)
+  bu_file.close()
+
+# ******************************************************************************
 
 class RegisterError(Exception):
     pass
 
 def register_laptop(url=REGISTER_URL):
-    if not have_ofw_tree():
-        logging.error('Registration: Cannot obtain data needed to register.')
-        raise RegisterError(_('Cannot obtain data needed for registration.'))
-
-    sn = read_ofw('mfg-data/SN')
-    uuid = read_ofw('mfg-data/U#')
-    sn = sn or 'SHF00000000'
-    uuid = uuid or '00000000-0000-0000-0000-000000000000'
 
     profile = get_profile()
 
     client = gconf.client_get_default()
+    JABBER_SERVER = client.get_string('/desktop/sugar/collaboration/jabber_server')
+
+    if not have_ofw_tree():
+        # logging.error('Registration: Cannot obtain data needed to register.')
+        # raise RegisterError(_('Cannot obtain data needed for registration.'))
+
+        # HAM : SoaS 
+        # if we did not find an ofw directory
+        # let's generate the serial and uuid from the current time and mac address
+        # save the generated serial and uuid into gconf
+
+        sn = gen_soas_serial()
+        uuid = gen_soas_uuid()
+
+        # we need to save these for later use
+        # we need them to work with backups
+        # client.set_string('/desktop/sugar/soas_serial', sn)
+        # client.set_string('/desktop/sugar/soas_uuid', uuid)
+
+        write_soas_info(sn,uuid,JABBER_SERVER)
+
+    else:
+        sn = read_ofw('mfg-data/SN')
+        uuid = read_ofw('mfg-data/U#')
+        sn = sn or 'SHF00000000'
+        uuid = uuid or '00000000-0000-0000-0000-000000000000'
+
     nick = client.get_string('/desktop/sugar/user/nick')
 
     server = ServerProxy(url)
@@ -56,4 +172,3 @@
     data = fh.read().rstrip('\0\n')
     fh.close()
     return data
-
