#!/bin/bash
#pavol-speaker
#Purpose: change color of pulseaudio tray icon
#Support: standard color names or hex values
#ex: pavol_speaker lime (speaker) black (sound)

CONFDIR="/usr/share/pavol"
[ ! -d $CONFDIR ] && mkdir -p $CONFDIR

IMGDIR="$CONFDIR/images"
[ ! -d $IMGDIR ] && mkdir -p $IMGDIR

# define icon size
SPEAKER_PETITE=$(cat /usr/local/jwmdesk/preferences/speaker_petite 2>/dev/null)
[ ! "$SPEAKER_PETITE" ] && SPEAKER_PETITE="false" && echo "$SPEAKER_PETITE" > /usr/local/jwmdesk/preferences/speaker_petite
if [ "$SPEAKER_PETITE" = "true" ]; then 
    SPEAKER_SIZE="10 60, 10 40, 20 40, 40 25, 40 75, 20 60, 10 60"
else
    SPEAKER_SIZE="38,15 40,15 40,85 38,85 15,60 4,60 4,40 15,40"
fi

# define icon color
SPEAKER_FILL_COLOR="$1"
[ ! "$SPEAKER_FILL_COLOR" ] && SPEAKER_FILL_COLOR=$(cat $CONFDIR/dot.speakercolor 2>/dev/null)
[ ! "$SPEAKER_FILL_COLOR" ] && SPEAKER_FILL_COLOR="#000000" && echo -n "$SPEAKER_FILL_COLOR" > $CONFDIR/dot.speakercolor
echo -n "$SPEAKER_FILL_COLOR" > $CONFDIR/dot.speakercolor

SPEAKER_MARGIN_COLOR="$SPEAKER_FILL_COLOR"

SOUND_WAVE_COLOR="$2"
[ ! "$SOUND_WAVE_COLOR" ] && SOUND_WAVE_COLOR=$(cat $CONFDIR/dot.speakercolor2 2>/dev/null)
[ ! "$SOUND_WAVE_COLOR" ] && SOUND_WAVE_COLOR="#228b22" && echo -n "$SOUND_WAVE_COLOR" > $CONFDIR/dot.speakercolor2
echo -n "$SOUND_WAVE_COLOR" > $CONFDIR/dot.speakercolor2

# make speaker icons
echo -n "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<svg height=\"100\" width=\"100\" version=\"1.0\">
    <polygon points=\"$SPEAKER_SIZE\" style=\"fill:$SPEAKER_FILL_COLOR;fill-opacity: 0.7; stroke:$SPEAKER_MARGIN_COLOR;stroke-width: 4.0;\"/>
</svg>" > $IMGDIR/audio-volume-none.svg

echo -n "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<svg height=\"100\" width=\"100\" version=\"1.0\">
    <polygon points=\"$SPEAKER_SIZE\" style=\"fill:$SPEAKER_FILL_COLOR;fill-opacity: 0.7; stroke:$SPEAKER_MARGIN_COLOR;stroke-width: 4.0;\"/>
    <line x1=\"5\" y1=\"75\" x2=\"60\" y2=\"20\" style=\"fill:none;stroke:red;stroke-width:5\"/>
</svg>" > $IMGDIR/audio-volume-muted.svg

echo -n "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<svg height=\"100\" width=\"100\" version=\"1.0\">
    <polygon points=\"$SPEAKER_SIZE\" style=\"fill:$SPEAKER_FILL_COLOR;fill-opacity: 0.7; stroke:$SPEAKER_MARGIN_COLOR;stroke-width: 4.0;\"/>
    <path d=\"m 55,27 c 24,23 0,48 0,48\" style=\"fill:none;stroke:$SOUND_WAVE_COLOR;stroke-width:7\"/>
</svg>" > $IMGDIR/audio-volume-low.svg

echo -n "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<svg height=\"100\" width=\"100\" version=\"1.0\">
    <polygon points=\"$SPEAKER_SIZE\" style=\"fill:$SPEAKER_FILL_COLOR;fill-opacity: 0.7; stroke:$SPEAKER_MARGIN_COLOR;stroke-width: 4.0;\"/>
    <path d=\"m 55,27 c 24,23 0,48 0,48\" style=\"fill:none;stroke:$SOUND_WAVE_COLOR;stroke-width:7\"/>
    <path d=\"M 63,15 C 100,53 62,88 62,88\" style=\"fill:none;stroke:$SOUND_WAVE_COLOR;stroke-width:7\"/>
</svg>" > $IMGDIR/audio-volume-medium.svg

echo -n "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<svg height=\"100\" width=\"100\" version=\"1.0\">
    <polygon points=\"$SPEAKER_SIZE\" style=\"fill:$SPEAKER_FILL_COLOR;fill-opacity: 0.7; stroke:$SPEAKER_MARGIN_COLOR;stroke-width: 4.0;\"/>
    <path d=\"m 55,27 c 24,23 0,48 0,48\" style=\"fill:none;stroke:$SOUND_WAVE_COLOR;stroke-width:7\"/>
    <path d=\"M 63,15 C 100,53 62,88 62,88\" style=\"fill:none;stroke:$SOUND_WAVE_COLOR;stroke-width:7\"/>
    <path d=\"M 72,5 C 120,54 73,97 73,97\" style=\"fill:none;stroke:$SOUND_WAVE_COLOR;stroke-width:7\"/>
