#!/bin/bash
# -*- ENCODING: UTF-8 -*-

# picasm
#
# Raise the sound level to the maximum (or to a specified value).
#
# Copyright (c) 2013-2026: Alexis Puente Montiel   < pica (a) picalibre.org >
#
# Licensed according to GNU AGPL version 3.0.
#
# It is libre/free software; you can use, redistribute and/or modify it according to the terms of GNU AGPL as published by GNU, version 3.0, 19 November 2007.
#
# It is distributed in the hope that it will be useful, but without any warranty. Read GNU AGPL version 3.0 for additional details.
#
# A copy of GNU AGPL version 3.0 is available at /usr/share/doc/<software-package-name>/agpl-3.0.txt (additionally on Internet as text at https://www.gnu.org/licenses/agpl-3.0.txt and as HTML at https://www.gnu.org/licenses/agpl-3.0-standalone.html ).
#
# Note: Additionally to the official e-mails, picalibre.org is strictly the only official site for this software project, please consider using it to download, report bugs and contribute.
#
# Depends: alsa-utils | pulseaudio, bash, coreutils, grep, sed
# Recommends: x11-xkb-utils


### SCRIPT VARIABLES ########################################

CNAME="picasm"
VERSION="1.3.1"
TITLE="'picasm'"
ICON="/usr/share/icons/picasm.png"

# Translations
if [ "$LANG" = "" ] ; then export $(cat /etc/default/locale | grep -a 'LANG=') ; fi
TEXTDOMAIN=am
TEXTDOMAINDIR=/usr/share/locale/

# Write errors to log
ERRORLOG="$HOME/.${CNAME}.log"
if [ -e "$ERRORLOG" ] ; then
	mv -f $ERRORLOG ${ERRORLOG}.ant
fi
if [ -e "$ERRORLOG" ] ; then rm -rf "$ERRORLOG" ; fi

for i in /etc/pica-global.dist /etc/pica-global.orig /etc/pica-global /etc/pica-global.local ~/.pica-global ~/pica-global ; do
	if [ -f "$i" ] ; then
	cat "$i"
	source "$i"
	source <(cat $i | sed -e "s/=\(YES\|Yes\|yes\|y\|SÍ\|SI\|Sí\|Si\|sí\|si\|S\|s\)/=Y/g" -e "s/=\(No\|no\|n\)/=N/g" -e "s/=\"\(YES\|Yes\|yes\|y\|SÍ\|SI\|Sí\|Si\|sí\|si\|S\|s\)\"/=Y/g" -e "s/=\"\(No\|no\|n\)\"/=N/g")
	fi
done
if [ "$DEBUG" = "Y" ] ; then
	set -xv
	DEBUG="Y"
else
	ERRORLOG="/tmp/.${CNAME}_$(id -nu).log"
	if [ -e "$ERRORLOG" ] ; then mv -f $ERRORLOG ${ERRORLOG}.ant ; fi
	if [ -e "$ERRORLOG" ] ; then rm -rf "$ERRORLOG" ; fi
fi
if [ "$DEBUG" = "" ] ; then DEBUG="N" ; fi
if [ "$DEBUG" != "N" ] ; then
exec > >(tee -a "$ERRORLOG") 2>&1
echo "$0" "$*" >> "$ERRORLOG"
#else
#exec 2>>"$ERRORLOG"
fi

# Description:
BDESCRIP=$"Raise the sound level to the maximum (or to a specified value)."
LDESCRIP=$"$TITLE is a tool to raise the sound level to the maximum (or to a specified value)."

# Documentation:
docu_info () {
echo "$CNAME ($VERSION) - $BDESCRIP"
echo 
echo $"Usage:" $CNAME [$"OPTIONS"] [$"VALUE"]
echo 
echo $"Options:"
echo -e "$ODESCRIP"
echo 
echo $"'man $CNAME' for more information."
echo 
}
ODESCRIP=" -x""\t"$"Show help documentation."

while getopts x OPTION ; do
	case $OPTION in
		x )   docu_info ; exit 0 ;;
	esac
done


### SCRIPT ########################################

# Audio commands independent of pulseaudio (such as amixer of alsa-utils and play of sox), may show pulseaudio errors if pulseaudio is working.

PREVAL=$(echo $(echo "$*" | sed 's| |\n|g' | grep -aEv "^-[a-z]") | sed 's| ||g')

if [ "$PREVAL" = "" ] ; then
	
	VAL=100
	
else

	CHECK_PREVAL=$(echo "$PREVAL" | sed "s|[0-9]||g")
	
	if [ "$CHECK_PREVAL" = "" ] ; then
	
	VAL=$PREVAL
	
	else
	
	echo $"ERROR:" "$PREVAL"
	
	exit 1
	
	fi
	
fi


if [ "$(command -v amixer)" != "" ] ; then

amixer set Master    $VAL% on
amixer set Headphone $VAL% on
amixer set PCM       $VAL% on
amixer set Capture   $VAL% on
amixer set Mux       $VAL% on

amixer | grep -aE "^[a-zA-Z]" | grep -aEio \'.* | grep -aEiv Beep | while read i ; do amixer set "$i" $VAL% on ; done

fi

if [ "$(command -v pactl)" != "" ] ; then

pulseaudio --check ; if [ "$?" -ne 0 ] ; then pulseaudio --start ; fi
pactl set-sink-volume @DEFAULT_SINK@ $VAL%
pactl set-sink-mute @DEFAULT_SINK@ 0

fi

if [ "$(command -v pulsemixer)" != "" ] ; then

pulseaudio --check ; if [ "$?" -ne 0 ] ; then pulseaudio --start ; fi
pulsemixer --unmute --set-volume $VAL

fi

exit "$?"
