XBMC and overlapping subtitles with iPlayer addon

There's often overlapping subtitles with XBMC and BBC iPlayer addon. Rather than modify actual addon and losing changes every time it's updated I hacked up script that edits SRT file after it's downloaded from BBC site but before XBMC reads it.

# Install inotify-tools
apt-get update
apt-get -y install inotify-tools

# Download perl script to mangle sub timings
cd /home/xbmc
wget http://dump.asiantuntijakaveri.fi/le_bueno_dumpo/subeau.pl
#wget https://subeautifuler.googlecode.com/svn/trunk/src/subeau.pl

# Create background processing script
cat <<'__EOF__' >/home/xbmc/iplayersubfix.sh
# Default location for iplayer subs
cd "/home/xbmc/.xbmc/userdata/addon_data/plugin.video.iplayer/Subtitles/"
# Infinite loop
while true
 # Use inotify to monitor changes on disk
 inotifywait -e create,modify iplayer.srt
 # Don't proceed until xbmc has finished writing subs to file
 while lsof iplayer.srt
  # wait 100ms until next check
  sleep 0.100
 # fix overlapping subs
 perl ~/subeau.pl -f -u -dd iplayer.srt
 # save original subs
 mv -f iplayer.srt iplayer.original.srt 
 # and use our new fixed ones as default
 mv -f iplayer.1.srt iplayer.srt
 # Cleanup
 rm -f iplayer.1.iso8859.srt iplayer.1.notag.iso8859.srt iplayer.1.notag.srt

# Make script executable
chmod a+x /home/xbmc/iplayersubfix.sh

# Launch script on background when system is restarted
echo 'su -l -c "/bin/bash /home/xbmc/iplayersubfix.sh" xbmc &' >>/etc/rc.local