#!/bin/bash
########################################
# Ryan Pisani mythdora@thepisanis.com  #
#         August 23rd, 2007            #
# Desc:   Schedule Direct update for   #
#         mythdora.                    #
########################################

PATH=/bin:/usr/bin:/usr/sbin

sep() {
	if [[ "$1" -ne "1" ]]; then

		echo -e "=============================================================================="
	else

		echo -e "******************************************************************************"
	fi

}

sep
cat <<STOP

This script will migrate your current MythDora installation to use the new SD
data source. It will update xmltv and dependencies via yum from a combination
of the MythDora and Atrpms repositories. 

It will then walk you through configuring xmltv and finally it will create a
mythfilldb.sh tool that will handle updates going forward. 

Please use at your own risk. This has been tested on MD 3.2 without issue.

STOP
read -a WAIT -p "Press Enter to Continue:"
 
sep
echo -e "Checking for root permissions...."
sleep 1
if [[ `id -u` -ne '0' ]]; then 

	echo -e "\n!!You must run this tool as root!!\n"

fi

sep
echo -e "Checking for mysql permissions...."
sleep 1

if !(echo select \* from videosource\; | mysql mythconverg >/dev/null 2>&1); then

	echo -e "\n!!Could not confirm roots access to mythconverg!!\n"  
	exit

fi

sep

#First thing we do is setup some repos to use for yum upgrade disabled by default
cat >/etc/yum.repos.d/mythdoratemp.repo<<STOP
[mythdoratemp]
name=Mythdora atrpms release
baseurl=http://dl.atrpms.net/mythdora/4.0/i386/os
gpgcheck=0
enabled=0

[atrpmtemp]
name=Atrpm Repo for update
baseurl=http://dl.atrpms.net/fc6-i386/atrpms/stable
gpgcheck=0
enabled=0
STOP


cat <<STOP

We will need to leech off of the MythDora 4.0 & ATRPMS FC6 yum repositories.
In testing this worked fine on MD 3.2 however, your mileage may vary....

Please proceed at your own risk, and pray that you do not get into 
dependency hell.

Good Luck

STOP
read -a WAIT -p "Press Enter to Continue:"

sep

echo -e "Cleaning the Yum cache"
sleep 2
sep

yum clean all

sep

cat <<STOP

The following packages will be upgraded from the mythdora 4.0 repo:

               perl perl-XML-Twig nuvexport

Some dependencies such as glibc will probably be upgraded at this as well
depending on which MythDora release you are currently using.

STOP

sep

sleep 5

if !(yum -y --disablerepo=\* --enablerepo=mythdoratemp update perl perl-XML-Twig nuvexport); then

	echo -e "\nThe initial yum update failed in some way. We cannot continue to retrieve xmltv updates\n"
	echo -e "Check the error output from above and see if you can manually remedy the issue\n"
	exit

else

sep

cat <<STOP

The following packages will be upgraded from the atrpms FC6 repo:

    perl-XML-Writer perl-XMLTV perl-gui xmltv xmltv-grabbers

Some dependencies may also install at this time.

STOP

sep

	if !(yum -y --disablerepo=\* --enablerepo=atrpmtemp update perl-XML-Writer perl-XMLTV xmltv-gui xmltv xmltv-grabbers); then

		echo -e "\nThe yum update of xmltv components failed in some way. We cannot continue.\n"
        	echo -e "Check the error output from above and see if you can manually remedy the issue\n"
        	exit

	fi

fi

sleep 2

sep

cat <<STOP

Now that we have successfully retrieved the latest xmltv-grabbers package we
must configure it. To do so you must answer the appropriate questions regarding
your schedules direct subscription & lineups. 

We will start by query your mythconverg database to see how many videosources 
you're currently using; and then configure tv_grab_na_dd appropriately. 

If you have more than 1 source you will be prompted to configure each of them.

Make sure you choose the SD lineup that corresponds with the DD lineup that
this script will display to you. 

