#!/bin/sh

# Patcher for R5F27

# Fix F1->F27 upgrade bugs and F27 installation bugs:
###

FirstTimeFile="/home/mythtv/.configure"
UpgradeFile="/home/mythtv/.upgrade"
NewCardFile="/home/mythtv/.newcard"

NEED_TO_RESTART_X=0
NEED_TO_REBOOT=0

# anti-aliased fonts
# --

# The new qtrc uses a bad font for AA.  Tweak it in GlobalTweaker.sh,
# forcing it to be Sans Serif.

GLOBALTWEAKER=/usr/bin/GlobalTweaker.sh
QTRC=/home/mythtv/.qt/qtrc

if [ `grep -c "Force Sans Serif" $GLOBALTWEAKER` -eq 0 ]; then
    echo "#" >> $GLOBALTWEAKER
    echo "# patcher.sh" >> $GLOBALTWEAKER
    echo "#" >> $GLOBALTWEAKER
    echo "# Force Sans Serif since it does AA" >> $GLOBALTWEAKER
    echo "if [[ \`grep -c \"Sans Serif\" $QTRC\` -eq 0 ]];" then >> $GLOBALTWEAKER
    echo    "sed -i \"s/font=.*/font=Sans Serif,12,-1,5,50,0,0,0,0,0/g\" $QTRC" >> $GLOBALTWEAKER
    echo "fi" >> $GLOBALTWEAKER
fi

# Remove dangerous and broken move_recordings.sh script
/bin/rm -f /usr/local/bin/move_recordings.sh

# Fix XMLTV bug by upgrading it
cd /root
wget http://internap.dl.sourceforge.net/sourceforge/xmltv/xmltv-0.5.48.tar.bz2
tar xjf xmltv-0.5.48.tar.bz2
cd xmltv-0.5.48/
perl Makefile.PL PREFIX=/usr --default
make
make install
cd /usr/share/perl5
/bin/mv -f XMLTV XMLTV.orig
/bin/mv -f XMLTV.pm XMLTV.pm.orig

# Use a fixed listings grabber for Australia
# Thanks to Better Access (manicmike) for hosting this
cd /usr/bin
/bin/mv -f tv_grab_au tv_grab_au.orig
wget --timeout=30 http://www.better-access.com/KM/tv_grab_au_reg
/bin/mv -f tv_grab_au_reg tv_grab_au
chmod a+x tv_grab_au

# Use resuming mplayer as default player for Dragon
sed -i "s/mplayer -fs/mplayer-resumer.pl -fs/g" /usr/bin/DragonSQLtweaker.sh

# Fix a bug if someone runs shootscreens.sh by hand
sed -i "s/OSD=1/OSD=1\nelse\n    OSD=0/g" /usr/local/bin/shootscreens.sh

# Add the KnoppMyth menu to the Classic MythTV menu layout
tail -48 /usr/src/mythtv/source/knoppmyth_tv_0.20.0-9_km.diff | patch -d /usr/share/mythtv -p1

# Import Files in optical disk menu failed horribly when no media was
# present.  This new version fixes that.
cd /usr/local/bin
/bin/rm -f importfiles.sh
wget ftp://ftp.knoppmyth.net/R5/R5F27/importfiles.sh
chmod a+x importfiles.sh

# Onboard audio for Biostar TF7025/7050 and other motherboards needs
# Realtek ALSA drivers.
if [[
	    (`lspci -mn | grep -c -e ".*0403.*10de.*055c.*-ra1.*1565.*820c.*"` -ge 1) ||
	    (`lspci -mn | grep -c -e ".*0403.*8086.*293e.*-r02.*1043.*829f.*"` -ge 1)
	    ]]; then
    echo "Your sound device needs newer Realtek ALSA drivers."
    echo "Downloading and installing them now."
    cd /tmp
    /bin/mv /lib/modules/2.6.18.chw-13/updates/alsa .
    # The numeric IP address comes from a link on Realtek's site
    wget ftp://209.216.61.149/pc/audio/realtek-linux-audiopack-4.06b.tar.bz2
    tar xfj realtek-linux-audiopack-4.06b.tar.bz2
    cd realtek-linux-audiopack-4.06b
    sed -i "s/^\(alsaconf\)/#\1/g" install
    ./install
    NEED_TO_REBOOT=1
fi

# Fix missing i2c-id.h
cd /usr/src/linux-headers-2.6.18-chw-13/include/linux/
/bin/rm -f i2c-id.h
wget http://lm-sensors.org/svn/i2c/trunk/kernel/i2c-id.h

# If user is using a remote control with LIRC, grab this newer version of ir-kbd-i2c
#cd /tmp
#wget ftp://ftp.knoppmyth.net/R5/R5F27/ir-kbd-i2c.ko
#cd /lib/modules/2.6.18-chw-13/kernel/drivers/media/video/
#/bin/mv ir-kbd-i2c.ko ir-kbd-i2c.ko-noremote
#/bin/mv /tmp/ir-kbd-i2c.ko ir-kbd-i2c.ko
#NEED_TO_REBOOT=1

# Change bad DEVICE file references in some hardware.conf files from /dev/usb/hiddev0 to lirc0
echo "Changing bad DEVICE file references in some hardware.conf files from /dev/usb/hiddev0 to lirc0..."
find /usr/local/share/knoppmyth/remotes -name 'hardware.conf' -exec grep hiddev0 {} \; -exec sed -i "s/DEVICE=.*/DEVICE=\"\/dev\/lirc0\"/g" {} \;

# Reboot if any of the above fixes will only take effect after
# a reboot.

if [ $NEED_TO_REBOOT -eq 1 ]; then
    echo "*** The KnoppMyth patcher has made some changes that
*** will not take effect until a reboot.  After the reboot, you
*** may be prompted to redo some steps.
*** Press ENTER to reboot."
    read CONTINUE;
    
    reboot;
fi

# Restart X if any of the above fixes will only take effect after
# X restarts.  This has the side-effect of re-running KnoppMyth-run.

if [ $NEED_TO_RESTART_X -eq 1 ]; then
    echo "*** The KnoppMyth patcher has made some changes to
*** running programs and needs to restart them.
*** You may be prompted to redo some steps.
*** Press ENTER to continue."
    read CONTINUE;
    
    pkill xinit;
fi
