config() {
  NEW="$1"
  OLD="$(dirname $NEW)/$(basename $NEW .new)"
  # If there's no config file by that name, mv it over:
  if [ ! -r $OLD ]; then
    mv $NEW $OLD
  elif [ "$(cat $OLD | md5sum)" = "$(cat $NEW | md5sum)" ]; then
    # toss the redundant copy
    rm $NEW
  fi
  # Otherwise, we leave the .new copy for the admin to consider...
}

preserve_perms() {
  NEW="$1"
  OLD="$(dirname $NEW)/$(basename $NEW .new)"
  if [ -e $OLD ]; then
    cp -a $OLD ${NEW}.incoming
    cat $NEW > ${NEW}.incoming
    mv ${NEW}.incoming $NEW
  fi
  config $NEW
}

preserve_perms etc/rc.d/rc.teamviewerd.new

if [ -x /usr/bin/update-desktop-database ]; then
  /usr/bin/update-desktop-database -q usr/share/applications >/dev/null 2>&1
fi
( cd opt/teamviewer/tv_bin/wine/lib ; rm -rf libwine.so.1 )
( cd opt/teamviewer/tv_bin/wine/lib ; ln -sf libwine.so.1.0 libwine.so.1 )
( cd opt/teamviewer/tv_bin/wine/lib ; rm -rf libwine.so )
( cd opt/teamviewer/tv_bin/wine/lib ; ln -sf libwine.so.1.0 libwine.so )
( cd opt/teamviewer/tv_bin ; rm -rf TeamViewer )
( cd opt/teamviewer/tv_bin ; ln -sf script/teamviewer TeamViewer )
( cd opt/teamviewer ; rm -rf config )
( cd opt/teamviewer ; ln -sf /etc/teamviewer config )
( cd opt/teamviewer ; rm -rf logfiles )
( cd opt/teamviewer ; ln -sf /var/log/teamviewer12 logfiles )
( cd usr/bin ; rm -rf teamviewer )
( cd usr/bin ; ln -sf /opt/teamviewer/tv_bin/script/teamviewer teamviewer )
( cd usr/share/icons/hicolor/48x48/apps ; rm -rf teamviewer.png )
( cd usr/share/icons/hicolor/48x48/apps ; ln -sf /opt/teamviewer/tv_bin/desktop/teamviewer.png teamviewer.png )
( cd usr/share/icons/hicolor/48x48/apps ; rm -rf TeamViewer.png )
( cd usr/share/icons/hicolor/48x48/apps ; ln -sf /opt/teamviewer/tv_bin/desktop/teamviewer.png TeamViewer.png )
( cd usr/share/pixmaps ; rm -rf teamviewer.png )
( cd usr/share/pixmaps ; ln -sf /opt/teamviewer/tv_bin/desktop/teamviewer.png teamviewer.png )
( cd usr/share/applications ; rm -rf com.teamviewer.TeamViewer.desktop )
( cd usr/share/applications ; ln -sf /opt/teamviewer/tv_bin/desktop/com.teamviewer.TeamViewer.desktop com.teamviewer.TeamViewer.desktop )
