From 71bb0f053b55f52de819efb46040d679d25236cd Mon Sep 17 00:00:00 2001
From: Daniel Prosser <dpross1100@msn.com>
Date: Mon, 28 Jan 2019 16:22:29 -0500
Subject: [PATCH] Add ZoneMinder patches, CONTENTDIR, and update requires

---
 system/ZoneMinder/README                      | 10 ++++----
 system/ZoneMinder/ZoneMinder.SlackBuild       | 12 +++++++--
 system/ZoneMinder/ZoneMinder.info             |  2 +-
 system/ZoneMinder/fix_video_file_format.patch | 11 +++++++++
 system/ZoneMinder/fix_zone_area_calc.patch    | 35 +++++++++++++++++++++++++++
 5 files changed, 62 insertions(+), 8 deletions(-)
 create mode 100644 system/ZoneMinder/fix_video_file_format.patch
 create mode 100644 system/ZoneMinder/fix_zone_area_calc.patch

diff --git a/system/ZoneMinder/README b/system/ZoneMinder/README
index d96eb97904..44db1da50e 100644
--- a/system/ZoneMinder/README
+++ b/system/ZoneMinder/README
@@ -7,10 +7,10 @@ be controlled via web or semi-automatically using a variety of
 protocols. It can also be integrated into a home automation system
 via X.10 or other protocols.  This build includes cambozola and jscalendar.
 
-Optional dependencies are ffmpeg (video streaming and recoding support),
-perl-Archive-Zip (automatic event uploading), perl-X10
-(integration with X10-based home automation), perl-Device-SerialPort
-(RS232/RS485 PTZ camera control), perl-MIME-Lite, and perl-MIME-Entity
-(automatic event mail notification).
+Optional dependencies are php-apcu (to enable the ZoneMinder API),
+perl-Archive-Zip (automatic event uploading), perl-X10 (integration with
+X10-based home automation), perl-Device-SerialPort (RS232/RS485 PTZ camera
+control), perl-MIME-Lite, and perl-MIME-Entity (automatic event mail
+notification).
 
 See README.SLACKWARE for configuration information.
diff --git a/system/ZoneMinder/ZoneMinder.SlackBuild b/system/ZoneMinder/ZoneMinder.SlackBuild
index 1423b69fc4..222d092dc5 100644
--- a/system/ZoneMinder/ZoneMinder.SlackBuild
+++ b/system/ZoneMinder/ZoneMinder.SlackBuild
@@ -34,6 +34,7 @@ DOCGRP=${DOCGRP:-apache}
 DOCROOT=${DOCROOT:-"/srv/httpd/htdocs/zm"}
 CGIROOT=${CGIROOT:-"/srv/httpd/cgi-bin"}
 CACHEDIR=${CACHEDIR:-"/var/cache/ZoneMinder"}
+CONTENTDIR=${CONTENTDIR:-"/var/lib/ZoneMinder"}
 
 if [ -z "$ARCH" ]; then
   case "$( uname -m )" in
@@ -80,6 +81,12 @@ find -L . \
  \( -perm 666 -o -perm 664 -o -perm 640 -o -perm 600 -o -perm 444 \
   -o -perm 440 -o -perm 400 \) -exec chmod 644 {} \;
 
+# Fails to encode mp4 videos without this patch
+patch -p0 < $CWD/fix_video_file_format.patch
+
+# Applies upstream commits 4937a686 and 4da95369
+patch -p0 < $CWD/fix_zone_area_calc.patch
+
 CFLAGS="$SLKCFLAGS" \
 CXXFLAGS="$SLKCFLAGS -D__STDC_CONSTANT_MACROS" \
 cd $TMP/$PRGNAM-$VERSION
@@ -90,7 +97,8 @@ cmake -DCMAKE_INSTALL_PREFIX=/usr \
 	-DZM_WEBDIR=$DOCROOT \
 	-DZM_CGIDIR=$CGIROOT \
 	-DZM_WEB_USER=$DOCOWN \
-	-DZM_WEB_GROUP=$DOCGRP .
+	-DZM_WEB_GROUP=$DOCGRP \
+	-DZM_CONTENTDIR=$CONTENTDIR .
 
 make
 make install DESTDIR=$PKG INSTALLDIRS=vendor INSTALLVENDORMAN3DIR=/usr/man/man3
@@ -128,7 +136,7 @@ mkdir -p $PKG$CACHEDIR
 install -d -o $DOCOWN -g $DOCGRP $PKG$CACHEDIR
 
 for DIR in events images sound; do
