#!/bin/sh
# setup
# This script should be run after the nPULSE archive is unpacked, in order
# to set up the various program, config, and html files

# Find distribution directory
cd `dirname $0`
dist_dir=`pwd`;
ver=`cat $dist_dir/version.txt`
autoos=2

echo ""
echo "***********************************************************************"
echo "       Welcome to the nPULSE setup script, version $ver    "
echo "***********************************************************************"
echo "nPULSE is a web-based network monitoring package for Unix-like"
echo "operating systems."
echo "***********************************************************************"
echo ""

# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
 
# 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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

# Only root can run this
id | grep "uid=0(" >/dev/null
if [ $? != "0" ]; then
	echo "ERROR: The nPULSE install script must be run as root"
	echo ""
	exit
fi

# Ask for nPULSE install directory
echo "nPULSE should be installed in a directory owned by root and not"
echo "accessible to non-privileged users."
echo ""
printf "Installation directory [/usr/local/npulse]: "
if [ "$inst_dir" = "" ]; then
	read inst_dir
fi
if [ "$inst_dir" = "" ]; then
	inst_dir=/usr/local/npulse
fi
abspath=`echo $inst_dir | grep "^/"`
if [ "$abspath" = "" ]; then
	echo "Install directory must be an absolute path"
	echo ""
	exit
fi
echo ""

# Ask for nPULSE config directory
echo "***********************************************************************"
echo "nPULSE uses separate directories for config, log, and data files."
echo "Unless you want to run multiple versions of nPULSE at the same time"
echo "you can just accept the defaults. Note that for large sites, the data"
echo "directory can get very large."
echo ""
printf "Config file directory [$inst_dir/etc]: "
if [ "$config_dir" = "" ]; then
	read config_dir
fi
if [ "$config_dir" = "" ]; then
	config_dir=$inst_dir/etc
fi
abspath=`echo $config_dir | grep "^/"`
if [ "$abspath" = "" ]; then
	echo "Config directory must be an absolute path"
	echo ""
	exit
fi
if [ -r "$config_dir/config" ]; then
        echo ""
	echo "Found existing nPULSE configuration in $config_dir"
        echo "Gathering previous configuration information."
	echo ""
	upgrading=1
fi

# Check if upgrading from an old version
if [ "$upgrading" = 1 ]; then
	# Stop old version
	$config_dir/stop >/dev/null 2>&1

	# Get old config info
	os_type=`grep "^os_type=" $config_dir/config | sed -e 's/os_type=//g'`
	os_version=`grep "^os_version=" $config_dir/config | sed -e 's/os_version=//g'`
	real_os_type=`grep "^real_os_type=" $config_dir/config | sed -e 's/real_os_type=//g'`
	real_os_version=`grep "^real_os_version=" $config_dir/config | sed -e 's/real_os_version=//g'`
        nmap=`grep "^nmap=" $config_dir/config | sed -e 's/nmap=//g'`
        tracert=`grep "^tracert=" $config_dir/config | sed -e 's/tracert=//g'`
        log_dir=`grep "^log_dir=" $config_dir/config | sed -e 's/log_dir=//g'`
        data_dir=`grep "^data_dir=" $config_dir/config | sed -e 's/data_dir=//g'`
        perl=`grep "^perl=" $config_dir/config | sed -e 's/perl=//g'`
        autointerval=`grep "^autointerval=" $config_dir/config | sed -e 's/autointerval=//g'`
        demo=`grep "^demo=" $config_dir/config | sed -e 's/demo=//g'`
        email=`grep "^email=" $config_dir/config | sed -e 's/email=//g'`
        have_email=`grep "^have_email=" $config_dir/config | sed -e 's/have_email=//g'`

	# Get old root, host, port
	olddist_dir=`grep "^root=" $config_dir/miniserv.conf | sed -e 's/root=//g'`
	host=`grep "^host=" $config_dir/miniserv.conf | sed -e 's/host=//g'`
	port=`grep "^port=" $config_dir/miniserv.conf | sed -e 's/port=//g'`

