Index: /trunk/pxeconfig/changelog
===================================================================
--- /trunk/pxeconfig/changelog	(revision 18)
+++ /trunk/pxeconfig/changelog	(revision 19)
@@ -1,5 +1,14 @@
-Always edit this file when changes are commited:
+Always edit this file when changes are commited::
 
 $Log: changelog,v $
+Revision 1.17  2002/09/27 14:47:36  sscpbas
+pxeconfig:
+  Added command line options so that we can use the program interactivly
+  and via command line
+
+  changed the info structure of a lot of functions. So the functions can
+  report different error messages for interactive usage and command line
+  usage.
+
 Revision 1.16  2002/09/11 12:59:19  sscpbas
 Change some header info for hexls
Index: /trunk/pxeconfig/pxeconfig
===================================================================
--- /trunk/pxeconfig/pxeconfig	(revision 18)
+++ /trunk/pxeconfig/pxeconfig	(revision 19)
@@ -7,6 +7,6 @@
 #
 # CVS info
-#  $Date: 2002/08/16 07:43:21 $
-#  $Revision: 1.9 $
+#  $Date: 2002/09/27 14:47:36 $
+#  $Revision: 1.10 $
 #
 # Copyright (C) 2002
@@ -29,8 +29,13 @@
 #
 """
-Usage: pxeconfig [-d|--directory <pxe_config_dir> ]
-
-With this program you can configure which PXE configuration file
-to use when a node boots. The program will ask the following questions:
+Usage: pxeconfig 
+ [-d|--directory <pxe_config_dir>]
+ [-n|--net <C-net> -s|--start <number> -e|--end <number> -f|--file <filename>]
+
+With this program you can configure which PXE configuration file a node
+will use when it boots. The program can be started interactivly or via
+command line options.
+
+When this program is started interactive it will ask the following questions:
   1) Network address (Class C-network address only)
   2) Starting number
@@ -63,12 +68,13 @@
 #
 PXE_CONF_DIR='/tftpboot/pxelinux.cfg'
-NADDR=0
-PXEFILE=1
-START=2
-END=3
-SHORTOPT_LIST='d:'
-LONGOPT_LIST=['directory=']
-
-def choice_pxe_configfile():
+NETWORK='network'
+FILENAME='filename'
+START='start'
+END='end'
+
+SHORTOPT_LIST='d:n:s:e:f:'
+LONGOPT_LIST=['directory=', 'net=', 'start=', 'end=', 'file=']
+
+def select_pxe_configfile():
   """
   Let user choose which pxeconfig file to use.
@@ -115,5 +121,5 @@
   return files[index-1]
 
-def create_links(list):
+def create_links(dict):
   """
   Create the links in the PXE_CONF_DIR,
@@ -122,7 +128,7 @@
   """
   os.chdir(PXE_CONF_DIR)
-  naddr = list[NADDR]
-  pxe_filename = list[PXEFILE]
-  for i in range(list[START], list[END]+1):
+  naddr = dict[NETWORK]
+  pxe_filename = dict[FILENAME]
+  for i in range(dict[START], dict[END]+1):
     haddr = '%s%02X' %(naddr, i)
     if DEBUG:
@@ -135,5 +141,5 @@
 
 
-def check_network(net):
+def check_network(net, prefix=''):
   """
   This function checks if the give network is a Class C-network and will
@@ -143,11 +149,21 @@
 
   if len(d) != 3:
-    print('Only C-class nerwork addresses are supported!!!')
-    sys.exit(1)
+    if prefix:
+      net = prefix + ' : ' + net
+      
+    print '%s is not a valid  C-class network address!!!' %net
+    sys.exit(1)
+
+
+  # Display right message for interactive/commandline
+  if prefix:
+    prefix = prefix + ' ' + net
+  else:
+    prefix = net
 
   # Check if we have valid network values
   r = ''
   for i in d:
-    i = check_number(i)
+    i = check_number(i, prefix)
     r = '%s%02X' %(r,i)
 
@@ -157,13 +173,14 @@
   return r
 
-def check_number(number):
+def check_number(number, prefix=''):
   """
   This functions checks if the input is between 0 < number < 255:
      number : is a string
+     prefix ; a string that must be displayed if an error occurs
   """
   try:
     n = int(number)
   except ValueError, detail:
-    print detail
+    print prefix,detail
     sys.exit(1)
 
@@ -171,10 +188,12 @@
     return n
   else:
+
+    if prefix:
+      number = prefix +' : ' + number
+
     print '%s is not a valid number, must be between 0 and 255' %number
     sys.exit(1)
 
-def interactive():
-
-  iptable = {}
+def interactive(binfo):
 
   print __doc__
@@ -189,25 +208,34 @@
   end = check_number(end)
 
-  pxe_filename = choice_pxe_configfile()
-
-  iptable[network] = []
-  iptable[network].append(naddr)
-  iptable[network].append(pxe_filename)
-  iptable[network].append(int(start))
-  iptable[network].append(int(end))
-
-  for net in iptable.keys():
-    if DEBUG:
-      print net, iptable[net]    
-    create_links(iptable[net])
-
-
-def check_args(argv):
+  pxe_filename = select_pxe_configfile()
+
+  binfo[NETWORK] = naddr
+  binfo[START] = int(start)
+  binfo[END] = int(end)
+  binfo[FILENAME] = pxe_filename
+
+  if DEBUG:
+    print network, binfo
+
+def check_cmd_line(binfo):
+  if len(binfo.keys()) != 4:
+    print 'Not enough arguments to create the links'
+    print __doc__
+    sys.exit(1)
+
+  # check_filename
+  #
+  if not os.path.isfile(os.path.join(PXE_CONF_DIR, binfo[FILENAME])):
+    print '%s: Filename does not exists' %binfo[FILENAME]
+    sys.exit(1)
+
+def check_args(argv, binfo):
   """
   command line option:
-    -d/--directory <dir>
+    -d|--directory <dir>
       Where <dir> is the directory where the pxe config files reside.
   """
   global PXE_CONF_DIR
+
   try:
     opts, args = getopt.getopt(argv[1:], SHORTOPT_LIST, LONGOPT_LIST)
@@ -217,14 +245,40 @@
     sys.exit(1)
 
-  if opts:
-    opt, PXE_CONF_DIR = opts[0]
-
-  if not os.path.isdir(PXE_CONF_DIR):
-    print 'Directory %s does not exists' %PXE_CONF_DIR
-    sys.exit(1)
+  # Check given options
+  #
+  for opt,value in opts:
+    if opt in ['-d', '--directory']:
+      if os.path.isdir(value):
+        PXE_CONF_DIR = value
+      else:
+        print 'Directory %s does not exists\n' %value
+	sys.exit(1)
+
+    elif opt in ['-n', '--net']:
+      network = value
+      binfo[NETWORK] = check_network(value, opt)
+
+    elif opt in ['-s', '--start']:
+      binfo[START] = check_number(value, opt)
+
+    elif opt in ['-e', '--end']:
+      binfo[END] = check_number(value, opt)
+
+    elif opt in ['-f', '--file']:
+      binfo[FILENAME] = value
+
 
 def main():
-  check_args(sys.argv)
-  interactive()
+  # A dictionary holding the boot info
+  #
+  bootinfo = {}
+  check_args(sys.argv, bootinfo)
+
+  if bootinfo:    
+    check_cmd_line(bootinfo)
+  else:
+    interactive(bootinfo)
+
+  create_links(bootinfo)
 
 if __name__ == '__main__':
