"""
Validates whether the system is reasonably well configured for
serving up content.  This is the code behind 'cobbler check'.

Copyright 2007-2008, Red Hat, Inc
Michael DeHaan <mdehaan@redhat.com>

This software may be freely redistributed under the terms of the GNU
general public license.

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., 675 Mass Ave, Cambridge, MA 02139, USA.
"""

# usage: --server=bootserver.example.com --koan="--profile=FOO"
# requires latest git://git.fedoraproject.org/git/hosted/livecd

import optparse
import subprocess
import sys
import os


# packages to put on the LiveCD

#packages = [
#  "kernel", "bash", "koan", "policycoreutils", "grub", "eject", "tree"
#]

#=======

def main(args):


   p = optparse.OptionParser()
   p.add_option("-m", "--mirror",action="store",help="Alternate Fedora mirror")
   (options,args) = p.parse_args()


   if not os.path.exists("/usr/bin/livecd-creator"):
      print "livecd-tools needs to be installed"
      sys.exit(1)

   if not os.path.exists("/usr/bin/createrepo"):
      print "createrepo needs to be installed"
      sys.exit(1)

   if not os.path.exists("/sbin/mksquashfs"):
      print "squashfs-tools needs to be installed"
      sys.exit(1)



   subprocess.call("mkdir -p /tmp/newkoan", shell=True) 
   subprocess.call("createrepo /tmp/newkoan",shell=True)

   # write config file
   # this configuration is the kickstart for the live CD, not the install system
   # tweak at your own risk

   basef = open("./base.cfg")
   base_config = basef.read()
   basef.close()
   if options.mirror:
      base_config = base_config.replace("download.fedora.redhat.com",options.mirror)
   cfg = open("/tmp/clonerlive.cfg","w+")
   cfg.write(base_config)
   cfg.close()

   # ======

   cmd = "livecd-creator"
   cmd = cmd + " --fslabel=cloner-live-cd"
   cmd = cmd + " --config=/tmp/clonerlive.cfg"
   

   #for x in packages:
   #   cmd = cmd + " --package=%s" % x
   
   print "running: %s" % cmd

   try:
       os.remove("cloner-live-cd.iso")
   except:
       print "existing file not removed"
   subprocess.call(cmd, shell=True)

if __name__ == "__main__":
   main(sys.argv)