else
        # Ask for log directory
	printf "Log file directory [$inst_dir/log]: "
	read log_dir
	if [ "$log_dir" = "" ]; then
		log_dir=$inst_dir/log
	fi
	abspath=`echo $log_dir | grep "^/"`
	if [ "$abspath" = "" ]; then
		echo "Log file directory must be an absolute path"
		echo ""
		exit
	fi
	# Ask for data directory
	printf "Data directory [$inst_dir/data]: "
	read data_dir
	if [ "$data_dir" = "" ]; then
		data_dir=$inst_dir/data
	fi
	abspath=`echo $data_dir | grep "^/"`
	if [ "$abspath" = "" ]; then
		echo "Data directory must be an absolute path"
		echo ""
		exit
	fi
	echo ""

	# Ask where traceroute is installed
	if [ "$tracert" = "" ]; then
                printf "Checking traceroute .. "
        	if [ -x /usr/bin/traceroute ]; then
	        	tracert=/usr/bin/traceroute
                        echo "ok"
	        elif [ -x /usr/sbin/traceroute ]; then
	        	tracert=/usr/sbin/traceroute
                        echo "ok"
	        else
                        tracert=`which traceroute`
                        if [ $? != 0 ]; then
                                echo "failed"
                                echo ""
                                echo "***********************************************************************"
                                echo "Setup is unable to find your traceroute command. nPULSE requires"
                                echo "traceroute. Please enter the full path to the traceroute command"
                                echo "on your system."
                                echo ""
                                printf "Full path to traceroute: "
                                read tracert
                                if [ "$tracert" = "" ]; then
                                        echo "ERROR: No path entered!"
                                        echo ""
                                        exit
                                fi
                        fi
	        fi
	fi

	# Ask where nmap is installed
	if [ "$nmap" = "" ]; then
                printf "Checking nmap ........ "
        	if [ -x /usr/local/bin/nmap ]; then
	        	nmap=/usr/local/bin/nmap
                        echo "ok"
	        elif [ -x /usr/bin/nmap ]; then
	        	nmap=/usr/bin/nmap
                        echo "ok"
	        else
                        nmap=`which nmap`
                        if [ $? != 0 ]; then
                                echo "failed"
                                echo ""
                                echo "***********************************************************************"
                                echo "Setup is unable to find nmap. Please enter the full path to the"
                                echo "nmap software on your system."
                                echo ""
                                printf "Full path to nmap: "
                                read nmap
                                if [ "$nmap" = "" ]; then
                                        echo ""
                                        echo "ERROR: No path entered!"
                                        echo "Nmap can be downloaded from http://www.insecure.org/nmap"
                                        echo ""
                                        exit
                                fi
                        fi
	        fi
                echo ""
	fi

        # Ask if nmap is equal to or greater than verion 2.51
        nmapver=`$nmap --version`
        if [ $? != 0 ]; then
          nmapver="unknown"
        fi
	echo "************************************************************************"
	echo "nPULSE requires version 2.51 or greater. Your version of nmap"
	echo "reports version: $nmapver"
        echo ""
	printf "Is this ok? (y/n) [y]: "
        read nmapok
        if [ "$nmapok" = "" ]; then
                nmapok=y
        fi
        nmapok=`echo $nmapok | grep '^[yY]'`
        if [ "$nmapok" = "" ]; then
                echo ""
                echo "ERROR: Invalid nmap version, exiting..."
                echo "Check out http://www.insecure.org/nmap for the latest version."
                exit
        fi
        echo ""

        # Ask if DEMO mode requested
	echo "***********************************************************************"
        echo "nPULSE can run in DEMO only mode. This mode allows a quick look at"
        echo "most of the features, but not all. Choose DEMO mode only if you do not "
        echo "want a fully operational system."
        echo ""
        printf "Run in DEMO mode? (y/n) [n]: "
        read demo
        if [ "$demo" = "y" ]; then
                echo "*** DEMO mode enabled. ***"
                autointerval=0
                demo=1
        else
                echo "DEMO mode disabled."
                demo=0
        fi
        echo ""

	# Ask where Perl is installed
	if [ "$perl" = "" ]; then
                echo "***********************************************************************"
                echo "nPULSE is written entirely in Perl. Please enter the full path to"
                echo "the Perl 5 interpreter on your system."
                echo ""
                if [ -x /usr/local/bin/perl ]; then
                        perldef=/usr/local/bin/perl
                elif [ -x /usr/bin/perl ]; then
                        perldef=/usr/bin/perl
                else
                        perldef=`which perl`
                        if [ $? != 0 ]; then
                                perldef=""
                        fi
                fi
		if [ "$perldef" = "" ]; then
			printf "Full path to Perl: "
			read perl
			if [ "$perl" = "" ]; then
				echo "ERROR: No path entered!"
				echo ""
				exit
			fi
		else
			printf "Full path to Perl [$perldef]: "
			read perl
			if [ "$perl" = "" ]; then
				perl=$perldef
			fi
		fi
	fi
	echo ""

	# Test Perl 
	printf "Testing Perl .... "
	if [ ! -x $perl ]; then
                echo "failed"
                echo ""
		echo "ERROR: Failed to find Perl at $perl"
		echo ""
		exit
	fi
        echo "ok"
        printf "Test script ..... "
	$perl -e 'print "foobar\n"' 2>/dev/null | grep foobar >/dev/null
	if [ $? != 0 ]; then
                echo "failed"
                echo ""
		echo "ERROR: Failed to run test Perl script. Maybe $perl is"
		echo "not the Perl interpreter, or is not installed properly"
		echo ""
		exit
	fi
        echo "ok"
        printf "Version check ... "
	$perl -e 'exit ($] < 5.002 ? 1 : 0)'
	if [ $? = "1" ]; then
                echo "failed"
                echo ""
		echo "ERROR: Detected old Perl version. nPULSE requires"
		echo "Perl 5.002 or greater to run"
		echo ""
		exit
	fi
        echo "ok"
        printf "Socket check .... "
	$perl -e 'use Socket; print "foobar\n"' 2>/dev/null | grep foobar >/dev/null
	if [ $? != 0 ]; then
                echo "failed"
                echo ""
		echo "ERROR: Perl Socket module not installed. Maybe Perl has"
		echo "not been properly installed on your system"
		echo ""
		exit
	fi
        echo "ok"
        printf "crypt check ..... "
	$perl -e '$c = crypt("xx", "yy"); exit($c ? 0 : 1)'
	if [ $? != 0 ]; then
                echo "failed"
                echo ""
		echo "ERROR: Perl crypt function does not work. Maybe Perl has"
		echo "not been properly installed on your system"
		echo ""
		exit
	fi
        echo "ok"
        printf "Mail check ...... "
        have_email=1
	$perl -e 'use Mail::Mailer; print "foobar\n"' 2>/dev/null | grep foobar >/dev/null
	if [ $? != 0 ]; then
                echo "failed"
                echo ""
		echo "WARNING: Perl Mail::Mailer module is not installed."
                echo "The email alert capability will be disabled."
		echo ""
		echo "To install the Mail::Mailer module, exit setup, then"
		echo "as root issue the following command:"
		echo ""
		echo "    perl -MCPAN -e 'install Mail::Mailer'"
	        echo ""
		echo "Then rerun this setup script."
		echo ""
                have_email=0
        else
                have_email=1
                echo "ok"
        fi
	echo "Perl seems to be installed ok"
	echo ""


	# Ask if auto recheck is required
	if [ "$autointerval" = "" ]; then
                echo "***********************************************************************"
                echo "nPULSE can automatically monitor the network at a specified"
                echo "interval. Please enter the monitoring interval in minutes."
                echo "Enter zero (0)if you want to disable this feature."
                echo ""
                printf "Automatic monitoring interval (minutes) [20]: "
                read autointerval
                if [ "$autointerval" = "" ]; then
                        autointerval="20"
	        fi
       	        echo ""
	fi
        if [ "$autointerval" = "" ]; then
                autointerval=0
        fi

        # Ask for email address for trigger notification
        if [ "$have_email" = "1" ]; then
                if [ "$email" = "" ]; then
                        echo "***********************************************************************"
                        echo "nPULSE can send email notifications for site failures and site failure"
                        echo "recoveries. Please enter an email address to enable this feature."
                        echo "All notifications will be sent to this address. To disable this feature,"
                        echo "enter a blank address."
                        echo ""
                        printf "Email notification address: "
                        read email
                        echo ""
                 fi
        fi

        # Ask for operating system type
	echo "***********************************************************************"
	if [ "$os_type" = "" ]; then
		$perl $dist_dir/oschooser.pl $dist_dir/os_list.txt /tmp/$$.os $autoos
		if [ $? != 0 ]; then
			exit $?
		fi
		. /tmp/$$.os
		rm -f /tmp/$$.os
	fi
	echo "Operating system name:    $real_os_type"
	echo "Operating system version: $real_os_version"
	echo ""

	# Ask for web server port, name and password
	echo "***********************************************************************"
	echo "nPULSE uses its own password-protected web server to provide"
	echo "access to the administration programs. The setup script needs to know:"
	echo " - What port to run the web server on. There must not be another"
	echo "   web server already using this port."
	echo " - The login name required to access the web server."
	echo " - The password required to access the web server."
	echo " - The hostname of this system that the web server should use."
	echo ""
	printf "Web server port [19000]: "
	if [ "$port" = "" ]; then
		read port
		if [ "$port" = "" ]; then
			port=19000
		fi
	fi
	if [ $port -lt 1 ]; then
		echo "ERROR: $port is not a valid port number"
		echo ""
		exit
	fi
	if [ $port -gt 65535 ]; then
		echo "ERROR: $port is not a valid port number. Port numbers cannot be"
		echo "       greater than 65535"
		echo ""
		exit
	fi
	$perl -e 'use Socket; socket(FOO, PF_INET, SOCK_STREAM, getprotobyname("tcp")); setsockopt(FOO, SOL_SOCKET, SO_REUSEADDR, pack("l", 1)); bind(FOO, sockaddr_in($ARGV[0], INADDR_ANY)) || exit(1); exit(0);' $port
	if [ $? != 0 ]; then
		echo "ERROR: TCP port $port is already in use by another program"
		echo ""
		exit
	fi
	printf "Login name [admin]: "
	if [ "$login" = "" ]; then
		read login
		if [ "$login" = "" ]; then
			login="admin"
		fi
	fi
	if [ "$password" = "" -a "$crypt" = "" ]; then
                while [ "$password" = "" ]; do
                        printf "Login password: "
                        stty -echo
                        read password
                        stty echo
                        printf "\n"
                        if [ "$password" = "" ]; then
                                echo "You must enter a password."
                        fi
                done
                printf "Password again: "
                stty -echo
                read password2
                stty echo
                printf "\n"
                if [ "$password" != "$password2" ]; then
                        echo "ERROR: Passwords don't match"
                        echo ""
                        exit
                fi
	fi
	defhost=`hostname -i | sed 's/ //g'`
        if [ $? != 0 ]; then
                defhost=`hostname | sed 's/ //g'`
        fi
	printf "Web server hostname or ip address [$defhost]: "
	if [ "$host" = "" ]; then
		read host
		if [ "$host" = "" ]; then
			host=$defhost
		fi
	fi
