Project

General

Profile

Picons support (was: Helper script: matchIcons.sh...) » matchIcons.sh

matchIcons.sh bash script - Dietmar Konermann, 2014-01-17 16:16

 
1
#!/bin/bash
2
# matchIcons.sh, [email protected], 20140116
3
#
4
# Takes a Tvheadend configuration and a directory containing channel icons (png), scans
5
# the channels in the config and tries to match them to the icon file names, creates
6
# symlinks in the icon directory for non-exact matches, writes file:// URLs to the
7
# configuration. 
8
# The orignal channel configuration is NOT touched by default. Instead we work on 
9
# a copy that needs to be moved into place manually before restarting Tvheadend.
10
#
11
# Tested only on recent Tvheadend 3.9.x versions (dvb rewrite)!
12
# Feedback welcome!
13
#
14

    
15
# config section
16

    
17
htsconf="/home/hts/.hts/tvheadend"
18
newchannel="$htsconf/channel.new$$"
19
icondir="/home/hts/channelicons"
20

    
21

    
22
# function secion
23

    
24
function simplify {
25
	echo "$1" | sed -e 's/[^a-zA-Z0-9]//g' -e 's/\(.*\)/\L\1/' -e 's/.*\///'
26
}
27

    
28
function exist {
29
	[ -f "$1" -o -h "$1" ] && [ -s "$1" ]
30
}
31

    
32
iconlist=$(
33
	ls -1 "$icondir" | 
34
	while read line; do
35
		if [[ "$icondir/$line" == *.png ]] && exist "$icondir/$line"; then
36
			printf "%s\t%s\n" "$line" $(simplify "${line%.png}") 
37
		fi
38
	done
39
)
40

    
41
function name2icon {
42
        simplename=$(simplify "$1")
43
	echo "$iconlist" | awk -F'\t' -vname="$simplename" '{if ($2==name) print $1;}' | head -1
44
}
45

    
46
function service2name {
47
	grep '"svcname"' "$htsconf/input/linuxdvb/networks/"*"/muxes/"*"/services/$1" |
48
	awk -F\" '{print $4}' |
49
	sed -e 's/\//-/g'
50
}
51

    
52
function patchicon {
53
	channelfile="$1"
54
	iconfile="$2"
55

    
56
	exist "$channelfile" || return
57
	exist "$iconfile" || return
58

    
59
	url=$(echo "file://$iconfile" | sed -e 's/"/\\"/g')
60
	
61
	savetxt=$(grep -v -Ee '{|}|"icon"' "$channelfile")
62
	echo '{' >"$channelfile"
63
	echo "        \"icon\": \"$url\"," >>"$channelfile"
64
	echo "$savetxt" >>"$channelfile"
65
	echo '}' >>"$channelfile"
66
}
67

    
68
############## MAIN
69

    
70
[ -d "$newchannel" ] && echo "$newcchannel exists!" && exit 1
71

    
72
echo "Copying current channel dir to $newchannel..."
73
cp -rp "$htsconf/channel" "$newchannel" || exit 1
74

    
75
ls -1 "$newchannel/"* |
76
while read channelfile; do
77
	exist "$channelfile" || continue
78

    
79
	name=$(service2name $(grep -A 1 '"services"' "$channelfile" | tail -1 | awk -F\" '{print $2}'))
80

    
81
	icon=$(name2icon "$name")
82
	! exist "$icondir/$icon" && icon=$(name2icon "${name% [hH][dD]}")
83

    
84
	echo "Processing '$name'..."
85
	if exist "$icondir/$icon"; then
86
		if ! exist "$icondir/$name.png"; then
87
			echo "  Creating symlink $icondir/$name.png -> $icondir/$icon"
88
			ln -sf "$icondir/$icon" "$icondir/$name.png"
89
		fi
90
		echo "  Setting URL 'file://$icondir/$name.png'..."
91
		patchicon "$channelfile" "$icondir/$name.png"
92
	else
93
		echo "  No icon found for '$name'."
94
	fi
95
done
96

    
97
echo "
98
Finished creating $newchannel.
99
Next steps: move in place and restart tvheadend.
100
Enjoy!
101
"
(1-1/2)