<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">#!/usr/bin/perl -wT
#
# ==========================================================================
#
# ZoneMinder Package Control Script, $Date$, $Revision$
# Copyright (C) 2001-2008 Philip Coombes
#
# 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.
#
# ==========================================================================
#
# This script is used to start and stop the ZoneMinder package primarily to
# allow command line control for automatic restart on reboot (see zm script)
#
use strict;
use bytes;

# ==========================================================================
#
# Don't change anything below here
#
# ==========================================================================

@EXTRA_PERL_LIB@
use ZoneMinder;
use DBI;
use POSIX;
use Time::HiRes qw/gettimeofday/;

# Detaint our environment
$ENV{PATH}  = '/bin:/usr/bin';
$ENV{SHELL} = '/bin/sh' if exists $ENV{SHELL};
delete @ENV{qw(IFS CDPATH ENV BASH_ENV)};

logInit();

my $command = $ARGV[0];

my $state;

my $dbh = zmDbConnect();

if ( !$command || $command !~ /^(?:start|stop|restart|status|logrot)$/ )
{
	if ( $command )
	{
		# Check to see if it's a valid run state
		my $sql = "select * from States where Name = '$command'";
		my $sth = $dbh-&gt;prepare_cached( $sql ) or Fatal( "Can't prepare '$sql': ".$dbh-&gt;errstr() );
		my $res = $sth-&gt;execute() or Fatal( "Can't execute: ".$sth-&gt;errstr() );
		if ( $state = $sth-&gt;fetchrow_hashref() )
		{
			$state-&gt;{Name} = $command;
			$state-&gt;{Definitions} = [];
			foreach( split( /,/, $state-&gt;{Definition} ) )
			{
				my ( $id, $function, $enabled ) = split( /:/, $_ );
				push( @{$state-&gt;{Definitions}}, { Id=&gt;$id, Function=&gt;$function, Enabled=&gt;$enabled } );
			}
			$command = 'state';
		}
		else
		{
			$command = undef;
		}
	}
	if ( !$command )
	{
		print( "Usage: zmpkg.pl &lt;start|stop|restart|status|logrot|'state'&gt;\n" );
		exit( -1 );
	}
}

# Move to the right place
chdir( ZM_PATH_WEB ) or Fatal( "Can't chdir to '".ZM_PATH_WEB."': $!" );

my $dbg_id = "";

Info( "Command: $command\n" );

my $retval = 0;

if ( $command eq "state" )
{
	Info( "Updating DB: $state-&gt;{Name}\n" );
	my $sql = "select * from Monitors order by Id asc";
	my $sth = $dbh-&gt;prepare_cached( $sql ) or Fatal( "Can't prepare '$sql': ".$dbh-&gt;errstr() );
	my $res = $sth-&gt;execute() or Fatal( "Can't execute: ".$sth-&gt;errstr() );
	while( my $monitor = $sth-&gt;fetchrow_hashref() )
	{
		foreach my $definition ( @{$state-&gt;{Definitions}} )
		{
			if ( $monitor-&gt;{Id} =~ /^$definition-&gt;{Id}$/ )
			{
				$monitor-&gt;{NewFunction} = $definition-&gt;{Function};
				$monitor-&gt;{NewEnabled} = $definition-&gt;{Enabled};
			}
		}
		#next if ( !$monitor-&gt;{NewFunction} );
		$monitor-&gt;{NewFunction} = 'None' if ( !$monitor-&gt;{NewFunction} );
		$monitor-&gt;{NewEnabled} = 0 if ( !$monitor-&gt;{NewEnabled} );
		if ( $monitor-&gt;{Function} ne $monitor-&gt;{NewFunction} || $monitor-&gt;{Enabled} ne $monitor-&gt;{NewEnabled} )
		{
			my $sql = "update Monitors set Function = ?, Enabled = ? where Id = ?";
			my $sth = $dbh-&gt;prepare_cached( $sql ) or Fatal( "Can't prepare '$sql': ".$dbh-&gt;errstr() );
			my $res = $sth-&gt;execute( $monitor-&gt;{NewFunction}, $monitor-&gt;{NewEnabled}, $monitor-&gt;{Id} ) or Fatal( "Can't execute: ".$sth-&gt;errstr() );
		}
	}
	$sth-&gt;finish();

	$command = "restart";
}

