#!/bin/bash

# SPDX-License-Identifier: GPL-2.0-or-later
# Copyright (C) 2020-present Shanti Gilbert (https://github.com/shantigilbert)
# Copyright (C) 2023 JELOS (https://github.com/JustEnoughLinuxOS)

# Source predefined functions and variables
. /etc/profile

HP_ON='*headset status is in*'
HP_OFF='*headset status is out*'

# Switch to headphones if we have them already connected at boot
BOOT_SETTING=$(journalctl | grep "headset status is" | tail -n 1)
case ${BOOT_SETTING} in
	(${HP_ON})
		amixer -c 0 cset name='Playback Path' ${DEVICE_PLAYBACK_PATH_HP}
	;;
        *)
                amixer -c 0 cset name='Playback Path' ${DEVICE_PLAYBACK_PATH_SPK}
        ;;
esac

journalctl -f | while read line; do
    case $line in
	(${HP_ON})
		amixer -c 0 cset name='Playback Path' ${DEVICE_PLAYBACK_PATH_HP}
	;;
        (${HP_OFF})
                amixer -c 0 cset name='Playback Path' ${DEVICE_PLAYBACK_PATH_SPK}
        ;;
    esac
done
