Linux-DVB-DVBT-Apps-QuartzPVR

 view release on metacpan or  search on metacpan

install/tpl/quartzpvr-install.pl  view on Meta::CPAN

	{
		my $password = $settings_href->{'SQL_ROOT_PASSWORD'} ;
		my $sql =<<SQL ;
ALTER TABLE  $settings_href->{DATABASE}.$table CHANGE  `adapter`  `adapter` VARCHAR( 16 ) NOT NULL DEFAULT  '0' ;
SQL
		mysql_runit($app, $password, $sql, "MySQL error while updating table '$table' to version $latest_version") ;
		
	}
}

#----------------------------------------------------------------------
sub update_table_schedule
{
	my ($app, $settings_href, $existing_version, $latest_version) = @_ ;

	my $table = 'schedule' ;
	if ($existing_version eq '0')
	{
		my $password = $settings_href->{'SQL_ROOT_PASSWORD'} ;
		my $sql =<<SQL ;
ALTER TABLE  $settings_href->{DATABASE}.$table CHANGE  `adapter`  `adapter` VARCHAR( 16 ) NOT NULL DEFAULT  '0' ;
SQL
		mysql_runit($app, $password, $sql, "MySQL error while updating table '$table' to version $latest_version") ;
		
	}
}



#======================================================================
# INSTALL
#======================================================================

#----------------------------------------------------------------------
# Directories
#
sub create_dirs
{
	my ($app, $settings_href) = @_ ;
	
	print "Creating directories:\n" ;

	my $web_uid = getpwnam($settings_href->{'WEB_USER'}) ;
	my $web_gid = getgrnam($settings_href->{'WEB_GROUP'}) ;
	
	my $pvr_uid = getpwnam($settings_href->{'PVR_USER'}) ;
	my $pvr_gid = getgrnam($settings_href->{'PVR_GROUP'}) ;
	
	## Web
	foreach my $d (qw/PVR_ROOT/)
	{
		my $dir = $settings_href->{$d} ;
		if (! -d $dir)
		{
			print " * $dir .. ($settings_href->{'WEB_USER'}:$settings_href->{'WEB_GROUP'})\n" ;
			if (!mkpath([$dir], 0, 0755))
			{
				print "ERROR unable to create dir $dir : $!" ;
				exit 1 ;
			}
			chown $web_uid, $web_gid, $dir ;
		}
	}
	
	## PVR
	foreach my $d (qw/VIDEO_DIR VIDEO_TRASH AUDIO_DIR PVR_LOGDIR PVR_HOME/)
	{
		my $dir = $settings_href->{$d} ;
		if (! -d $dir)
		{
			print " * $dir .. ($settings_href->{'PVR_USER'}:$settings_href->{'PVR_GROUP'})\n" ;
			if (!mkpath([$dir], 0, 0755))
			{
				print "ERROR unable to create dir $dir : $!" ;
				exit 1 ;
			}
			chown $pvr_uid, $pvr_gid, $dir ;
		}
	}

#	## Subdirs
#	my $dir = "VIDEO_DIR" ;
#	foreach my $d (qw/TRASH/)
#	{
#		my $dir = "$settings_href->{$dir}/$d" ;
#		if (! -d $dir)
#		{
#			print " * $dir .. ($settings_href->{'PVR_USER'}:$settings_href->{'PVR_GROUP'})\n" ;
#			if (!mkpath([$dir], 0, 0755))
#			{
#				print "ERROR unable to create dir $dir : $!" ;
#				exit 1 ;
#			}
#			chown $pvr_uid, $pvr_gid, $dir ;
#		}
#		
#	}

	# pvr server
	foreach my $dir (qw%/var/run/quartzpvr%)
	{
		if (! -d $dir)
		{
			print " * $dir .. ($settings_href->{'PVR_USER'}:$settings_href->{'PVR_GROUP'})\n" ;
			if (!mkpath([$dir], 0, 0755))
			{
				print "ERROR unable to create dir $dir : $!" ;
				exit 1 ;
			}
			chown $pvr_uid, $pvr_gid, $dir ;
		}
	}
}

#----------------------------------------------------------------------
# Install
#
sub install_files
{
	my ($app, $settings_href, $dirs_aref) = @_ ;
	
	my $dest = $settings_href->{'PVR_ROOT'} ;
	
	print "Installing files:\n" ;
	
	foreach my $aref (@$dirs_aref)
	{
		my ($dir, $owner) = @$aref ;
		
		## copy directory
		print " * Installing files from $dir .. " ;
		runit($app,
			"cp -pr $dir $dest",
			"copying files from $dir"
		) ;
		print "done\n" ;
		
#		$app->run("cp -pr $dir $dest") ;
#		print "done\n" ;
#		my $status = $app->run()->status ;
#		if ($status)
#		{
#			print "Error copying files from $dir\n" ;
#			exit 1 ;
#		}
		
		## Set ownership
		runit($app,
			"chown -R $owner $dest/$dir",
			"setting ownership of $dest/$dir to $owner"
		) ;

#		$app->run("chown -R $owner $dest/$dir") ;
#		$status = $app->run()->status ;
#		if ($status)
#		{
#			print "Error setting ownership of $dest/$dir to $owner\n" ;
#			exit 1 ;
#		}
	}
	
	## Copy index file 
	runit($app,
		"cp index.php $dest",
		"copying index.php to $dest"
	) ;
	

}