fi

# Ask user if all the info entered is correct

echo "***********************************************************************"
echo "Setup has gathered the following information. Please check it for"
echo "correctness. If anything is incorrect, exit setup and start again."
echo ""
if [ "$upgrading" = 1 ]; then
        echo "Upgrading an existing nPULSE installation"
fi
echo "Current Directory:      $dist_dir"
echo "Install Directory:      $inst_dir"
echo "Config Directory:       $config_dir"
echo "Log Directory:          $log_dir"
echo "Data Directory:         $data_dir"
echo "Nmap command:           $nmap"
echo "Traceroute command:     $tracert"
echo "Perl 5 command:         $perl"
if [ "$demo" = "1" ]; then
        echo "Demonstration mode:     on"
else
        echo "Demonstration mode:     off"
fi
if [ "$autointerval" = "0" ]; then
        echo "Automatic monitoring:   off"
else
        echo "Automatic monitoring:   every $autointerval minutes"
fi
if [ "$have_email" = "1" ]; then
        if [ "$email" = "" ]; then
                echo "Email notification:     disabled"
        else
                echo "Email notification:     $email"
        fi
else
        echo "Email notification:     OFF"
fi
echo "Web server hostname:    $host"
echo "Web server port:        $port"
echo ""
printf "Is this information correct (y/n) [y]? "
read ans
if [ "$ans" = "" ]; then
        isok="y"