if ( $command =~ /^(?:stop|restart)$/ )
{
	my $status = runCommand( "zmdc.pl check" );

	if ( $status eq "running" )
	{
		runCommand( "zmdc.pl shutdown" );
		zmMemTidy();
	}
	else
	{
		$retval = 1;
	}
}

runCommand( "zmupdate.pl -f" );

if ( $command =~ /^(?:start|restart)$/ )
{
	my $status = runCommand( "zmdc.pl check" );

	if ( $status eq "stopped" )
	{
        if ( ZM_DYN_DB_VERSION &amp;&amp; ZM_DYN_DB_VERSION ne ZM_VERSION )
        {
            Fatal( "Version mismatch, system is version ".ZM_VERSION.", database is ".ZM_DYN_DB_VERSION.", please run zmupdate.pl to update." );
            exit( -1 );
        }

        # Recreate the temporary directory if it's been wiped
        if ( !-e "@ZM_TMPDIR@" )
        {
            Debug( "Recreating temporary directory '@ZM_TMPDIR@'" );
            mkdir( "@ZM_TMPDIR@", 0700 ) or Fatal( "Can't create missing temporary directory '@ZM_TMPDIR@': $!" );
            my ( $runName ) = getpwuid( $&gt; );
            if ( $runName ne ZM_WEB_USER )
            {
                # Not running as web user, so should be root in whch case chown the temporary directory
                my ( $webName, $webPass, $webUid, $webGid ) = getpwnam( ZM_WEB_USER ) or Fatal( "Can't get user details for web user '".ZM_WEB_USER."': $!" );
                chown( $webUid, $webGid, "@ZM_TMPDIR@" ) or Fatal( "Can't change ownership of temporary directory '@ZM_TMPDIR@' to '".ZM_WEB_USER.":".ZM_WEB_GROUP."': $!" );
            }
        }
		zmMemTidy();
		runCommand( "zmfix" );
		runCommand( "zmdc.pl startup" );

		my $sql = "select * from Monitors";
		my $sth = $dbh-&gt;prepare_cached( $sql ) or Fatal( "Can't prepare '$sql': ".$dbh-&gt;errstr() );
		my $res = $sth-&gt;execute() or Fatal( "Can't execute: ".$sth-&gt;errstr() );
		while( my $monitor = $sth-&gt;fetchrow_hashref() )
		{
			if ( $monitor-&gt;{Function} ne 'None' )
			{
				if ( $monitor-&gt;{Type} eq 'Local' )
				{
					runCommand( "zmdc.pl start zmc -d $monitor-&gt;{Device}" );
				}
				else
				{
					runCommand( "zmdc.pl start zmc -m $monitor-&gt;{Id}" );
				}
				if ( $monitor-&gt;{Function} ne 'Monitor' )
				{
					if ( ZM_OPT_FRAME_SERVER )
					{
						runCommand( "zmdc.pl start zmf -m $monitor-&gt;{Id}" );
					}
					runCommand( "zmdc.pl start zma -m $monitor-&gt;{Id}" );
				}
				if ( ZM_OPT_CONTROL )
				{
					if ( $monitor-&gt;{Function} eq 'Modect' || $monitor-&gt;{Function} eq 'Mocord' )
					{
						if ( $monitor-&gt;{Controllable} &amp;&amp; $monitor-&gt;{TrackMotion} )
						{
							runCommand( "zmdc.pl start zmtrack.pl -m $monitor-&gt;{Id}" );
						}
					}
				}
			}
		}
		$sth-&gt;finish();

		# This is now started unconditionally
		runCommand( "zmdc.pl start zmfilter.pl" );
		if ( ZM_RUN_AUDIT )
		{
			runCommand( "zmdc.pl start zmaudit.pl -c" );
		}
		if ( ZM_OPT_TRIGGERS )
		{
			runCommand( "zmdc.pl start zmtrigger.pl" );
		}
		if ( ZM_OPT_X10 )
		{
			runCommand( "zmdc.pl start zmx10.pl -c start" );
		}
		runCommand( "zmdc.pl start zmwatch.pl" );
		if ( ZM_CHECK_FOR_UPDATES )
		{
			runCommand( "zmdc.pl start zmupdate.pl -c" );
		}
	}
	else
	{
		$retval = 1;
	}
}

if ( $command eq "status" )
{
	my $status = runCommand( "zmdc.pl check" );

	print( STDOUT $status."\n" );
}

if ( $command eq "logrot" )
{
	runCommand( "zmdc.pl logrot" );
}

exit( $retval );
</pre></body></html>