control:
	AddInstallable = ( samba samba_reload netatalk netatalk_reload lprng lprng_reload cups cups_reload )

classes:
	#
	# Determine if this is a samba server, netatalk server or both, and define
	# classes based on that.
	#
	samba		= ( `/usr/bin/test -x /usr/sbin/smbd` )
	netatalk	= ( `/usr/bin/test -x /usr/sbin/afpd` )

	#
	# Determine which print system is in use, and define classes based on that.
	# We only support lprng and cups
	#
	lprng		= ( `/usr/bin/test -x /usr/sbin/lpd` )
	cups		= ( `/usr/bin/test -x /usr/sbin/cupsd` )

editfiles:
    samba.lprng::
	    { /etc/samba/smb.conf
		AppendIfNoSuchLine "include = /etc/samba/smb-printers-lprng.conf"
	    }
    samba.cups::
	    { /etc/samba/smb.conf
		AppendIfNoSuchLine "include = /etc/samba/smb-printers-cups.conf"
	    }

    samba::
	    { /etc/samba/smb.conf
		#
		# Global stuff
		#
		# Remove share and printer declarations from main smb.conf. The
		# configuration is split up in the following files:
		# - smb.conf
		# - smb-shares.conf
		# - smb-printers-[cups|lprng].conf
		#
		LocateLineMatching "^\[homes\]"
		DeleteNLines "100"
		CatchAbort
		ResetSearch "1"
		#
		# workgroup = $(site)
		#
		LocateLineMatching "^[;[:blank:]]*workgroup[[:blank:]]*=.*"
		BeginGroupIfNoLineMatching '^[[:blank:]]*workgroup[[:blank:]]*=[[:blank:]]*$(site)[[:blank:]]*'
			ReplaceLineWith '   workgroup = $(site)'
		EndGroup
		CatchAbort
		BeginGroupIfNoMatch '^[[:blank:]]*workgroup[[:blank:]]*=[[:blank:]]*$(site)[[:blank:]]*'
			InsertLine '   workgroup = $(site)'
		EndGroup
		#
		# wins support = yes
		#
		LocateLineMatching "^[;[:blank:]]*wins support[[:blank:]]*=.*"
		BeginGroupIfNoLineMatching "^[[:blank:]]*wins support[[:blank:]]*=[[:blank:]]*yes[[:blank:]]*"
			ReplaceLineWith '   wins support = yes'
		EndGroup
		CatchAbort
		BeginGroupIfNoMatch "^[[:blank:]]*wins support[[:blank:]]*=[[:blank:]]*yes[[:blank:]]*"
			InsertLine '   wins support = yes'
		EndGroup
		#
		# os level = 65
		#
		LocateLineMatching "^[;[:blank:]]*os level[[:blank:]]*=.*"
		BeginGroupIfNoLineMatching "^[[:blank:]]*os level[[:blank:]]*=[[:blank:]]*65[[:blank:]]*"
			ReplaceLineWith '   os level = 65'
		EndGroup
		CatchAbort
		BeginGroupIfNoMatch "^[[:blank:]]*os level[[:blank:]]*=[[:blank:]]*65[[:blank:]]*"
			InsertLine '   os level = 65'
		EndGroup
		#
		# domain master = yes
		#
		LocateLineMatching "^[;[:blank:]]*domain master[[:blank:]]*=.*"
		BeginGroupIfNoLineMatching "^[[:blank:]]*domain master[[:blank:]]*=[[:blank:]]*yes[[:blank:]]*"
			ReplaceLineWith '   domain master = yes'
		EndGroup
		CatchAbort
		BeginGroupIfNoMatch "^[[:blank:]]*domain master[[:blank:]]*=[[:blank:]]*yes[[:blank:]]*"
			InsertLine '   domain master = yes'
		EndGroup
		#
		# local master = yes
		#
		LocateLineMatching "^[;[:blank:]]*local master[[:blank:]]*=.*"
		BeginGroupIfNoLineMatching "^[[:blank:]]*local master[[:blank:]]*=[[:blank:]]*yes"
			ReplaceLineWith '   local master = yes'
		EndGroup
		CatchAbort
		BeginGroupIfNoMatch "^[[:blank:]]*local master[[:blank:]]*=[[:blank:]]*yes[[:blank:]]*"
			InsertLine '   local master = yes'
		EndGroup
		#
		# logon drive = Z:
		#
		LocateLineMatching "^[;[:blank:]]*logon drive[[:blank:]]*=.*"
		BeginGroupIfNoLineMatching "^[[:blank:]]*logon drive[[:blank:]]*=[[:blank:]]*Z:[[:blank:]]*"
			ReplaceLineWith '   logon drive = Z:'
		EndGroup
		CatchAbort
		BeginGroupIfNoMatch "^[[:blank:]]*logon drive[[:blank:]]*=[[:blank:]]*Z:[[:blank:]]*"
			InsertLine '   logon drive = Z:'
		EndGroup
		#
		# logon script = logon-$(fqhost).bat
		#
		LocateLineMatching "^[;[:blank:]]*logon script[[:blank:]]*=.*"
		BeginGroupIfNoLineMatching "^[[:blank:]]*logon script[[:blank:]]*=[[:blank:]]*logon-$(fqhost).bat[[:blank:]]*"
			ReplaceLineWith '   logon script = logon-$(fqhost).bat'
		EndGroup
		CatchAbort
		BeginGroupIfNoMatch "^[[:blank:]]*logon script[[:blank:]]*=[[:blank:]]*logon-$(fqhost).bat[[:blank:]]*"
			InsertLine '   logon script = logon-$(fqhost).bat'
		EndGroup
		#
		# domain logons = yes
		#
		LocateLineMatching "^[;[:blank:]]*domain logons[[:blank:]]*=.*"
		BeginGroupIfNoLineMatching "^[[:blank:]]*domain logons[[:blank:]]*=[[:blank:]]*yes[[:blank:]]*"
			ReplaceLineWith '   domain logons = yes'
		EndGroup
		CatchAbort
		BeginGroupIfNoMatch "^[[:blank:]]*domain logons[[:blank:]]*=[[:blank:]]*yes[[:blank:]]*"
			InsertLine '   domain logons = yes'
		EndGroup
		#
		# logon home = \\%N\USERPROFILES\%U
		#
		LocateLineMatching "^[;[:blank:]]*logon home[[:blank:]]*=.*"
		BeginGroupIfNoLineMatching "^[[:blank:]]*logon home[[:blank:]]*=[[:blank:]]*[\\][\\]%N[\\]USERPROFILES[\\]%U[[:blank:]]*"
			ReplaceLineWith '   logon home = \\%N\USERPROFILES\%U'
		EndGroup
		CatchAbort
		BeginGroupIfNoMatch "^[[:blank:]]*logon home[[:blank:]]*=[[:blank:]]*[\\][\\]%N[\\]USERPROFILES[\\]%U[[:blank:]]*"
			InsertLine '   logon home = \\%N\USERPROFILES\%U'
		EndGroup
		#
		# preferred master = yes
		#
		LocateLineMatching "^[;[:blank:]]*preferred master[[:blank:]]*=.*"
		BeginGroupIfNoLineMatching "^[[:blank:]]*preferred master[[:blank:]]*=[[:blank:]]*yes[[:blank:]]*"
			ReplaceLineWith '   preferred master = yes'
		EndGroup
		CatchAbort
		BeginGroupIfNoMatch "^[[:blank:]]*preferred master[[:blank:]]*=[[:blank:]]*yes[[:blank:]]*"
			InsertLine '   preferred master = yes'
		EndGroup
	        #
                # character set = ISO8859-1 
                #
                LocateLineMatching "^[;[:blank:]]*character set[[:blank:]]*=.*"
                BeginGroupIfNoLineMatching "^[[:blank:]]*character set[[:blank:]]*=[[:blank:]]*ISO8859-1[[:blank:]]*"
                        ReplaceLineWith '   character set = ISO8859-1'
                EndGroup
                CatchAbort
                BeginGroupIfNoMatch "^[[:blank:]]*character set[[:blank:]]*=[[:blank:]]*ISO8859-1[[:blank:]]*"
                        InsertLine '   character set = ISO8859-1'
                EndGroup
                #
                # client code page = 850
                #
                LocateLineMatching "^[;[:blank:]]*client code page[[:blank:]]*=.*"
                BeginGroupIfNoLineMatching "^[[:blank:]]*client code page[[:blank:]]*=[[:blank:]]*850[[:blank:]]*"
                        ReplaceLineWith '   client code page = 850'
                EndGroup
                CatchAbort
                BeginGroupIfNoMatch "^[[:blank:]]*client code page[[:blank:]]*=[[:blank:]]*850[[:blank:]]*"
                        InsertLine '   client code page = 850'
                EndGroup

		#
		# Append the include lines for the shares definition file
		#
		AppendIfNoSuchLine "include = /etc/samba/smb-shares.conf"
		DefineClasses "samba_reload"
	    }

	    #
	    # Batch file for mounting shares automatically
	    #
	    { /etc/samba/netlogon/logon-$(fqhost).bat
		#
		# We don't make this file dynamically, but instead we copy the contents
		# of a master file, but only if it's newer than the one installed.
		#
		AutoCreate
		BeginGroupIfFileIsNewer "/etc/local-COMMON/samba/netlogon/logon-$(fqhost).bat"
		    EmptyEntireFilePlease
		    Append "@echo off"
		    Append "rem DO NOT EDIT - auto-created by cfengine $(date)"
		    InsertFile "/etc/local-COMMON/samba/netlogon/logon-$(fqhost).bat"
		EndGroup
		FixEndOfLine "dos"
	    }

	    #
	    # Check if the shares definitions have changed
	    #
	    { /etc/samba/smb-shares.conf
		AutoCreate
		BeginGroupIfFileIsNewer "/etc/local-COMMON/file-$(fqhost).m4"
		    EmptyEntireFilePlease
		    SetLine "# DO NOT EDIT - auto-created by cfengine $(date)"
		    PrependIfNoLineMatching "^# .* cfengine .*"
		    RunScript "/etc/local-COMMON/cfengine/script.update_sambashares.sh"
		EndGroup
		DefineClasses "samba_reload"
	    }

    lprng::
	    #
	    # Printer configuration stuff
	    #
	    { /etc/printcap
		#
		# We don't make the printcap dynamically, but instead we copy the contents
		# of a master file, but only if it's newer than the one installed.
		#
		BeginGroupIfFileIsNewer "/etc/local-COMMON/printcap.$(fqhost)"
		    SetLine "# DO NOT EDIT - auto-created by cfengine $(date)"
		    PrependIfNoLineMatching "^# .* cfengine .*"
		    EmptyEntireFilePlease
		    InsertFile "/etc/local-COMMON/printcap.$(fqhost)"
		EndGroup
		DefineClasses "lprng_reload"
	    }

    samba_reload::
	    { /etc/samba/smb.conf
			LocateLineMatching "^; EDITED BY CFENGINE .*"
			ReplaceAll '; EDITED BY CFENGINE .*$' With '; EDITED BY CFENGINE $(date)'
			CatchAbort
			BeginGroupIfNoMatch "^; EDITED BY CFENGINE .*"
			    Append '; EDITED BY CFENGINE $(date)'
			EndGroup
	    }

processes:
	"smbd"	restart "/etc/init.d/samba restart"
	"afpd"	restart "/etc/init.d/netatalk restart"

shellcommands:
    samba_reload::
	"/etc/init.d/samba force-reload"
    netatalk_reload::
	"/etc/init.d/netatalk force-reload"
    lprng_reload::
	"/etc/init.d/lprng force-reload"
    cups_reload::
	"/etc/init.d/cups force-reload"