-  install -d -o $DOCOWN -g $DOCGRP $PKG/var/lib/ZoneMinder/$DIR
+  install -d -o $DOCOWN -g $DOCGRP $PKG/$CONTENTDIR/$DIR
 done
 
 # Install logrotate script
diff --git a/system/ZoneMinder/ZoneMinder.info b/system/ZoneMinder/ZoneMinder.info
index 2bae7ed3f6..b7bdfa0558 100644
--- a/system/ZoneMinder/ZoneMinder.info
+++ b/system/ZoneMinder/ZoneMinder.info
@@ -9,6 +9,6 @@ MD5SUM="fc986fcb7601d3fe463a2970ead67cf7 \
         10f2160fe68294013efcd1473cd36f72"
 DOWNLOAD_x86_64=""
 MD5SUM_x86_64=""
-REQUIRES="perl-Sys-MemInfo perl-Date-Manip perl-Sys-Mmap perl-LWP-Protocol-https SDL2 perl-JSON-MaybeXS perl-Number-Bites-Human perl-TimeDate perl-Sys-CPU"
+REQUIRES="perl-Sys-MemInfo perl-Date-Manip perl-Sys-Mmap perl-LWP-Protocol-https SDL2 perl-JSON-MaybeXS perl-Number-Bites-Human perl-TimeDate perl-Sys-CPU libmp4v2 x264"
 MAINTAINER="Geno Bob"
 EMAIL="GenoBob@gmail.com"
diff --git a/system/ZoneMinder/fix_video_file_format.patch b/system/ZoneMinder/fix_video_file_format.patch
new file mode 100644
index 0000000000..f238c77f37
--- /dev/null
+++ b/system/ZoneMinder/fix_video_file_format.patch
@@ -0,0 +1,11 @@
+--- src/zm_event.cpp    2019-01-21 23:17:19.881961455 -0500
++++ src/zm_event.cpp.new        2019-01-21 23:17:38.452315254 -0500
+@@ -196,7 +196,7 @@
+
+   if ( monitor->GetOptVideoWriter() != 0 ) {
+     snprintf(video_name, sizeof(video_name), "%" PRIu64 "-%s", id, "video.mp4");
+-    snprintf(video_file, sizeof(video_file), staticConfig.video_file_format, path, video_name);
++    snprintf(video_file, sizeof(video_file), "%s/%s", path, video_name);
+     Debug(1,"Writing video file to %s", video_file );
+
+     /* X264 MP4 video writer */
diff --git a/system/ZoneMinder/fix_zone_area_calc.patch b/system/ZoneMinder/fix_zone_area_calc.patch
new file mode 100644
index 0000000000..36cf8bb667
--- /dev/null
+++ b/system/ZoneMinder/fix_zone_area_calc.patch
@@ -0,0 +1,35 @@
+--- web/skins/classic/views/js/zone.js	2019-01-23 00:24:26.094085919 -0500
++++ web/skins/classic/views/js/zone.js.new	2019-01-23 00:25:13.871710907 -0500
+@@ -347,6 +347,7 @@
+   zone['Points'][index].x = x;
+   var Point = $('zonePoly').points.getItem(index);
+   Point.x = x;
++  updateArea();
+ }
+ 
+ function updateY( index ) {
+@@ -359,6 +360,7 @@
+   zone['Points'][index].y = y;
+   var Point = $('zonePoly').points.getItem(index);
+   Point.y = y;
++  updateArea();
+ }
+ 
+ function saveChanges( element ) {
+@@ -697,11 +699,12 @@
+   var n_coords = coords.length;
+   var float_area = 0.0;
+ 
+-  for ( i = 0, j = n_coords-1; i < n_coords; j = i++ ) {
+-    var trap_area = ( ( coords[i].x - coords[j].x ) * ( coords[i].y + coords[j].y ) ) / 2;
+-    float_area += trap_area;
+-    //printf( "%.2f (%.2f)\n", float_area, trap_area );
++  for ( i = 0; i < n_coords-1; i++ ) {
++      var trap_area = (coords[i].x*coords[i+1].y - coords[i+1].x*coords[i].y) / 2;
++      float_area += trap_area;
+   }
++  float_area += (coords[n_coords-1].x*coords[0].y - coords[0].x*coords[n_coords-1].y) / 2;
++
+   return Math.round( Math.abs( float_area ) );
+ }
+ 
-- 
2.14.5

