summaryrefslogtreecommitdiffstats
path: root/abs/core/mythtv/stable-0.28/mythtv/recordings
blob: 984fea9870f4d64bb55831c6222a13a0ebf79dd4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#!/bin/bash

#START=$(date +%s)

#check if mythbackend is running and was just started
for i in 1 2
do
if [ `cat /service/mythbackend/supervise/pid` ]
then
    now=$(date +%s)
    mythbackendStartTime=`stat -c %Y /service/mythbackend/supervise/pid`
    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 '%U/%T/%T %- S%ssE%ep %- %oY-%om-%od = %S' --link '$tmprecdir'"

    #delete Deleted recgroup and move files out of other dirs
    if [ -d "$tmprecdir/Deleted" ]
    then
        rm -r "$tmprecdir/Deleted"
    fi
    #move all links in recgroup dirs out to tmprecdir
    for recgroup in $tmprecdir/*
    do
        if [ -d "$recgroup" ]
        then
            rsync -a "$recgroup/" "$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
        /usr/LH/bin/plexmediascanner.sh -l | grep -i myth | cut -d: -f1 | while read -r line
        do
            /usr/LH/bin/plexmediascanner.sh --scan --refresh --section $line
        done
    fi
fi

#END=$(date +%s)
#DIFF=$(( $END - $START ))
#echo "It took $DIFF seconds"