STOP

read -a WAIT -p "Press Enter to Continue:"

sep

#Create dummy mythfilldb.sh file to append to later
cat >/usr/bin/mythfilldb.sh<<STOP
#!/bin/bash

DATE=\`date +%d%m%Y\`
rm -rf /tmp/dd-*xml >/dev/null 2>&1

STOP

chmod 755 /usr/bin/mythfilldb.sh

echo -e "Checking mythconverg for the number of video sources..."
sleep 1

sep

SOURCES=(`echo select \* from videosource\; | mysql mythconverg | grep -v source | awk {'print $1'}`)

SOURCENAME=(`echo select \* from videosource\; | mysql mythconverg | grep -v source | awk '{print $6}'`)

sep

echo -e "Creating xmltv settings dir in /var/mythdora"

mkdir -p /var/mythdora/.xmltv >/dev/null 2>&1

chown mythtv /var/mythdora/.xmltv >/dev/null 2>&1

sep

x=0
for i in ${SOURCES[@]}; do 

	sep 1
	sep 1
	echo -e "\nNow configuring for data source ${SOURCENAME[$x]}\n"
	echo -e "Make sure you choose ${SOURCENAME[$x]} when configuring xmltv to work with SD.\n"
	sep 1
	sep 1
	read -a WAIT -p "Press Enter to Continue:"

	/usr/bin/tv_grab_na_dd --configure --config-file /var/mythdora/.xmltv/scheddirect_${SOURCENAME[$x]}	
	
	#Now let's add the newly configured xmltv setup to the new mythfilldb.sh script
	echo -e "/usr/bin/tv_grab_na_dd --config-file /var/mythdora/.xmltv/scheddirect_${SOURCENAME[$x]} --days 13 --download-only --dd-data /tmp/dd-$i-\$DATE.raw.xml\n\n" >>/usr/bin/mythfilldb.sh

	echo -e "cat /tmp/dd-$i-\$DATE.raw.xml | sed -e \"s/\(\<program id=...\)00/\\\1/\" -e \"s/\(\<schedule program=...\)00/\\\1/\" -e \"s/\(\<programGenre program=...\)00/\\\1/\" -e \"s/\(<series\>..\)00/\\\1/\" > /tmp/dd-$i-\$DATE.xml\n\n" >>/usr/bin/mythfilldb.sh

        echo -e "/usr/bin/mythfilldatabase --do-channel-updates --refresh-all --dd-file $i -1 ${SOURCENAME[$x]} /tmp/dd-$i-\$DATE.xml" >>/usr/bin/mythfilldb.sh 

	let x="$x + 1"
done

sep

cat <<STOP

We will now make a change to the MythFillDatabasePath attribute in the settings 
table. This will effectively replace /usr/bin/mythfilldatabase, which should be
currently handling updates with /usr/bin/mythfilldb.sh. 

If you have not had Mythtv doing the updates automatically you should go
turn on automatic listing updates under Settings -> General in the Mythtv
frontend.

The /usr/bin/mythfilldb.sh script contains the tv_grab_na_dd tool and the
mythfilldatabase commands to manually grab and populate your listings going
forward.

STOP
read -a WAIT -p "Press Enter to Continue:"

sep

echo -e "Making a change to the MythFillDatabasePath setting in the settings table"
sleep 2
	
sep

#Now let's update the mythconverg mythfilldb path
echo "UPDATE settings SET data='/usr/bin/mythfilldb.sh' WHERE value = 'MythFillDatabasePath';" | mysql mythconverg

sep

cat <<STOP

Now we'll run the /usr/bin/mythfillldb.sh script to retrieve listings.

You may notice some errors in regards to mysql duplicate entries; This is ok
just feel free to ignore them.

Oh...This may take a little while too. So don't kill it, it's not hung!

STOP
read -a WAIT -p "Press Enter to Continue:"
sep

/usr/bin/mythfilldb.sh

echo -e "Configuration complete"
