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

config etc/pulse/client.conf.new
config etc/pulse/daemon.conf.new
config etc/pulse/default.pa.new
config etc/pulse/system.pa.new
config etc/xdg/autostart/pulseaudio.desktop.new
preserve_perms etc/rc.d/rc.pulseaudio.new

# Make sure the pulse user is in the audio group:
chroot . /usr/sbin/usermod -a -G audio pulse 1> /dev/null 2> /dev/null

# Make sure the root user is in the audio group:
chroot . /usr/sbin/usermod -a -G audio root 1> /dev/null 2> /dev/null

# Recompile glib schemas:
if [ -e usr/share/glib-2.0/schemas ]; then
  if [ -x /usr/bin/glib-compile-schemas ]; then
    /usr/bin/glib-compile-schemas usr/share/glib-2.0/schemas >/dev/null 2>&1
  fi
fi

( cd usr/bin ; rm -rf pamon )
( cd usr/bin ; ln -sf pacat pamon )
( cd usr/bin ; rm -rf paplay )
( cd usr/bin ; ln -sf pacat paplay )
( cd usr/bin ; rm -rf parec )
( cd usr/bin ; ln -sf pacat parec )
( cd usr/bin ; rm -rf parecord )
( cd usr/bin ; ln -sf pacat parecord )
( cd usr/lib64 ; rm -rf libpulse-mainloop-glib.so )
( cd usr/lib64 ; ln -sf libpulse-mainloop-glib.so.0 libpulse-mainloop-glib.so )
( cd usr/lib64 ; rm -rf libpulse-mainloop-glib.so.0 )
( cd usr/lib64 ; ln -sf libpulse-mainloop-glib.so.0.0.6 libpulse-mainloop-glib.so.0 )
( cd usr/lib64 ; rm -rf libpulse-simple.so )
( cd usr/lib64 ; ln -sf libpulse-simple.so.0 libpulse-simple.so )
( cd usr/lib64 ; rm -rf libpulse-simple.so.0 )
( cd usr/lib64 ; ln -sf libpulse-simple.so.0.1.1 libpulse-simple.so.0 )
( cd usr/lib64 ; rm -rf libpulse.so )
( cd usr/lib64 ; ln -sf libpulse.so.0 libpulse.so )
( cd usr/lib64 ; rm -rf libpulse.so.0 )
( cd usr/lib64 ; ln -sf libpulse.so.0.24.2 libpulse.so.0 )
( cd usr/man/man1 ; rm -rf pamon.1.gz )
( cd usr/man/man1 ; ln -sf pacat.1.gz pamon.1.gz )
( cd usr/man/man1 ; rm -rf paplay.1.gz )
( cd usr/man/man1 ; ln -sf pacat.1.gz paplay.1.gz )
( cd usr/man/man1 ; rm -rf parec.1.gz )
( cd usr/man/man1 ; ln -sf pacat.1.gz parec.1.gz )
( cd usr/man/man1 ; rm -rf parecord.1.gz )
( cd usr/man/man1 ; ln -sf pacat.1.gz parecord.1.gz )
( cd usr/share/bash-completion/completions ; rm -rf pacat )
( cd usr/share/bash-completion/completions ; ln -sf pactl pacat )
( cd usr/share/bash-completion/completions ; rm -rf pacmd )
( cd usr/share/bash-completion/completions ; ln -sf pactl pacmd )
( cd usr/share/bash-completion/completions ; rm -rf padsp )
( cd usr/share/bash-completion/completions ; ln -sf pactl padsp )
( cd usr/share/bash-completion/completions ; rm -rf paplay )
( cd usr/share/bash-completion/completions ; ln -sf pactl paplay )
( cd usr/share/bash-completion/completions ; rm -rf parec )
( cd usr/share/bash-completion/completions ; ln -sf pactl parec )
( cd usr/share/bash-completion/completions ; rm -rf parecord )
( cd usr/share/bash-completion/completions ; ln -sf pactl parecord )
( cd usr/share/bash-completion/completions ; rm -rf pasuspender )
( cd usr/share/bash-completion/completions ; ln -sf pactl pasuspender )
( cd usr/share/bash-completion/completions ; rm -rf pulseaudio )
( cd usr/share/bash-completion/completions ; ln -sf pactl pulseaudio )
