From 62edd0d8966eeccbc1ebd4520dfac40e23e068db Mon Sep 17 00:00:00 2001
From: Britney Fransen <brfransen@gmail.com>
Date: Sun, 19 Feb 2023 20:15:52 -0500
Subject: linhes-system: add recordings.cron

lh_mtc.cron: run backup as root
---
 linhes/linhes-system/PKGBUILD             |  10 +-
 linhes/linhes-system/lh_mtc.cron          |   2 +-
 linhes/linhes-system/lh_system_backup_job |   2 +-
 linhes/linhes-system/recordings.cron      | 146 ++++++++++++++++++++++++++++++
 4 files changed, 154 insertions(+), 6 deletions(-)
 create mode 100644 linhes/linhes-system/recordings.cron

diff --git a/linhes/linhes-system/PKGBUILD b/linhes/linhes-system/PKGBUILD
index 8ed8b1f..eceb676 100755
--- a/linhes/linhes-system/PKGBUILD
+++ b/linhes/linhes-system/PKGBUILD
@@ -1,6 +1,6 @@
 pkgname=linhes-system
 pkgver=9.0.0
-pkgrel=47
+pkgrel=48
 arch=('x86_64')
 #install=$pkgname.install
 pkgdesc="Everything that makes LinHES a system"
