#!/bin/bash
#
# Author:	"Turbo Fredriksson" <turbo@bayour.com>
# Patches by:	"David D. Kilzer" <ddkilzer@lubricants-oil.com>

# --------- We want to be ABSOLUTLY sure that we should run this script!
if [ `id -u` != 0 ]; then
    echo "We need to run this script as root."
    exit 1
fi

OIFS=$IFS
NAGDIR=/tmp/nagios
CURRENTDATE=`date +"%Y%m%d"`

# --------- Clean up temp files from a previous run
if [ -d "$NAGDIR" ]; then
    echo "Please remove or rename $NAGDIR, then rerun this script."
    exit 1
fi
rm -f /tmp/nag.* $NAGDIR/*

# --------- Save the Netsaint and Nagios configurations
cd /etc/
CFG="-configurations_$CURRENTDATE.tgz"
if [ ! -f "netsaint$CFG" -a -d netsaint ]; then
    echo -n "Saving netsaint config: "
    tar czf netsaint$CFG netsaint
    echo done.
fi
if [ ! -f "nagios$CFG" -a -d nagios ]; then
    echo -n "Saving nagios config: "
    tar czf nagios$CFG nagios
    echo done.
fi

# --------- Create $NAGDIR before we use it
mkdir $NAGDIR

# --------- Create some temporary files for conversion purposes
TMP_TIMEPERIOD=`tempfile --prefix=nag.`	# timeperiods.cfg
TMP_HOST=`tempfile --prefix=nag.`	# hosts.cfg
TMP_HOSTGRP=`tempfile --prefix=nag.`	# hostgroups.cfg
TMP_CMD=`tempfile --prefix=nag.`	# misccommands.cfg
TMP_CONT=`tempfile --prefix=nag.`	# contacts.cfg
TMP_CONTGRP=`tempfile --prefix=nag.`	# contactgroups.cfg
TMP_SERVICE=`tempfile --prefix=nag.`	# services.cfg
TMP_CMDCHK=`tempfile --prefix=nag.`	# checkcommands.cfg
TMP_CGI=`tempfile --prefix=nag.`	# cgi.cfg
TMP_ESCAL=`tempfile --prefix=nag.`	# escalations.cfg
TMP_MAIN=`tempfile --prefix=nag.`	# nagios.cfg

# --------- Setup headers so we know which file is which just by looking at the first line
(echo "# Timeperiod configuration - converted from netsaint: $CURRENTDATE";echo)	> $TMP_TIMEPERIOD
(echo "# Host configuration - converted from netsaint: $CURRENTDATE";echo)		> $TMP_HOST
(echo "# Host group configuration - converted from netsaint: $CURRENTDATE";echo)	> $TMP_HOSTGRP
(echo "# Command configuration - converted from netsaint: $CURRENTDATE";echo)		> $TMP_CMD
(echo "# Contact configuration - converted from netsaint: $CURRENTDATE";echo)		> $TMP_CONT
(echo "# Contact group configuration - converted from netsaint: $CURRENTDATE";echo)	> $TMP_CONTGRP
(echo "# Service configuration - converted from netsaint: $CURRENTDATE";echo)		> $TMP_SERVICE
(echo "# Check command configuration - converted from netsaint: $CURRENTDATE";echo)	> $TMP_CMDCHK
(echo "# CGI configuration - converted from netsaint: $CURRENTDATE";echo)		> $TMP_CGI
(echo "# Main Nagios configuration - converted from netsaint: $CURRENTDATE";echo)	> $TMP_MAIN

# --------- Create some templates for some of the files
cat <<EOF >> $TMP_HOST
# Generic host definition template
define host {
	name				generic-host	; The name of this host template - referenced in other host definitions, used for template recursion/resolution
	notifications_enabled		1		; Host notifications are enabled
	event_handler_enabled		1		; Host event handler is enabled
	flap_detection_enabled		1		; Flap detection is enabled
	process_perf_data		1		; Process performance data
	retain_status_information	1		; Retain status information across program restarts
	retain_nonstatus_information	1		; Retain non-status information across program restarts

	register			0		; DONT REGISTER THIS DEFINITION - ITS NOT A REAL HOST, JUST A TEMPLATE!
}

EOF

cat <<EOF >> $TMP_SERVICE
# Generic service definition template
define service {
	name				generic-service	; The 'name' of this service template, referenced in other service definitions
	active_checks_enabled		1		; Active service checks are enabled
	passive_checks_enabled		1		; Passive service checks are enabled/accepted
	parallelize_check		1		; Active service checks should be parallelized (disabling this can lead to major performance problems)
	obsess_over_service		1		; We should obsess over this service (if necessary)
	check_freshness			0		; Default is to NOT check service 'freshness'
	notifications_enabled		1		; Service notifications are enabled
	event_handler_enabled		1		; Service event handler is enabled
	flap_detection_enabled		1		; Flap detection is enabled
	process_perf_data		1		; Process performance data
	retain_status_information	1		; Retain status information across program restarts
	retain_nonstatus_information	1		; Retain non-status information across program restarts

	register			0		; DONT REGISTER THIS DEFINITION - ITS NOT A REAL SERVICE, JUST A TEMPLATE!
}

EOF

cat <<EOF >> $TMP_CMDCHK
################################################################################
# COMMAND DEFINITIONS
#
# SYNTAX:
#	define command{
#		template      <templatename>
#		name          <objectname>
#		command_name  <commandname>
#		command_line  <commandline>
#	}
#
# WHERE:
# <templatename> = object name of another command definition that should be
#                  used as a template for this definition (optional)
# <objectname>   = object name of command definition, referenced by other
#                  command definitions that use it as a template (optional)
# <commandname>  = name of the command, as recognized/used by Nagios
# <commandline>  = command line
#
################################################################################

EOF

# --------- FILE: hosts.cfg -> XXX.cfg
cat /etc/netsaint/hosts.cfg | while read line; do
    case "$line" in
	timeperiod\[*)
	    # --> KEYWORD: timeperiod

	    NAME=`echo $line | sed -e 's@.*\[@@' -e 's@\].*@@'`
	    DEFINE=`echo $line | sed 's@.*=@@'`
	    IFS=';' ; set -- $DEFINE ; IFS="$OIFS"

	    cat <<EOF >> $TMP_TIMEPERIOD
define timeperiod {
	timeperiod_name			$NAME
	alias				$1
EOF
	    [ ! -z "$2" ] && echo "	sunday				$2" >> $TMP_TIMEPERIOD
	    [ ! -z "$3" ] && echo "	monday				$3" >> $TMP_TIMEPERIOD
	    [ ! -z "$4" ] && echo "	tuesday				$4" >> $TMP_TIMEPERIOD
	    [ ! -z "$5" ] && echo "	wednesday			$5" >> $TMP_TIMEPERIOD
	    [ ! -z "$6" ] && echo "	thursday			$6" >> $TMP_TIMEPERIOD
	    [ ! -z "$7" ] && echo "	friday				$7" >> $TMP_TIMEPERIOD
	    [ ! -z "$8" ] && echo "	saturday			$8" >> $TMP_TIMEPERIOD
	    cat <<EOF >> $TMP_TIMEPERIOD
}

EOF
	    ;;
	host\[*)
	    # --> KEYWORD: host

	    NAME=`echo $line | sed -e 's@.*\[@@' -e 's@\].*@@'`
	    DEFINE=`echo $line | sed 's@.*=@@'`
	    IFS=';' ; set -- $DEFINE ; IFS="$OIFS"

	    cat <<EOF >> $TMP_HOST
define host {
	use				generic-host	; Name of host template to use

	host_name			$NAME
EOF
	    # Not used:
	    #  3 <parent_hosts>
	    #  8 <notify_recovery>
	    #  9 <notify_down>
	    # 10 <notify_unreachable>
	    # 11 <event_handler>
	    [ ! -z "$1"  ] && echo "	alias				$1"  >> $TMP_HOST
	    [ ! -z "$2"  ] && echo "	address				$2"  >> $TMP_HOST
# not used  [ ! -z "$3"  ] && echo "					$3"  >> $TMP_HOST
	    [ ! -z "$4"  ] && echo "	check_command			$4"  >> $TMP_HOST
	    [ ! -z "$5"  ] && echo "	max_check_attempts		$5"  >> $TMP_HOST
	    [ ! -z "$6"  ] && echo "	notification_interval		$6"  >> $TMP_HOST
	    [ ! -z "$7"  ] && echo "	notification_period		$7"  >> $TMP_HOST
# not used  [ ! -z "$8"  ] && echo "					$8"  >> $TMP_HOST
# not used  [ ! -z "$9"  ] && echo "					$9"  >> $TMP_HOST
# not used  [ ! -z "$10" ] && echo "					$10" >> $TMP_HOST
# not used  [ ! -z "$11" ] && echo "					$11" >> $TMP_HOST
	    cat <<EOF >> $TMP_HOST
	notification_options		d,u,r
}

EOF
	    ;;
	hostgroup\[*)
	    # --> KEYWORD: hostgroup

	    NAME=`echo $line | sed -e 's@.*\[@@' -e 's@\].*@@'`
	    DEFINE=`echo $line | sed 's@.*=@@'`
	    IFS=';' ; set -- $DEFINE ; IFS="$OIFS"

	    cat <<EOF >> $TMP_HOSTGRP
define hostgroup {
	hostgroup_name			$NAME
EOF
	    [ ! -z "$1" ] && echo "	alias				$1" >> $TMP_HOSTGRP
	    [ ! -z "$2" ] && echo "	contact_groups			$2" >> $TMP_HOSTGRP
	    [ ! -z "$3" ] && echo "	members				$3" >> $TMP_HOSTGRP
	    cat <<EOF >> $TMP_HOSTGRP
}

EOF
	    ;;
	command\[*)
	    # --> KEYWORD: command

	    NAME=`echo $line | sed -e 's@.*\[@@' -e 's@\].*@@'`
	    DEFINE=`echo $line | sed 's@.*=@@'`
	    IFS=';' ; set -- $DEFINE ; IFS="$OIFS"

	    cat <<EOF >> $TMP_CMD
define command {
	command_name			$NAME
EOF
	    [ ! -z "$1" ] && echo "	command_line			$1" >> $TMP_CMD
	    cat <<EOF >> $TMP_CMD
}

EOF
	    ;;
	contact\[*)
	    # --> KEYWORD: contact

	    NAME=`echo $line | sed -e 's@.*\[@@' -e 's@\].*@@'`
	    DEFINE=`echo $line | sed 's@.*=@@'`
	    IFS=';' ; set -- $DEFINE ; IFS="$OIFS"

	    cat <<EOF >> $TMP_CONT
define contact {
	contact_name			$NAME
EOF
	    # Not used:
	    #  4 <notify_service_recovery>		
	    #  5 <notify_service_critical>		
	    #  6 <notify_service_warning>		
	    #  7 <notify_host_recovery>		
	    #  8 <notify_host_down>			
	    #  9 <notify_host_unreachable>		
	    [ ! -z "$1" ] && echo "	alias				$1" >> $TMP_CONT
	    [ ! -z "$2" ] && echo "	service_notification_period	$2" >> $TMP_CONT
	    [ ! -z "$3" ] && echo "	host_notification_period	$3" >> $TMP_CONT
# not used  [ ! -z "$4" ] && echo "			$4"  >> $TMP_CONT
# not used  [ ! -z "$5" ] && echo "			$5"  >> $TMP_CONT
# not used  [ ! -z "$6" ] && echo "			$6"  >> $TMP_CONT
# not used  [ ! -z "$7" ] && echo "			$7"  >> $TMP_CONT
# not used  [ ! -z "$8" ] && echo "			$8"  >> $TMP_CONT
# not used  [ ! -z "$9" ] && echo "			$9"  >> $TMP_CONT
	    shift ; shift ; shift ; shift ; shift ; shift ; shift ; shift ; shift
	    [ ! -z "$1" ] && echo "	service_notification_commands	$1" >> $TMP_CONT
	    [ ! -z "$2" ] && echo "	host_notification_commands	$2" >> $TMP_CONT
	    [ ! -z "$3" ] && echo "	email				$3" >> $TMP_CONT
	    [ ! -z "$4" ] && echo "	pager				$4" >> $TMP_CONT
	    cat <<EOF >> $TMP_CONT
}

EOF
	    ;;
	contactgroup\[*)
	    # --> KEYWORD: contactgroup

	    NAME=`echo $line | sed -e 's@.*\[@@' -e 's@\].*@@'`
	    DEFINE=`echo $line | sed 's@.*=@@'`
	    IFS=';' ; set -- $DEFINE ; IFS="$OIFS"

	    cat <<EOF >> $TMP_CONTGRP
define contactgroup {
	contactgroup_name		$NAME
EOF
	    [ ! -z "$1" ] && echo "	alias				$1" >> $TMP_CONTGRP
	    [ ! -z "$2" ] && echo "	members				$2" >> $TMP_CONTGRP
	    cat <<EOF >> $TMP_CONTGRP
}

EOF
	    ;;
	service\[*)
	    # --> KEYWORD: service

	    NAME=`echo $line | sed -e 's@.*\[@@' -e 's@\].*@@'`
	    DEFINE=`echo $line | sed 's@.*=@@'`
	    IFS=';' ; set -- $DEFINE ; IFS="$OIFS"

	    cat <<EOF >> $TMP_SERVICE
define service {
	use				generic-service	; Name of service template to use

	host_name			$NAME
EOF

	    # Not used:
	    # 10 <notify_recovery>		
	    # 11 <notify_critical>		
	    # 12 <notify_warning>		
	    # 13 <event_hander>		
	    [ ! -z "$1" ] && echo "	service_description		$1" >> $TMP_SERVICE
	    [ ! -z "$2" ] && echo "	is_volatile			$2" >> $TMP_SERVICE
	    [ ! -z "$3" ] && echo "	check_period			$3" >> $TMP_SERVICE
	    [ ! -z "$4" ] && echo "	max_check_attempts		$4" >> $TMP_SERVICE
	    [ ! -z "$5" ] && echo "	normal_check_interval		$5" >> $TMP_SERVICE
	    [ ! -z "$6" ] && echo "	retry_check_interval		$6" >> $TMP_SERVICE
	    [ ! -z "$7" ] && echo "	contact_groups			$7" >> $TMP_SERVICE
	    [ ! -z "$8" ] && echo "	notification_interval		$8" >> $TMP_SERVICE
	    [ ! -z "$9" ] && echo "	notification_period		$9" >> $TMP_SERVICE
	    shift ; shift ; shift ; shift ; shift ; shift ; shift ; shift ; shift
# not used  [ ! -z "$1" ] && echo "			$1" >> $TMP_SERVICE
# not used  [ ! -z "$2" ] && echo "			$2" >> $TMP_SERVICE
# not used  [ ! -z "$3" ] && echo "			$3" >> $TMP_SERVICE
# not used  [ ! -z "$4" ] && echo "			$4" >> $TMP_SERVICE
	    [ ! -z "$5" ] && echo "	check_command			$5" >> $TMP_SERVICE
	    cat <<EOF >> $TMP_SERVICE
}

EOF
	    ;;
	hostgroupescalation\[*)
	    # --> KEYWORD: hostgroupescalation

	    NAME=`echo $line | sed -e 's@.*\[@@' -e 's@\].*@@'`
	    DEFINE=`echo $line | sed 's@.*=@@'`
	    IFS=';' ; set -- $DEFINE ; IFS="$OIFS"
	    grp=$2

	    IFS='-' ; set -- $1 ; IFS="$OIFS"

	    cat <<EOF >> $TMP_ESCAL
define hostgroupescalation {
	hostgroup_name			$NAME
	first_notification		$1
	last_notification		$2
	contact_groups			$grp
	notification_interval		10
}

EOF
	    ;;
	serviceescalation\[*)
	    # --> KEYWORD: serviceescalation

	    NAME=`echo $line | sed -e 's@.*\[@@' -e 's@\].*@@'`
	    DEFINE=`echo $line | sed 's@.*=@@'`

	    IFS=';'
	      set -- $NAME   ; NAME=$1 ; DESC=$2
	      set -- $DEFINE ; GRP=$2
	    IFS="$OIFS" 

	    IFS='-'
	      set -- $1
	    IFS="$OIFS"

	    cat <<EOF >> $TMP_ESCAL
define serviceescalation {
	host_name			$NAME
	service_description		$DESC
	first_notification		$1
	last_notification		$2
	contact_groups			$GRP
	notification_interval		10
}

EOF
	    ;;
    esac
done

# --------- FILE: hosts.cfg -> escalations.cfg
cat /etc/netsaint/hosts.cfg | while read line; do
    case "$line" in
	service\[*)
	    # --> KEYWORD: service

	    NAME=`echo $line | sed -e 's@.*\[@@' -e 's@\].*@@'`
	    DEFINE=`echo $line | sed 's@.*=@@'`
	    IFS=';' ; set -- $DEFINE ; IFS="$OIFS"

	    cat <<EOF >> $TMP_ESCAL
define serviceescalation {
	host_name			$NAME
EOF
	    [ ! -z "$1" ] && echo "	service_description		$1"	>> $TMP_ESCAL
	    [ ! -z "$7" ] && echo "	contact_groups			$7"	>> $TMP_ESCAL
	    [ ! -z "$8" ] && echo "	notification_interval		$8"	>> $TMP_ESCAL

	    cat <<EOF >> $TMP_ESCAL
	first_notification		2
	last_notification		6
}

EOF
	    ;;
	hostgroup\[*)
	    # --> KEYWORD: hostgroup

	    NAME=`echo $line | sed -e 's@.*\[@@' -e 's@\].*@@'`
	    DEFINE=`echo $line | sed 's@.*=@@'`
	    IFS=';' ; set -- $DEFINE ; IFS="$OIFS"

	    cat <<EOF >> $TMP_ESCAL
define hostgroupescalation {
	hostgroup_name			$NAME
	first_notification		2
	last_notification		5
	contact_groups			$2
	notification_interval		10
}

EOF
	    ;;
    esac
done

# --------- FILE: plugins-auto.cfg -> checkcommands.cfg
if [ -f /etc/netsaint/plugins-auto.cfg ]; then
    cat /etc/netsaint/plugins-auto.cfg | while read line; do
	case "$line" in
	    command\[*)
		# --> KEYWORD: command

		NAME=`echo $line | sed -e 's@.*\[@@' -e 's@\].*@@'`
		DEFINE=`echo $line | sed 's@.*=@@'`
		IFS=';' ; set -- $DEFINE ; IFS="$OIFS"
		
		cat <<EOF >> $TMP_CMDCHK
define command {
	command_name			$NAME
EOF
		cmdline=`echo $1 | sed 's@/usr/lib/netsaint/plugins@\$USER1\$@'`
		[ ! -z "$1" ] && echo "	command_line			$cmdline" >> $TMP_CMDCHK
		cat <<EOF >> $TMP_CMDCHK
}

EOF
		;;
	esac
    done
fi

# --------- FILE: nscgi.cfg -> cgi.cfg
cat /etc/netsaint/nscgi.cfg | sed -e "s@netsaint@nagios@g" -e "s@^nagios_check_command@\#nagios_check_command@" >> $TMP_CGI
cat <<EOF >> $TMP_CGI
# -- NEW IN NAGIOS --

# CONTEXT-SENSITIVE HELP
# This option determines whether or not a context-sensitive
# help icon will be displayed for most of the CGIs.
# Valid options are as follows:
#	0 = disables context-sensitive help
#	1 = enables context-sensitive help
show_context_help=1

# DEFAULT STATUSMAP LAYOUT METHOD
# This option allows you to specify the default layout method
# the statusmap CGI should use for drawing hosts.  If you do
# not use this option, the default is to use user-defined
# coordinates.
# Valid options are as follows:
#	0 = User-defined coordinates
#	1 = Depth layers
#	2 = Collapsed tree
#	3 = Balanced tree
#	4 = Circular
#	5 = Circular (Marked Up)
#default_statusmap_layout=3

# DEFAULT STATUSWRL LAYOUT METHOD
# This option allows you to specify the default layout method
# the statuswrl (VRML) CGI should use for drawing hosts.  If you
# do not use this option, the default is to use user-defined
# coordinates.
# Valid options are as follows:
#	0 = User-defined coordinates
#	2 = Collapsed tree
#	3 = Balanced tree
#	4 = Circular
#default_statuswrl_layout=4

# PING SYNTAX
# This option determines what syntax should be used when
# attempting to ping a host from the WAP interface (using
# the statuswml CGI.  You must include the full path to
# the ping binary, along with all required options.  The
# $HOSTADDRESS$ macro is substituted with the address of
# the host before the command is executed.
ping_syntax=/bin/ping -n -c 5 $HOSTADDRESS$

# DB DOWNTIME DATA (Read-Only For CGIs)
# Note: These config directives are only used if you compiled
# in database support for downtime data!
# The user you specify here only needs SELECT privileges on the
# 'hostdowntime', and 'servicedowntime' tables in the database,
# as these values are only used by the CGIs.  The core program
# will read the directives you specify in a resource file.
#xdddb_host=somehost
#xdddb_port=someport
#xdddb_database=somedatabase
#xdddb_username=someuser
#xdddb_password=somepassword
EOF

# --------- FILE: netsaint.cfg -> nagios.cfg
cat /etc/netsaint/netsaint.cfg | \
    sed -e "s@netsaint@nagios@g" \
	-e "s@^euro_style_dates@# Not availible in Nagios: euro_style_dates@" \
	-e "s@^program_mode@# Not availible in Nagios: program_mode@" \
	-e "s@.*cfg_file.*\
@@" -e "s@^# EOF.*\
@@" >> $TMP_MAIN
cat <<EOF >> $TMP_MAIN
# -- NEW IN NAGIOS --

# Plugin commands (service and host check commands)
# Arguments are likely to change between different releases of the
# plugins, so you should use the same config file provided with the
# plugin release rather than the one provided with Nagios.
cfg_file=/etc/nagios/checkcommands.cfg

# Misc commands (notification and event handler commands, etc)
cfg_file=/etc/nagios/misccommands.cfg

# You can split other types of object definitions across several
# config files if you wish (as done here), or keep them all in a
# single config file.
cfg_file=/etc/nagios/contactgroups.cfg
cfg_file=/etc/nagios/contacts.cfg
cfg_file=/etc/nagios/dependencies.cfg
cfg_file=/etc/nagios/escalations.cfg
cfg_file=/etc/nagios/hostgroups.cfg
cfg_file=/etc/nagios/hosts.cfg
cfg_file=/etc/nagios/services.cfg
cfg_file=/etc/nagios/timeperiods.cfg

# SERVICE FRESHNESS CHECK OPTION
# This option determines whether or not Nagios will periodically
# check the "freshness" of service results.  Enabling this option
# is useful for ensuring passive checks are received in a timely
# manner.
# Values:
#	1 = enabled freshness checking
#	0 = disable freshness checking
check_service_freshness=1

# FRESHNESS CHECK INTERVAL
# This setting determines how often (in seconds) Nagios will
# check the "freshness" of service check results.  If you have
# disabled service freshness checking, this option has no effect.
freshness_check_interval=60

# DATE FORMAT OPTION
# This option determines how short dates are displayed. Valid options
# include:
#	us		MM-DD-YYYY HH:MM:SS
#	euro		DD-MM-YYYY HH:MM:SS
#	iso8601		YYYY-MM-DD HH:MM:SS
#	strict-iso8601	YYYY-MM-DDTHH:MM:SS
#
date_format=iso8601

# DOWNTIME FILE
# This is the file that Nagios will use for storing host and service
# downtime data.
downtime_file=/var/log/nagios/downtime.log

# NOTIFICATIONS OPTION
# This determines whether or not Nagios will sent out any host or
# service notifications when it is initially (re)started.
# Values:
#	1 = enable notifications
#	0 = disable notifications
enable_notifications=1
EOF

# --------- Done processing, move the tempfiles as nagios configs
[ -f "$NAGDIR/timeperiods.cfg" ] && mv $NAGDIR/timeperiods.cfg $NAGDIR/timeperiods.cfg.OLD
[ -f "$NAGDIR/hosts.cfg" ] && mv $NAGDIR/hosts.cfg $NAGDIR/hosts.cfg.OLD
[ -f "$NAGDIR/hostgroups.cfg" ] && mv $NAGDIR/hostgroups.cfg $NAGDIR/hostgroups.cfg.OLD
[ -f "$NAGDIR/misccommands.cfg" ] && mv $NAGDIR/misccommands.cfg $NAGDIR/misccommands.cfg.OLD
[ -f "$NAGDIR/contacts.cfg" ] && mv $NAGDIR/contacts.cfg $NAGDIR/contacts.cfg.OLD
[ -f "$NAGDIR/contactgroups.cfg" ] && mv $NAGDIR/contactgroups.cfg $NAGDIR/contactgroups.cfg.OLD
[ -f "$NAGDIR/services.cfg" ] && mv $NAGDIR/services.cfg $NAGDIR/services.cfg.OLD
[ -f "$NAGDIR/checkcommands.cfg" ] && mv $NAGDIR/checkcommands.cfg $NAGDIR/checkcommands.cfg.OLD
[ -f "$NAGDIR/cgi.cfg" ] && mv $NAGDIR/cgi.cfg $NAGDIR/cgi.cfg.OLD
[ -f "$NAGDIR/escalations.cfg" ] && mv $NAGDIR/escalations.cfg $NAGDIR/escalations.cfg.OLD 
[ -f "$NAGDIR/nagios.cfg" ] && mv $NAGDIR/nagios.cfg $NAGDIR/nagios.cfg.OLD

mv $TMP_TIMEPERIOD $NAGDIR/timeperiods.cfg
mv $TMP_HOST $NAGDIR/hosts.cfg
mv $TMP_HOSTGRP $NAGDIR/hostgroups.cfg
mv $TMP_CMD $NAGDIR/misccommands.cfg
mv $TMP_CONT $NAGDIR/contacts.cfg
mv $TMP_CONTGRP $NAGDIR/contactgroups.cfg
mv $TMP_SERVICE $NAGDIR/services.cfg
mv $TMP_CMDCHK $NAGDIR/checkcommands.cfg
mv $TMP_CGI $NAGDIR/cgi.cfg
mv $TMP_ESCAL $NAGDIR/escalations.cfg
mv $TMP_MAIN $NAGDIR/nagios.cfg

chmod 644 $NAGDIR/*.cfg