</svg>" > $IMGDIR/audio-volume-high.svg

if [ -d /usr/share/icons/"Puppy Standard" ]; then
    for i in 16 22 24 32 48 64; do
        rm -f /usr/share/icons/"Puppy Standard"/${i}/status/audio-volume-*.svg
        cat $IMGDIR/audio-volume-none.svg | sed "s/^.*<svg .*$/<svg height=\"${i}\" width=\"${i}\" viewBox=\"0 0 100 100\" version=\"1.0\">/" > /usr/share/icons/"Puppy Standard"/${i}/status/audio-volume-none.svg
        cat $IMGDIR/audio-volume-muted.svg | sed "s/^.*<svg .*$/<svg height=\"${i}\" width=\"${i}\" viewBox=\"0 0 100 100\" version=\"1.0\">/" > /usr/share/icons/"Puppy Standard"/${i}/status/audio-volume-muted.svg
        cat $IMGDIR/audio-volume-low.svg | sed "s/^.*<svg .*$/<svg height=\"${i}\" width=\"${i}\" viewBox=\"0 0 100 100\" version=\"1.0\">/" > /usr/share/icons/"Puppy Standard"/${i}/status/audio-volume-low.svg
        cat $IMGDIR/audio-volume-medium.svg | sed "s/^.*<svg .*$/<svg height=\"${i}\" width=\"${i}\" viewBox=\"0 0 100 100\" version=\"1.0\">/" > /usr/share/icons/"Puppy Standard"/${i}/status/audio-volume-medium.svg
        cat $IMGDIR/audio-volume-high.svg | sed "s/^.*<svg .*$/<svg height=\"${i}\" width=\"${i}\" viewBox=\"0 0 100 100\" version=\"1.0\">/" > /usr/share/icons/"Puppy Standard"/${i}/status/audio-volume-high.svg
    done
fi
if [ -d /usr/share/icons/"PMaterial" ]; then
    for i in 16 22 24 32 48 64; do
        rm -f /usr/share/icons/"PMaterial"/${i}/status/audio-volume-*.svg
        cat $IMGDIR/audio-volume-none.svg | sed "s/^.*<svg .*$/<svg height=\"${i}\" width=\"${i}\" viewBox=\"0 0 100 100\" version=\"1.0\">/" > /usr/share/icons/"PMaterial"/${i}/status/audio-volume-none.svg
        cat $IMGDIR/audio-volume-muted.svg | sed "s/^.*<svg .*$/<svg height=\"${i}\" width=\"${i}\" viewBox=\"0 0 100 100\" version=\"1.0\">/" > /usr/share/icons/"PMaterial"/${i}/status/audio-volume-muted.svg
        cat $IMGDIR/audio-volume-low.svg | sed "s/^.*<svg .*$/<svg height=\"${i}\" width=\"${i}\" viewBox=\"0 0 100 100\" version=\"1.0\">/" > /usr/share/icons/"PMaterial"/${i}/status/audio-volume-low.svg
        cat $IMGDIR/audio-volume-medium.svg | sed "s/^.*<svg .*$/<svg height=\"${i}\" width=\"${i}\" viewBox=\"0 0 100 100\" version=\"1.0\">/" > /usr/share/icons/"PMaterial"/${i}/status/audio-volume-medium.svg
        cat $IMGDIR/audio-volume-high.svg | sed "s/^.*<svg .*$/<svg height=\"${i}\" width=\"${i}\" viewBox=\"0 0 100 100\" version=\"1.0\">/" > /usr/share/icons/"PMaterial"/${i}/status/audio-volume-high.svg
    done
fi
if [ -d /usr/share/icons/"Puppy Flat" ]; then
    for i in 16 22 24 32 48 64; do
        rm -f /usr/share/icons/"Puppy Flat"/${i}/status/audio-volume-*.svg
        cat $IMGDIR/audio-volume-none.svg | sed "s/^.*<svg .*$/<svg height=\"${i}\" width=\"${i}\" viewBox=\"0 0 100 100\" version=\"1.0\">/" > /usr/share/icons/"Puppy Flat"/${i}/status/audio-volume-none.svg
        cat $IMGDIR/audio-volume-muted.svg | sed "s/^.*<svg .*$/<svg height=\"${i}\" width=\"${i}\" viewBox=\"0 0 100 100\" version=\"1.0\">/" > /usr/share/icons/"Puppy Flat"/${i}/status/audio-volume-muted.svg
        cat $IMGDIR/audio-volume-low.svg | sed "s/^.*<svg .*$/<svg height=\"${i}\" width=\"${i}\" viewBox=\"0 0 100 100\" version=\"1.0\">/" > /usr/share/icons/"Puppy Flat"/${i}/status/audio-volume-low.svg
        cat $IMGDIR/audio-volume-medium.svg | sed "s/^.*<svg .*$/<svg height=\"${i}\" width=\"${i}\" viewBox=\"0 0 100 100\" version=\"1.0\">/" > /usr/share/icons/"Puppy Flat"/${i}/status/audio-volume-medium.svg
        cat $IMGDIR/audio-volume-high.svg | sed "s/^.*<svg .*$/<svg height=\"${i}\" width=\"${i}\" viewBox=\"0 0 100 100\" version=\"1.0\">/" > /usr/share/icons/"Puppy Flat"/${i}/status/audio-volume-high.svg
    done
fi

exit 0