@@ -24,7 +24,7 @@ source=($binfiles
     '79-cronie.hook' 'fstrim.hook' 'openssh.hook' 'plex_lib.conf' 'rsyslog.hook'
     '10-monitor.conf' 'x11vnc.override.conf' 'lh_lighttpd.conf' 'lh_php.ini'
     'system-sudo.rules' 'linhes-profile.sh' 'lh_sqlserver.cnf'
-    'lh_system_start.sh.desktop' 'rc6_mce.toml' 'rsyslog.mythtv.conf')
+    'lh_system_start.sh.desktop' 'rc6_mce.toml' 'recordings.cron' 'rsyslog.mythtv.conf')
 sha256sums=('525bfe29b63d3ec5a17a32fa29745e24070020490c3f5b6dd6b03250348fb324'
             '1ec3c266cafb0c1b231e88df88b97fec1a1b7465b5d95a0c1e64c8cb727b7c47'
             '97fe9e851c782fa9f85c5b69b110ccff2817dd4fa2a6d9ff6ee225dc558677e4'
@@ -37,7 +37,7 @@ sha256sums=('525bfe29b63d3ec5a17a32fa29745e24070020490c3f5b6dd6b03250348fb324'
             '87875d9e5f5ce18208f419698ce69b6bcbcd08955a57a4a13940e715af58b787'
             '93d664f4a46fda05d0f754d19df40cbda120e325b80c07092345b14763993833'
             '8d6d09cedc85e49712144699ccb905be6ef663e9ab0bc01abd434370ab12fede'
-            'c7a6abe41a89acd436d71fd93dedc2f499643718b5c792269cb7c3c8ffc64ade'
+            '97a0ceb3a6ca667563becd42d9808a5077478da28245bf08d5c4fce9aa697067'
             '91bdec992bb2c933e15625c181f2195c402060b879168ebf35944cb064c904b9'
             '5cacfdd02833e5a3130d765573e772e6bd5030336ba86239c5e4db5ffa36fc69'
             'ebdb3ee0212e0cc72526bb5e50a032573e1894acb7bf75617243b0b49aa1f8f2'
@@ -55,7 +55,7 @@ sha256sums=('525bfe29b63d3ec5a17a32fa29745e24070020490c3f5b6dd6b03250348fb324'
             'a961cfdc6f02b12fb445777dd2c144fed96306ca2f430cc8853ae307c759c1ad'
             'd8574104b75c6d41284488612ec5583c50a8dab438492fa42c47231add4cfc54'
             '6bdbf593d3e1348d1a8f7c4c17cb2e893f7e18ae355daf978173e669cfe3be80'
-            'b3f02ec3f8cedc98c74b3169049b7b6aa78bd79d558a8bc98d00e064b983965c'
+            '2fe78433b7b671d031450741d37167e3a20524a40e051a28643768c861555723'
             '186203d3c0520bb3d611da99d33a7713e9c1563814285f1f101097234f214b2f'
             'd8d36a501928d0cc505957d392291fad317b1e895ff99847d90643cf5f622a89'
             'cdfc0c836b8194f631f4a9e022c232ff75a13ff1a161a1a011858578bea5f930'
@@ -77,6 +77,7 @@ sha256sums=('525bfe29b63d3ec5a17a32fa29745e24070020490c3f5b6dd6b03250348fb324'
             '3ed91fb5a7894f82fb4895e06d2e3f1df3ac4f82e46c970d4a85aaa4edc24cf1'
             '0b9868a563036c81f8fdb8ab8bbad51934aca2a07e9d7634e24214791afda8e2'
             '1b965b5e7eeafdf3815c8f2722587a560693dd780327cca9910dc47fba0f1aef'
+            'aa670bc7aeed907b0f1c92a2e158fc17145e6e533f7aec9ad9c9a28c2fbdaa2e'
             '2b91f6eb8c010a0dce1f41149c0549d067915fba93251c7af7e5328a05977f0c')
 
 package() {
@@ -132,6 +133,7 @@ package() {
     install -Dm755 $srcdir/lh_mtc.cron $pkgdir/etc/cron.hourly/lh_mtc
     install -Dm755 $srcdir/flatpak_update.cron $pkgdir/etc/cron.daily/flatpak_update
     install -Dm755 $srcdir/lh_log_care.cron $pkgdir/etc/cron.daily/lh_log_care
+    install -Dm755 $srcdir/recordings.cron $pkgdir/etc/cron.hourly/recordings
 
     #hooks
 	install -Dm0644 $srcdir/79-cronie.hook "${pkgdir}"/usr/share/libalpm/hooks/79-cronie.hook
diff --git a/linhes/linhes-system/lh_mtc.cron b/linhes/linhes-system/lh_mtc.cron
index b556d60..4ab62eb 100644
--- a/linhes/linhes-system/lh_mtc.cron
+++ b/linhes/linhes-system/lh_mtc.cron
@@ -50,7 +50,7 @@ then
 
     if ! grep -q "Finished Backup" $logFile && grep -q "Finished Optimize" $logFile
     then
-        sudo -u mythtv bash -c "MYTHCONFDIR=/usr/share/mythtv unbuffer lh_mtc.py --backup | $log"
+        MYTHCONFDIR=/usr/share/mythtv unbuffer lh_mtc.py --backup | $log
         if [ $? = 0 ]
         then
             echo "" | $log
diff --git a/linhes/linhes-system/lh_system_backup_job b/linhes/linhes-system/lh_system_backup_job
index 257bb87..0c8b468 100755
--- a/linhes/linhes-system/lh_system_backup_job
+++ b/linhes/linhes-system/lh_system_backup_job
@@ -37,7 +37,7 @@ function backup(){
     mkdir -p $BACKUPDIR/$DATE
 
     #backup database
-    if [ $SystemType = Master_backend -o  $SystemType = Standalone ]
+    if [ $SystemType = MasterBackend -o $SystemType = Standalone ]
     then
         echo
         echo "Backup mysql databases"
diff --git a/linhes/linhes-system/recordings.cron b/linhes/linhes-system/recordings.cron
new file mode 100644
index 0000000..387910f
--- /dev/null
+++ b/linhes/linhes-system/recordings.cron
@@ -0,0 +1,146 @@
+#!/bin/bash
+
+#START=$(date +%s)
+
+#check if mythbackend is running and was just started
+for i in 1 2
+do
+if [ `ps -ef | grep mythbackend |grep -v "grep" | wc -l` ]
+then
+    now=$(date +%s)
+    mythbackendStartTime=`systemctl status mythbackend.service | grep Active| cut -d ' ' -f 11-12`
+    mythbackendStartTime=`date -d"$mythbackendStartTime" +"%s"`
+    if [[ $(( $now - $mythbackendStartTime )) -lt 59 ]]
+    then
+        #echo "mythbackend started less than a minute ago. Sleeping..."
+        sleep 60
+    fi
+else
+    #echo "mythbackend not running. exiting."
+    exit
+fi
+done
+
+if [ -f /usr/share/mythtv/contrib/user_jobs/mythlink.pl ]
+then
+    recdir="/data/storage/disk0/media/recordings"
+    tmprecdir="/data/storage/disk0/media/tmp/recordings"
+    rm -r $tmprecdir
+    su - mythtv -c "perl /usr/share/mythtv/contrib/user_jobs/mythlink.pl --format '%Ct/%U/%T/%T %- S%ssE%ep %- %oY-%om-%od = %S' --link '$tmprecdir'"
+
+    # rename category_types (%Ct) from numbers to names
+    for cattype in $tmprecdir/*
+    do
+        if [ $cattype == "$tmprecdir/1" ]
+        then
+            rsync -a "$cattype/" "$tmprecdir/Movies"
+            rm -r "$cattype"
+        elif [[ $cattype == "$tmprecdir/2" ]] || [[ $cattype == "$tmprecdir/4" ]]
+        then
+            rsync -a "$cattype/" "$tmprecdir/TV Shows"
+            rm -r "$cattype"
+        elif [ $cattype == "$tmprecdir/3" ]
+        then
+            rsync -a "$cattype/" "$tmprecdir/Sports"
+            rm -r "$cattype"
+        else
+            #ignore Movies, TV Shows, Sports. Move all others to TV Shows
+            if [[ $cattype != "$tmprecdir/Movies" ]] && [[ $cattype != "$tmprecdir/TV Shows" ]] && [[ $cattype != "$tmprecdir/Sports" ]]
+            then
+                if [ ! -d "$tmprecdir/TV Shows" ]
+                then
+                    mkdir "$tmprecdir/TV Shows"
+                fi
+                rsync -a "$cattype" "$tmprecdir/TV Shows"
+                rm -r "$cattype"
+            fi
+        fi
+    done
+
+    #delete Deleted recgroup
+    for link in $tmprecdir/**/Deleted
+    do
+        rm -r "$link"
+    done
+
+    #move all links in recgroup dirs out to parent dir
+    for recgroup in $tmprecdir/**/*
+    do
+        if [ -d "$recgroup" ]
+        then
+            cd "$recgroup"
+            rsync -a "$recgroup/" ..
+            cd "$tmprecdir"
+            rm -r "$recgroup"
+        fi
+    done
+
+    #replace SE if no season/episode is in myth
+    for link in $tmprecdir/**/**/*\ -\ SE\ -\ *
+    do
+        newlink=`echo "$link" | sed 's/ - SE - / - /'`
+        mv "$link" "$newlink"
+    done
+    #replace SEyy if no season is in myth
+    for link in $tmprecdir/**/**/*\ -\ SE[0-9][0-9]\ -\ *
+    do
+        newlink=`echo "$link" | sed 's/ - SE/ - S00E/'`
+        mv "$link" "$newlink"
+    done
+    #replace SyyE if no episode is in myth
+    for link in $tmprecdir/**/**/*\ -\ S[0-9][0-9]E\ -\ *
+    do
+        newlink=`echo "$link" | sed 's/E - /E00 - /'`
+        mv "$link" "$newlink"
+    done
+    #replace blank original date
+    for link in $tmprecdir/**/**/*\ -\ 0000-00-00\ -\ *
+    do
+        newlink=`echo "$link" | sed 's/ - 0000-00-00 - / - /'`
+        mv "$link" "$newlink"
+    done
+    #add dash pt suffix if filename before the subtitle is the same
+    #so that plex will scan and include in library
+    uniqs="$(ls $tmprecdir/**/**/* | sed 's/ = .*//' | sort | uniq -d)"
+    SAVEIFS=$IFS
+    IFS=$'\n'
+    for link in $uniqs
+    do
+        i=1
+        for dup in `ls -v $link*`
+        do
+            newlink=`echo "$dup" | sed "s/ = /-pt$i = /"`
+            mv "$dup" "$newlink"
+            i=$((i+1))
+        done
+    done
+    IFS=$SAVEIFS
+
+    #change symlinks mtime to match the file it is linked to
+#    for link in $tmprecdir/**/*
+#    do
+#        if [ -L "$link" ]
+#        then
+#            file=`readlink "$link"`
+#            touch -hr "$file" "$link"
+#        fi
+#    done
+
+    #sync tmprecdir to recdir
+    #rsync -aOP --delete --ignore-existing "$tmprecdir/" "$recdir/"
+    rsync -aO --delete "$tmprecdir/" "$recdir/"
+
+    #check if plex media server is running
+    if [[ `pidof "Plex Media Server"` ]]
+    then
+        #get plex section and update
+        script -q -c '/usr/bin/plexmediascanner.sh -l' | grep -i myth | cut -d: -f1 | while read -r line
+        do
+            /usr/bin/plexmediascanner.sh --scan --refresh --section $line
+        done
+    fi
+fi
+
+#END=$(date +%s)
+#DIFF=$(( $END - $START ))
+#echo "It took $DIFF seconds"
-- 
cgit v0.12

