#!/bin/sh

# Check args
if [ ${#} -ne 1 ]; then
    echo "Usage: ${0} new_volume_percentage"
    exit 1
fi

# Check value's range
if [ ${1} -gt 100 ]; then
    echo "Usage: Wrong volume percentage (${1}), must be between 0 and 100"
    exit 1
fi

# Scale new volume value between 0 and 63
volume_percent=${1}
volume_scaled=$(echo "a = ${1} * 63 / 100 + 0.5; scale = 0; a / 1" | bc -l)

# Get current value
current_volume=$(volume_get)

# Turn on/off ampli if necessary
if [ ${current_volume} -eq 0 -a ${volume_scaled} -ne 0 ]; then
    start_audio_amp 1
elif [ ${current_volume} -ne 0 -a ${volume_scaled} -eq 0 ]; then
    start_audio_amp 0
fi

# Set new volume
amixer -q sset 'Headphone' ${volume_scaled} unmute
exit 0
