summarylogtreecommitdiffstats
path: root/wgpp.sh
blob: c5b3101f1245531a6145b268ed46ecfb5a1a8012 (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
#!/bin/bash
# script: wg++ (WebGrab+Plus)
# author: Nikos Toutountzoglou, nikos.toutou@protonmail.com
# rev.date: 21/04/2024

# vars
WGPP_USR=$(whoami)
WGPP_EXE=$(basename $0)
WGPP_USR_HOME=$(getent passwd "$WGPP_USR" | cut -d: -f6)
WGPP_CFGDIR=$WGPP_USR_HOME/wg++
WGPP_SYS=/usr/share/wg++

# functions
helpMsg() {
	echo "WebGrab+Plus EPG/XMLTV grabber"
	echo "Usage: $WGPP_EXE [-d|--dir <CUSTOM_DIR>] [-g|--generate] [-d <CUSTOM_DIR> -g]"
	echo "       $WGPP_EXE -h|-?|--help"
	echo
	echo "Options:"
	echo "  -d <CUSTOM_DIR>         Run from custom configuration folder <CUSTOM_DIR>."
	echo "  -g                      Create new configuration folder 'wg++' in user's home directory."
	echo "  -d <CUSTOM_DIR> -g      Create new custom configuration folder <CUSTOM_DIR>."
}

missingSysFiles() {
	if [ ! -e "$WGPP_CFGDIR/install.sh" -o ! -e "$WGPP_CFGDIR/run.net.sh" ]; then
		cp -r -u $WGPP_SYS/* "$WGPP_CFGDIR" 2>/dev/null
		echo ":: Restored missing script files 'install.sh' and/or 'run.net.sh'."
	fi
}

missingWGPPDir() {
	if [ ! -d "$WGPP_CFGDIR" ]; then
		echo ":: No '$WGPP_EXE' working directory found for user '$WGPP_USR', exiting."
		exit 1
	fi
}

custFolder() {
	if [[ -z "$cust_dir" ]]; then
		echo ":: Empty custom directory input, exiting."
		exit 1
	fi
	WGPP_CFGDIR=$(realpath "$cust_dir")
}

genFolder() {
	if [ ! -d "$WGPP_CFGDIR" ]; then
		cp -r $WGPP_SYS "$WGPP_CFGDIR"
		cd "$WGPP_CFGDIR" && sudo -u $WGPP_USR ./install.sh
		echo ":: Configuration folder '$WGPP_CFGDIR' created."
		echo ":: It's now possible to configure 'WebGrab++.config.xml' and re-run '$WGPP_EXE' to generate EPG data."
		exit 0
	else
		echo ":: Configuration folder '$WGPP_CFGDIR' already exists, exiting."
		exit 1
	fi
}

runScript() {
	cd "$WGPP_CFGDIR"
	# workaround for 'No Internet' issue
	sudo ./run.net.sh
	#sudo -u $WGPP_USR ./run.net.sh

	# output 'guide.xml' file with pretty XML format
	if [ ! -e "$WGPP_CFGDIR/latest.xml" ]; then
		echo ":: Error, cannot find latest EPG XML data file named 'latest.xml'. Exiting."
		exit 1
	fi
	xmllint --format latest.xml > guide.xml
	exit 0
}

# args
args=("$@")

while [ $# -ne 0 ]
do
	case "$1" in
		-d|--dir|-[Dd]ir)
			shift
			cust_dir="$1"
			custFolder
			;;
		-g|--generate|-[Gg]enerate)
			genFolder
			;;
		-?|--?|-h|--help|-[Hh]elp)
			helpMsg
			exit 1
			;;
		*)
			echo "Unknown argument '$1'"
			exit 1
			;;
	esac
	shift
done

# run script
missingWGPPDir
missingSysFiles
runScript