echo "[vxparser.sh] $*"

INPUT=$1
NAME=vxparser
#BIN=/usr/bin/python3.8
if [ $(uname -a | grep x86_64 | wc -l) == 1 ];then
	CLONEDIR="$HOME/epg"
else
	CLONEDIR="/tmp/localhoster/epg"
fi
#CMD=/home/atemio/.local/lib/python3.8/site-packages/vxparser
#DEST=/home/www-openaaf/epg
TMP=/tmp/epg

if [ $(uname -a | grep x86_64 | wc -l) == 1 ];then
#	CMD=$(find $HOME/.local/ -type d -name vxparser | head -n1)
	CMD=$(find /home/atemio/.local/lib/python3.11/ -type d -name vxparser | head -n1)
else
#	TMP=$1
	CMD=$(find /usr/lib/python*/ -type d -name vxparser | head -n1)
	if [ -z "$CMD" ];then
		opkg update
		opkg install python3
		wget https://bootstrap.pypa.io/get-pip.py -O /tmp/get-pip.py
		python3 /tmp/get-pip.py
		pip3 install --upgrade pip
		#reinstall
		pip3 install -I vxparser
	else
		pip3 install --upgrade vxparser
	fi
	CMD=$(find /usr/lib/python*/ -type d -name vxparser | head -n1)
fi

echo CMD $CMD
BIN=$(echo $CMD | sed -nr 's,/.*(python.*)/site-packages.*,\1,p')
echo BIN $BIN
DEST=/var/www/openaaf/epg

if [ -z "$CMD" ];then
	echo EXIT
	exit
fi

parse()
{
	rm $CMD/$NAME.py
	rm $CMD/$NAME.sh
	rm $CMD/epg.sh
	cp "$CLONEDIR/$NAME.py" "$CMD/$NAME.py"
	cp "$CLONEDIR/$NAME.sh" "$CMD/$NAME.sh"
	cp "$CLONEDIR/epg.sh" "$CMD/epg.sh"
	cd $CMD
	rm -r "$TMP"
	rm -r "$DEST"_old
	mkdir -p $TMP
echo $BIN $CMD/$NAME.py $INPUT
	$BIN $CMD/$NAME.py $INPUT
	cp $CMD/data/lists/epg.xml.gz $TMP
	cd $TMP
	gzip -fd $TMP/epg.xml.gz
	cp $CMD/data/lists/epg.xml.gz $TMP
	echo $CMD/epg.sh convert $TMP
	$CMD/epg.sh convert $TMP
	if [ $(uname -a | grep x86_64 | wc -l) == 1 ];then
		gzip $TMP/titan.epg
		cp -a $TMP "$DEST"_new
		if [ -e "$DEST"_new ]; then
			mv -f "$DEST" "$DEST"_old
			mv -f "$DEST"_new "$DEST"
		fi
	else
		rm $TMP/epg.xml*
	fi
}

parse $INPUT
