#!/bin/sh

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
}

# Install profile scripts for console use:
preserve_perms etc/profile.d/pipewire.csh.new
preserve_perms etc/profile.d/pipewire.sh.new

# Toss redundant sample files:
for file in pipewire.desktop pipewire-pulse.desktop ; do
  cmp -s etc/xdg/autostart/${file} etc/xdg/autostart/${file}.sample && rm etc/xdg/autostart/${file}.sample
done

# Disable pulseaudio.desktop:
if [ -r etc/xdg/autostart/pulseaudio.desktop ]; then
  if ! grep -q "^Hidden=true$" etc/xdg/autostart/pulseaudio.desktop ; then
    echo "Hidden=true" >> etc/xdg/autostart/pulseaudio.desktop
  fi
fi

# Pipewire is now the default multimedia server, so if we see pulseaudio
# running let's kill it so that it doesn't interfere:
killall --ns $$ /usr/bin/pulseaudio 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 pipewire-aes67 )
( cd usr/bin ; ln -sf pipewire pipewire-aes67 )
( cd usr/bin ; rm -rf pipewire-avb )
( cd usr/bin ; ln -sf pipewire pipewire-avb )
( cd usr/bin ; rm -rf pipewire-pulse )
( cd usr/bin ; ln -sf pipewire pipewire-pulse )
( cd usr/bin ; rm -rf pipewire-vulkan )
( cd usr/bin ; ln -sf pipewire pipewire-vulkan )
( cd usr/bin ; rm -rf pw-dsdplay )
( cd usr/bin ; ln -sf pw-cat pw-dsdplay )
( cd usr/bin ; rm -rf pw-encplay )
( cd usr/bin ; ln -sf pw-cat pw-encplay )
( cd usr/bin ; rm -rf pw-midi2play )
( cd usr/bin ; ln -sf pw-cat pw-midi2play )
( cd usr/bin ; rm -rf pw-midi2record )
( cd usr/bin ; ln -sf pw-cat pw-midi2record )
( cd usr/bin ; rm -rf pw-midiplay )
( cd usr/bin ; ln -sf pw-cat pw-midiplay )
( cd usr/bin ; rm -rf pw-midirecord )
( cd usr/bin ; ln -sf pw-cat pw-midirecord )
( cd usr/bin ; rm -rf pw-play )
( cd usr/bin ; ln -sf pw-cat pw-play )
( cd usr/bin ; rm -rf pw-record )
( cd usr/bin ; ln -sf pw-cat pw-record )
( cd usr/bin ; rm -rf pw-sysex )
( cd usr/bin ; ln -sf pw-cat pw-sysex )
( cd usr/lib64 ; rm -rf libpipewire-0.3.so )
( cd usr/lib64 ; ln -sf libpipewire-0.3.so.0 libpipewire-0.3.so )
( cd usr/lib64 ; rm -rf libpipewire-0.3.so.0 )
( cd usr/lib64 ; ln -sf libpipewire-0.3.so.0.1607.0 libpipewire-0.3.so.0 )
( cd usr/lib64/pipewire-0.3/jack ; rm -rf libjack.so )
( cd usr/lib64/pipewire-0.3/jack ; ln -sf libjack.so.0 libjack.so )
( cd usr/lib64/pipewire-0.3/jack ; rm -rf libjack.so.0 )
( cd usr/lib64/pipewire-0.3/jack ; ln -sf libjack.so.0.3.1607 libjack.so.0 )
( cd usr/lib64/pipewire-0.3/jack ; rm -rf libjacknet.so )
( cd usr/lib64/pipewire-0.3/jack ; ln -sf libjacknet.so.0 libjacknet.so )
( cd usr/lib64/pipewire-0.3/jack ; rm -rf libjacknet.so.0 )
( cd usr/lib64/pipewire-0.3/jack ; ln -sf libjacknet.so.0.3.1607 libjacknet.so.0 )
( cd usr/lib64/pipewire-0.3/jack ; rm -rf libjackserver.so )
( cd usr/lib64/pipewire-0.3/jack ; ln -sf libjackserver.so.0 libjackserver.so )
( cd usr/lib64/pipewire-0.3/jack ; rm -rf libjackserver.so.0 )
( cd usr/lib64/pipewire-0.3/jack ; ln -sf libjackserver.so.0.3.1607 libjackserver.so.0 )
