summaryrefslogtreecommitdiffstats
path: root/linhes/linhes-system/recordings.cron
blob: d71c13973a683aa46a9e85a0f34988b5a2b5f494 (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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#!/bin/bash

#check if mythbackend is running and was just started
for i in 1 2
do
if [ $(pidof mythbackend) ]
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"