#!/bin/bash

# drop sudo privileges
if [[ "$USER" == "root" ]]; then
  if [[ -n "$SUDO_USER" ]] ; then
    user="$SUDO_USER"
  else
    FIRSTUSER="$(getent passwd 1000 | cut -d: -f1)"
    user="${FIRSTUSER:-pi}"
  fi
  sudo -u "${user}" "$0" "$@"
fi

local_file="$(dotpi readlink_follow "$0")"
local_path="$(dirname -- "$local_file")"

sudo dotpi service_install --user "${local_path}/systemd/dotpi_pipewire.service"

source_path="${local_path}/bin"
destination_path="${DOTPI_ROOT}/bin"
sudo mkdir -p -- "$destination_path"
for executable in "${source_path}/"* ; do
    sudo ln -s -f -- "$executable" "${destination_path}/$(basename -- "$executable")"
done
