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
}

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

if [ -x /usr/bin/update-mime-database ]; then
  /usr/bin/update-mime-database usr/share/mime >/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 -f usr/share/icons/hicolor >/dev/null 2>&1
  fi
fi

# Prepare the new configuration files
config etc/vbox/vbox.cfg.new
config etc/default/virtualbox.new
preserve_perms etc/rc.d/rc.vboxdrv.new
preserve_perms etc/rc.d/rc.vboxballoonctrl-service.new
preserve_perms etc/rc.d/rc.vboxautostart-service.new

( cd usr/lib64/virtualbox/components ; rm -rf VBoxXPCOM.so )
( cd usr/lib64/virtualbox/components ; ln -sf ../VBoxXPCOM.so VBoxXPCOM.so )
( cd usr/lib64/virtualbox/components ; rm -rf VBoxDDU.so )
( cd usr/lib64/virtualbox/components ; ln -sf ../VBoxDDU.so VBoxDDU.so )
( cd usr/lib64/virtualbox/components ; rm -rf VBoxRT.so )
( cd usr/lib64/virtualbox/components ; ln -sf ../VBoxRT.so VBoxRT.so )
( cd usr/lib64/virtualbox/components ; rm -rf VBoxREM.so )
( cd usr/lib64/virtualbox/components ; ln -sf ../VBoxREM.so VBoxREM.so )
( cd usr/lib64/virtualbox/components ; rm -rf VBoxVMM.so )
( cd usr/lib64/virtualbox/components ; ln -sf ../VBoxVMM.so VBoxVMM.so )
( cd usr/bin ; rm -rf vboxautostart )
( cd usr/bin ; ln -sf /usr/lib64/virtualbox/VBox vboxautostart )
( cd usr/bin ; rm -rf vboxheadless )
( cd usr/bin ; ln -sf /usr/lib64/virtualbox/VBox vboxheadless )
( cd usr/bin ; rm -rf VBoxAutostart )
( cd usr/bin ; ln -sf /usr/lib64/virtualbox/VBox VBoxAutostart )
( cd usr/bin ; rm -rf VBoxVRDP )
( cd usr/bin ; ln -sf /usr/lib64/virtualbox/VBox VBoxVRDP )
( cd usr/bin ; rm -rf VBoxManage )
( cd usr/bin ; ln -sf /usr/lib64/virtualbox/VBox VBoxManage )
( cd usr/bin ; rm -rf vboxmanage )
( cd usr/bin ; ln -sf /usr/lib64/virtualbox/VBox vboxmanage )
( cd usr/bin ; rm -rf VBoxBalloonCtrl )
( cd usr/bin ; ln -sf /usr/lib64/virtualbox/VBox VBoxBalloonCtrl )
( cd usr/bin ; rm -rf vboxsdl )
( cd usr/bin ; ln -sf /usr/lib64/virtualbox/VBox vboxsdl )
( cd usr/bin ; rm -rf VBoxHeadless )
( cd usr/bin ; ln -sf /usr/lib64/virtualbox/VBox VBoxHeadless )
( cd usr/bin ; rm -rf VirtualBox )
( cd usr/bin ; ln -sf /usr/lib64/virtualbox/VBox VirtualBox )
( cd usr/bin ; rm -rf vboxballoonctrl )
( cd usr/bin ; ln -sf /usr/lib64/virtualbox/VBox vboxballoonctrl )
( cd usr/bin ; rm -rf virtualbox )
( cd usr/bin ; ln -sf /usr/lib64/virtualbox/VBox virtualbox )
( cd usr/bin ; rm -rf VBoxSDL )
( cd usr/bin ; ln -sf /usr/lib64/virtualbox/VBox VBoxSDL )