else
        isok=`echo $ans | grep "^[yY]"`
fi
if [ "$isok" = "" ]; then
        echo ""
        echo "Exiting Setup at your request."
        rm -f $config_dir/config
        echo ""
        exit
fi
echo ""

# Write out installation information

if [ ! -d $inst_dir ]; then
        printf "Creating installation directory ..... "
	mkdir $inst_dir;
        chown root:bin $inst_dir
        chmod 700 $inst_dir
	if [ $? != 0 ]; then
                echo "failed"
                echo ""
		echo "ERROR: Failed to create directory $inst_dir"
		echo ""
		exit
	fi
        echo "done"
fi

if [ "$dist_dir" != "$inst_dir" ]; then
        printf "Installing files .................... "
        sed 's#require ".\/preferences.pl";#require "'$inst_dir'/preferences.pl";#' $dist_dir/autorecheck.pl > $inst_dir/autorecheck.pl
        sed 's#require ".\/preferences.pl";#require "'$inst_dir'/preferences.pl";#' $dist_dir/npulse.cgi > $inst_dir/npulse.cgi
        sed 's#require ".\/preferences.pl";#require "'$inst_dir'/preferences.pl";#' $dist_dir/demogen.pl > $inst_dir/demogen.pl
        cp $dist_dir/cgi-lib.pl $dist_dir/miniserv.pl $dist_dir/*.demo $dist_dir/services $inst_dir
        cp $dist_dir/index.html $inst_dir/index.html
        cp $dist_dir/help.html $inst_dir/help.html
        cp $dist_dir/default.html $inst_dir/compact.html
        cp $dist_dir/default.html $inst_dir/basic.html
        cp $dist_dir/default.html $inst_dir/full.html
        if [ ! -d $inst_dir/lib ]; then
          mkdir $inst_dir/lib
          chown root:bin $inst_dir/lib
          chmod 700 $inst_dir/lib
        fi
        cp $dist_dir/lib/* $inst_dir/lib
        if [ ! -d $inst_dir/images ]; then
          mkdir $inst_dir/images
          chown root:bin $inst_dir/images
          chmod 700 $inst_dir/images
        fi
        cp $dist_dir/images/* $inst_dir/images
        rm -f $inst_dir/default.html
        echo "done"
fi

if [ ! -d $config_dir ]; then
        printf "Creating config directory ........... "
	mkdir $config_dir;
        chown root:bin $config_dir
        chmod 700 $config_dir
	if [ $? != 0 ]; then
                echo "failed"
                echo ""
		echo "ERROR: Failed to create directory $config_dir"
		echo ""
		exit
	fi
        echo "done"
fi

if [ ! -d $log_dir ]; then
        printf "Creating log directory .............. "
        mkdir $log_dir
        chown root:bin $log_dir
        chmod 700 $log_dir
        if [ $? != 0 ]; then
                echo "failed"
                echo ""
                echo "ERROR: Failed to create directory $log_dir"
                echo ""
                exit
        fi
        echo "done"
fi

if [ ! -d $data_dir ]; then
        printf "Creating data directory ............. "
        mkdir $data_dir
        chown root:bin $data_dir
        chmod 700 $data_dir
        if [ $? != 0 ]; then
                echo "failed"
                echo ""
                echo "ERROR: Failed to create directory $data_dir"
                echo ""
                exit
        fi
        echo "done"
fi

if [ "$upgrading" = 1 ]; then
        printf "Updating web server config file ..... "
	# Update miniserv.conf with new root directory and mime types file
	grep -v "^root=" $config_dir/miniserv.conf | grep -v "^mimetypes=" >/tmp/$$.miniserv.conf
	mv /tmp/$$.miniserv.conf $config_dir/miniserv.conf
	echo "root=$inst_dir" >> $config_dir/miniserv.conf
	echo "mimetypes=$config_dir/mime.types" >> $config_dir/miniserv.conf
	grep logout= $config_dir/miniserv.conf >/dev/null
	if [ $? != "0" ]; then
		echo "logout=$config_dir/logout-flag" >> $config_dir/miniserv.conf
	fi
        echo "done"
else
	# Create webserver config file
        cfile=$config_dir/miniserv.conf
        printf "Creating web server config files .... "
	echo "port=$port" > $cfile
	echo "root=$inst_dir" >> $cfile
	echo "host=$host" >> $cfile
	echo "mimetypes=$config_dir/mime.types" >> $cfile
	echo "addtype_cgi=internal/cgi" >> $cfile
	echo "realm=nPULSE Server" >> $cfile
	echo "logfile=$log_dir/miniserv.log" >> $cfile
	echo "pidfile=$log_dir/miniserv.pid" >> $cfile
	echo "logtime=168" >> $cfile
	echo "ppath=$ppath" >> $cfile
	echo "env_npulse_CONFIG=$config_dir" >> $cfile
	echo "env_npulse_VAR=$log_dir" >> $cfile
	echo "logout=$config_dir/logout-flag" >> $cfile
	echo "listen=$port" >> $cfile
	echo "denyfile=\\.pl\$" >> $cfile
	if [ "$allow" != "" ]; then
		echo "allow=$allow" >> $cfile
	fi

        ufile=$config_dir/miniserv.users
	if [ "$crypt" != "" ]; then
		echo "$login:$crypt:0" > $ufile
	else
		$perl -e 'print "$ARGV[0]:",crypt($ARGV[1], "XX"),":0\n"' "$login" "$password" > $ufile
	fi
	chmod 600 $ufile
	echo "userfile=$ufile" >> $cfile
	chmod 600 $cfile

	echo "done"
fi	

if [ "$noperlpath" = "" ]; then
        printf  "Inserting Perl path into scripts .... "
	(find $inst_dir -name '*.cgi' -print ; find $inst_dir -name '*.pl' -print) | $perl $dist_dir/perlpath.pl $perl -
	echo "done"
fi

if [ "$noprefupdate" = "" ]; then
        printf "Updating preferences file ........... "
        cp $dist_dir/preferences.pl $inst_dir
        if [ "$have_email" = "0" ]; then
          emailaddr=
        else
          emailaddr=$email
        fi
        $perl $dist_dir/updpref.pl $inst_dir $data_dir $config_dir $log_dir $nmap $tracert $autointerval $demo $emailaddr
	if [ $? != 0 ]; then
                echo "failed"
                echo ""
		echo "ERROR: Preference file update error."
		echo ""
		exit
	fi
        echo "done"
fi

printf "Creating start and stop scripts ..... "
        rm -f $config_dir/stop $config_dir/start
        echo "#!/bin/sh" >$config_dir/start
        echo "echo Starting nPULSE server in $inst_dir" >>$config_dir/start
        echo "$inst_dir/miniserv.pl $config_dir/miniserv.conf" >>$config_dir/start
        if [ "$autointerval" != "0" ]; then
                echo "echo Starting nPULSE AutoRecheck in $inst_dir" >>$config_dir/start
                echo "$inst_dir/autorecheck.pl $log_dir/autorecheck.pid $autointerval" >>$config_dir/start
        fi

        echo "#!/bin/sh" >$config_dir/stop
        echo "echo Stopping nPULSE server in $inst_dir" >>$config_dir/stop
        echo "kill \`cat $log_dir/miniserv.pid\`" >>$config_dir/stop
        if [ "$autointerval" != "0" ]; then
                echo "echo Stopping nPULSE AutoRecheck in $inst_dir" >>$config_dir/stop
                echo "kill \`cat $log_dir/autorecheck.pid\`" >>$config_dir/stop
        fi
echo "done"

printf "Creating config files ............... "
	echo "os_type=$os_type" > $config_dir/config
	echo "os_version=$os_version" >> $config_dir/config
	echo "real_os_type=$real_os_type" >> $config_dir/config
	echo "real_os_version=$real_os_version" >> $config_dir/config
	echo "nmap=$nmap" >> $config_dir/config
	echo "tracert=$tracert" >> $config_dir/config
        echo "log_dir=$log_dir" >> $config_dir/config
        echo "data_dir=$data_dir" >> $config_dir/config
        echo "perl=$perl" >> $config_dir/config
        echo "autointerval=$autointerval" >> $config_dir/config
        echo "demo=$demo" >> $config_dir/config
        echo "email=$email" >> $config_dir/config
        echo "have_email=$have_email" >> $config_dir/config
        chmod 600 $config_dir/config

        echo $ver > $config_dir/version.txt
        cp $dist_dir/services $config_dir/services
        cp $dist_dir/mime.types $config_dir/mime.types
        chmod 600 $config_dir/version.txt $config_dir/services $config_dir/mime.types
echo "done"

printf "Changing ownership and permissions .. "
        chmod -R 600 $inst_dir
        chmod 750 $inst_dir/npulse.cgi $inst_dir/miniserv.pl $inst_dir/demogen.pl
        chmod 750 $inst_dir/autorecheck.pl
        chmod -R 600 $config_dir
        chmod 750 $config_dir/start $config_dir/stop

if [ "$nochown" != 1 ]; then
        chown -R root:bin $inst_dir $config_dir
fi
echo "done"
echo ""

if [ "$demo" = "1" ]; then
  if [ -f $dist_dir/demogen.tar ]; then
    echo "Copying DEMO data."
    tar -xf $dist_dir/demogen.tar $data_dir
  else 
    echo "Generating DEMO information."
    $inst_dir/demogen.pl
  fi
  echo "Generating HTML files."
  $inst_dir/npulse.cgi 'a=create' >/dev/null 2>/dev/null
  echo ""
fi

if [ "$nostart" = "" ]; then
	echo "Attempting to start nPULSE .. "
	cd $config_dir
	$config_dir/start
	if [ $? != 0 ]; then
                echo ""
		echo "ERROR: Failed to start nPULSE"
		echo ""
		exit
	fi
        echo ""

	echo ""
	echo "***********************************************************************"
        if [ "$upgrading" = 1 ]; then
                echo "nPULSE has been upgraded and started successfully."
        else
	        echo "nPULSE has been installed and started successfully."
        fi
        echo "Use your web browser to go to"
	echo ""
	echo "  http://$host:$port/index.html"
	echo ""
	echo "and log in with the name and password you entered previously."
	echo "***********************************************************************"
	echo ""
fi

