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...
}

perms() {
  # Keep same perms on file
  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
}

if [ -x /usr/bin/update-desktop-database ]; then
  /usr/bin/update-desktop-database -q usr/share/applications > /dev/null 2>&1
fi

if [ -e usr/share/icons/hicolor/icon-theme.cache ]; then
  if [ -x /usr/bin/gtk-update-icon-cache ]; then
  	/usr/bin/gtk-update-icon-cache -t -f -q usr/share/icons/hicolor >/dev/null 2>&1
  fi
fi

perms etc/rc.d/rc.teamviewerd.new
( 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/teamviewer15 logfiles )
( cd usr/bin ; rm -rf teamviewer )
( cd usr/bin ; ln -sf /opt/teamviewer/tv_bin/script/teamviewer teamviewer )
( 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 )
( cd usr/share/applications ; rm -rf teamviewer8.desktop )
( cd usr/share/applications ; ln -sf /opt/teamviewer/tv_bin/desktop/teamviewer8.desktop teamviewer8.desktop )
( cd usr/share/applications ; rm -rf teamviewerapi.desktop )
( cd usr/share/applications ; ln -sf /opt/teamviewer/tv_bin/desktop/teamviewerapi.desktop teamviewerapi.desktop )
( cd usr/share/applications ; rm -rf tvoneweblogin.desktop )
( cd usr/share/applications ; ln -sf /opt/teamviewer/tv_bin/desktop/tvoneweblogin.desktop tvoneweblogin.desktop )
( cd usr/share/dbus-1/services ; rm -rf com.teamviewer.TeamViewer.Desktop.service )
( cd usr/share/dbus-1/services ; ln -sf /opt/teamviewer/tv_bin/script/com.teamviewer.TeamViewer.Desktop.service com.teamviewer.TeamViewer.Desktop.service )
( cd usr/share/dbus-1/services ; rm -rf com.teamviewer.TeamViewer.service )
( cd usr/share/dbus-1/services ; ln -sf /opt/teamviewer/tv_bin/script/com.teamviewer.TeamViewer.service com.teamviewer.TeamViewer.service )
( cd usr/share/icons/hicolor/16x16/apps ; rm -rf TeamViewer.png )
( cd usr/share/icons/hicolor/16x16/apps ; ln -sf /opt/teamviewer/tv_bin/desktop/teamviewer_16.png TeamViewer.png )
( cd usr/share/icons/hicolor/20x20/apps ; rm -rf TeamViewer.png )
( cd usr/share/icons/hicolor/20x20/apps ; ln -sf /opt/teamviewer/tv_bin/desktop/teamviewer_20.png TeamViewer.png )
( cd usr/share/icons/hicolor/24x24/apps ; rm -rf TeamViewer.png )
( cd usr/share/icons/hicolor/24x24/apps ; ln -sf /opt/teamviewer/tv_bin/desktop/teamviewer_24.png TeamViewer.png )
( cd usr/share/icons/hicolor/256x256/apps ; rm -rf TeamViewer.png )
( cd usr/share/icons/hicolor/256x256/apps ; ln -sf /opt/teamviewer/tv_bin/desktop/teamviewer_256.png TeamViewer.png )
( cd usr/share/icons/hicolor/32x32/apps ; rm -rf TeamViewer.png )
( cd usr/share/icons/hicolor/32x32/apps ; ln -sf /opt/teamviewer/tv_bin/desktop/teamviewer_32.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_48.png TeamViewer.png )
( cd usr/share/polkit-1/actions ; rm -rf com.teamviewer.TeamViewer.policy )
( cd usr/share/polkit-1/actions ; ln -sf /opt/teamviewer/tv_bin/script/com.teamviewer.TeamViewer.policy com.teamviewer.TeamViewer.policy )
