#!/bin/sh

# Patcher for R5F1

# Fix E50->F1 upgrade bugs and F1 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 old qtrc is being restored.  Tweak it in GlobalTweaker.sh if it
# had no settings for anti-aliased fonts.

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

if [ `grep -c enableXft $GLOBALTWEAKER` -eq 0 ]; then
    echo "#" >> $GLOBALTWEAKER
    echo "# patcher.sh" >> $GLOBALTWEAKER
    echo "#" >> $GLOBALTWEAKER
    echo "if [[ \`grep -c enableXft $QTRC\` -eq 0 ]] && [[ \`grep -c enableXft $QTRC\` -eq 0 ]];" then >> $GLOBALTWEAKER
    echo    "# There are no AA settings in this qtrc.  Let's fix that and use a good AA" >> $GLOBALTWEAKER
    echo    "# font." >> $GLOBALTWEAKER
    echo    "sed -i \"s/embedFonts=true/embedFonts=true\nenableXft=true\nuseXft=true/g\" $QTRC" >> $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

# restore list patches
# --

# Restores the soundcard state file between upgrades.
sed -i.old \
    -e '/^BACKUP_LIST=/s/\.htaccess"$/.htaccess .\/var\/lib\/alsa\/asound.state"/' \
    -e '/^RESTORE_LIST=/s/asound\.conf"$/asound.conf .\/etc\/default\/aumix .\/var\/lib\/alsa\/asound.state"/' \
    /usr/local/bin/backupcommon 

# Dragon tweaks
# --

# XvMC tested well in terms of CPU usage, but Standard is still the best decoder for performance.
sed -i "s|xvmc|ffmpeg|g" /usr/bin/DragonSQLtweaker.sh

# Blootube's OSD is too gorgeous not to use by default.
sed -i "s|isthmus|blootube-osd|g" /usr/bin/DragonSQLtweaker.sh

# Allow any thumbnail to scale properly in MythWeb's video view
echo    "sed -i 's/94);/\"100%\");/g' /usr/share/mythtv/mythweb/includes/config.php" >> $GLOBALTWEAKER
echo    "sed -i 's/140);/\"100%\");/g' /usr/share/mythtv/mythweb/includes/config.php" >> $GLOBALTWEAKER


# channel icons
# --

# Recent installs of E50 have failed to download channel icons for
# North America.  Adjust the icon fix code block in KnoppMyth-run to
# handle this, and restart KnoppMyth-run after letting the user know.

KNOPPMYTHRUN=/usr/local/bin/KnoppMyth-run

# See if we've already applied this patch
if [ `grep -c "there are no icons" $KNOPPMYTHRUN` -eq 0 ]; then
    # first part of new logic block
    sed -i "s|\(perl -e 'while(<>).*\)|\1\nif [ \`grep -c \\\/ /tmp/iconfiles.txt\` -eq 0 ]; then # there are no icons\nall_icons_exist=0;\nelse\n|g" $KNOPPMYTHRUN

    # last part of new logic block
    sed -i "s|\(.*\$all_icons_exist.*\)|fi\n\1|g" $KNOPPMYTHRUN

    # These patches are only necessary for upgrades.  Apply them anyway, but there's no need to 
    # re-run KnoppMyth-run unless we're on an upgrade.
    if [ -f "$UpgradeFile" ]; then
	NEED_TO_RESTART_X=1
    fi
fi

# Do we have a fake sound device?
if [[ `lsmod | grep -c cx88_alsa` -gt 0 ]]; then
    # Prevent devices like the pcHDTV hd-5500 from pretending to be the primary sound device
    if [[ `grep -c cx88-alsa /etc/modprobe.d/alsa-base` -lt 2 ]]; then
	echo "options cx88-alsa index=-2,-2,-2,-2,-2" >> /etc/modprobe.d/alsa-base
	echo "options cx88-alsa enable=false,false,false,false,false" >> /etc/modprobe.d/alsa-base
	NEED_TO_REBOOT=1
    fi
fi

# 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 of the upgrade process.
*** 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 of the upgrade
*** process.  Press ENTER to continue."
    read CONTINUE;
    
    pkill xinit;
fi