#----------------------------------------------------------------------
# Templates
#
sub template_files
{
	my ($app, $settings_href, $template_file) = @_ ;
	
	my %vars = (%$settings_href) ;
	
	$vars{'uid'} = $< ;
	$vars{'gid'} = $( ;
	
	$vars{'web_uid'} = getpwnam($settings_href->{'WEB_USER'}) ; 
	$vars{'web_gid'} = getgrnam($settings_href->{'WEB_GROUP'}) ;
	$vars{'pvr_uid'} = getpwnam($settings_href->{'PVR_USER'}) ; 
	$vars{'pvr_gid'} = getgrnam($settings_href->{'PVR_GROUP'}) ;
	$vars{'pvrdir'} = $settings_href->{'PVR_ROOT'} ;
	foreach my $field (keys %$settings_href)
	{
		$vars{lc $field} = $settings_href->{$field} ;
	}

$app->prt_data("template_files() : vars=", \%vars) if $DEBUG >= 2 ;

	## read in control file
	my @templates ;
	my $line ;
	open my $fh, "<$template_file" or die "Error: Unable to read template control file $template_file : $!" ;
	while (defined($line=<$fh>))
	{
		chomp $line ;
		next if $line =~ m/^\s*#/ ;
		
		# "php/Config/Constants.inc", "$pvrdir/php/Config/Constants.inc", $web_uid, $web_gid, 0666
		my @fields = split(/,/, $line) ;
		if (@fields >= 5)
		{
			my $aref = [] ;
			foreach my $field (@fields)
			{
				$field =~ s/^\s+// ;
				$field =~ s/\s+// ;
				$field =~ s/^['"](.*)['"]$/$1/ ;
				
#				$field =~ s/\$(\w+)/$vars{$1}/ge ;
				$field =~ s/\$(\w+)/$vars{$1}/g ;
				
				push @$aref, $field ;
			}
			push @templates, $aref ;
		}
	}
	close $fh ;

$app->prt_data("templates=", \@templates) if $DEBUG >= 2 ;
	
	## Process template files	
	print "Installing template files:\n" ;
	foreach my $aref (@templates)
	{
		my ($src, $dest, $uid, $gid, $mode) = @$aref ;
		
		$mode = oct($mode) ;

		print " * Installing template $src .. " ;
		
		# read
		local $/ ;
		open my $fh, "<$src" or die "Error: unable to read template $src : $!" ;
		my $data = <$fh> ;
		close $fh ;
		
		# translate
#		$data =~ s/\%([\w_]+)\%/$settings_href->{$1}/ge ;
		$data =~ s/\%([\w_]+)\%/$settings_href->{$1}/g ;
		
		# check destination directory
		my $dir = dirname($dest) ;
		if (! -d $dir)
		{
			mkpath([$dir], 0, 0755) ;
			chown $uid, $gid, $dir ;
		}
		
		# write
		open my $fh, ">$dest" or die "Error: unable to write template $dest : $!" ;
		print $fh $data ;
		close $fh ;
		
		# set perms
		chown $uid, $gid, $dest ;
		chmod $mode, $dest ;
		
print "\nSet $dest owner $uid:$gid  mode $mode\n" if $DEBUG ;
		print "done\n" ;
		
	}	
	
}


#----------------------------------------------------------------------
sub start_server
{
	my ($app, $settings_href) = @_ ;

	print "Starting QuartzPVR server ..\n" ;
	system("/etc/init.d/quartzpvr-server restart") ;
	
	print "Setting QuartzPVR server service runlevels ..\n" ;
	my $rc ;
	$rc = try_runit($app, "chkconfig quartzpvr-server on") ;
	print " * Success (chkconfig)\n" if ($rc == 0) ;
	$rc = try_runit($app, "update-rc.d quartzpvr-server defaults") ;
	print " * Success (update-rc.d)\n" if ($rc == 0) ;
}

#----------------------------------------------------------------------
sub dvbt_scan
{
	my ($app, $settings_href) = @_ ;

	print "Initialising DVB-T ..\n" ;
	
	## Create dvb 
	my $dvb = Linux::DVB::DVBT->new('errmode' => 'return') ;
	my @devices = $dvb->devices() ;
	if (@devices < 1) 
	{
		print " ** No DVB adapters found, skipping **\n" ;
		return 0 ;
	}
	else
	{
		my $tuning_href = $dvb->get_tuning_info() ;
	#Linux::DVB::DVBT::prt_data("Current tuning info=", $tuning_href) ;
		$dvb = undef ;
		
		if ($tuning_href)
		{
			print " * DVB-T already initialised, skipping\n" ;
		}
		else
		{
			print " * Tuning DVB-T, this may take some time - please wait ..\n" ;
			system("dvbt-scan $settings_href->{'DVBT_FREQFILE'}") ;
		}
	
	}
	
	return 1 ;



( run in 1.108 second using v1.01-cache-2.11-cpan-71847e10f99 )