#!/usr/bin/python
"""
Creates context dependant symbolic links (cdsl). 
The process includes creating and copying of files and directories and building symbolic links. 
Needs a working cdsl-infrastructure and a matching inventoryfile. 
Could create hostdependent and shared cdsls.
"""
__version__ = "$revision$"
__description__="""
Binary to manage cdsls
"""

import getopt
import sys
import logging
import os.path

from comoonics import ComLog
from comoonics.ComExceptions import ComException
#from comoonics import GetOpts

from comoonics.cluster import parseClusterConf
from comoonics.cluster.ComClusterInfo import ClusterInfo
from comoonics.cluster.ComClusterRepository import ClusterRepository, RedHatClusterRepository
from comoonics.cdsl.ComCdslRepository import CdslRepository
from comoonics.cdsl.ComCdsl import Cdsl
from comoonics.cdsl import dirtrim, commonoptparseroptions

from optparse import OptionParser

usage = "usage: %prog [options] fullpathtofile"
parser = OptionParser(usage=usage, description=__doc__)
parser=commonoptparseroptions(parser)

parser.add_option("-a", "--hostdependent", dest="hostdependent", default=False, action="store_true", help="Creates hostdependent cdsl and copy src to all nodes")
parser.add_option("-s", "--shared", dest="shared", default=False, action="store_true", help="Creates shared cdsl and copy src to shared tree")

parser.add_option("-f", "--force", dest="force", default=False, action="store_true", help="forces overwriting of existing links, files and directories, skip backup")
parser.add_option("-u", "--updateinventory", dest="updateinventory", default=False, action="store_true", help="""updates inventoryfile:
   if cdsl with given target does not exist in inventoryfile, add entry
   if it does not exist on filesystem but in inventoryfile, delete entry
   if it exists in inventoryfile but verifies from filesystem, update entry""")

ComLog.setLevel(logging.INFO)
(options, args) = parser.parse_args()

if len(args) != 1:
    parser.error("Failure no ore more then one arguments given.")
elif not options.shared and not options.hostdependent:
    parser.error("You must specify the type of the cdsl to be created. Means either hostdendent (-a|--hostdependent) or shared (-s|--shared).")
else:
    src=args[0]

doc=parseClusterConf(options.clusterconf)
          
#create needed cluster objects
clusterRepository = ClusterRepository(doc.documentElement,doc)
clusterInfo = ClusterInfo(clusterRepository)
    
#create cdsl objects
cdslRepository = CdslRepository(options.inventoryfile,None,False,root=options.root, mountpoint=options.mountpoint)

if options.hostdependent:
    #Creates a hostdependent cdsl and copies content to all nodes defined 
    #in clusterrepository or specified by maxnodeidnum in cdslrepository
    obj = Cdsl(src, Cdsl.HOSTDEPENDENT_TYPE, cdslRepository, clusterInfo, None)
elif options.shared:
    #Creates a shared cdsl and copies content to sharedtree defined 
    #in clusterrepository
    obj = Cdsl(src, Cdsl.SHARED_TYPE, cdslRepository, clusterInfo, None)

obj.commit(force=options.force)
        
if options.updateinventory:
    cdslRepository.update(src,clusterInfo, chroot=options.root)
