
# Handle the incoming configuration files:
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...
}


# Prepare PolicyKit system files.
config etc/PolicyKit/PolicyKit.conf.new
config etc/dbus-1/system.d/org.freedesktop.PolicyKit.conf.new


# Create dbus' user and group if they do not exist.
group_exists=$(grep ^polkituser etc/group)
if [ x"${group_exists}" == x"" ]; then
  if ! ( groupadd -g 87 polkituser ); then
    echo "Warning: Could not add group 'polkituser'!"
  fi
fi
user_exists=$(grep ^polkituser etc/passwd)
if [ x"${user_exists}" == x"" ]; then
  if ! ( useradd -c "PolicyKit System User" -s /bin/false -d /var/run/PolicyKit -u 87 -g polkituser polkituser ); then
    echo "Warning: Could not add user 'polkituser'!"
  fi
fi


# Set correct ownerships. Taken from the PolicyKit README.
# Use chmod after setting ownership or SUID/SGID will get lost.
chown root:polkituser       usr/libexec/polkit-read-auth-helper
chmod 2755                  usr/libexec/polkit-read-auth-helper
chown root:polkituser       usr/libexec/polkit-grant-helper
chmod 2755                  usr/libexec/polkit-grant-helper
chown root:polkituser       usr/libexec/polkit-grant-helper-shadow
chmod 4754                  usr/libexec/polkit-grant-helper-shadow
chown root:polkituser       usr/libexec/polkit-revoke-helper
chmod 2755                  usr/libexec/polkit-revoke-helper
chown root:polkituser       usr/libexec/polkit-explicit-grant-helper
chmod 2755                  usr/libexec/polkit-explicit-grant-helper
chown polkituser:root       usr/libexec/polkit-set-default-helper
chmod 4755                  usr/libexec/polkit-set-default-helper
chown root:root             usr/libexec/polkit-resolve-exe-helper
chmod 4755                  usr/libexec/polkit-resolve-exe-helper
chown root:polkituser       var/run/PolicyKit
chmod 0770                  var/run/PolicyKit
chown root:polkituser       var/lib/PolicyKit
chmod 0770                  var/lib/PolicyKit
chown polkituser:root       var/lib/PolicyKit-public
chmod 0755                  var/lib/PolicyKit-public
chown polkituser:polkituser var/lib/misc/PolicyKit.reload
chmod 0664                  var/lib/misc/PolicyKit.reload

( cd usr/lib ; rm -rf libpolkit-dbus.so )
( cd usr/lib ; ln -sf libpolkit-dbus.so.2.0.0 libpolkit-dbus.so )
( cd usr/lib ; rm -rf libpolkit.so.2 )
( cd usr/lib ; ln -sf libpolkit.so.2.0.0 libpolkit.so.2 )
( cd usr/lib ; rm -rf libpolkit-grant.so.2 )
( cd usr/lib ; ln -sf libpolkit-grant.so.2.0.0 libpolkit-grant.so.2 )
( cd usr/lib ; rm -rf libpolkit-grant.so )
( cd usr/lib ; ln -sf libpolkit-grant.so.2.0.0 libpolkit-grant.so )
( cd usr/lib ; rm -rf libpolkit.so )
( cd usr/lib ; ln -sf libpolkit.so.2.0.0 libpolkit.so )
( cd usr/lib ; rm -rf libpolkit-dbus.so.2 )
( cd usr/lib ; ln -sf libpolkit-dbus.so.2.0.0 libpolkit-dbus.so.2 )
