#!/bin/bash
cReset="[0m"
cBold="[1m"
colorBlack=0
colorRed=1
colorGreen=2
colorYellow=3
colorBlue=4
colorMagenta=5
colorCyan=6
colorWhite=7
colorDefault=9
cGreen="[0;3${colorGreen}m"${cBold}
cRed="[1;3${colorRed}m"${cBold}
cYellow="[1;3${colorYellow}m"${cBold}
cBlue="[1;3${colorBlue}m"${cBold}
cMagenta="[1;3${colorMagenta}m"${cBold}

function get_mplayer(){
    local EXISTING
    local FOUND
    local TO_USE
    echo -e "  >>> ${cMagenta}MPlayer${cReset}"
    echo -en "    Reading in file ${cMagenta}MPlayer${cReset} version    ... "
    EXISTING=$(sed -n '/svn-[0-9]/p' pyradio/win.py | sed -e 's/.*svn-//' -e 's/-.*//')

    echo -e "${cYellow}r${EXISTING}${cReset}"

    echo -en "    Reading published ${cMagenta}MPlayer${cReset} versions ... "

    FOUND=$(curl -s -L 'https://sourceforge.net/projects/mplayerwin/files/MPlayer-MEncoder' 2>/dev/null | \
        grep '<tr title' | \
        sed -e 's/.*<tr title="//' \
            -e 's/".*//' | \
        sed -n 1p
        )

    if [ $? -ne 0 ]
    then
        echo -e '\n  Error accessing SourceForge...'
        exit 1
    fi

    count=0
    X=$FOUND
    for n in $FOUND
    do
        if ((count == 0))
        then
            if [ r"${EXISTING}" = "$n" ]
            then
                echo -e "$FOUND" | sed -e '2,$d' -e "s/.*/${cYellow}&${cReset}/"
                echo -e "    Latest ${cMagenta}MPlayer${cReset} version already used..."
                return
            else
                echo -e "$FOUND" | sed -e '2,$d' -e "s/.*/${cRes}&${cReset}/"
                TO_USE="${n/r/}"
                break
            fi
            ((count++))
        fi
    done

    if [ ! -z "${TO_USE}" ]
    then
        echo "Version to use: ${TO_USE}"
    fi

    echo -en "    Updating in file ${cMagenta}MPlayer${cReset} version ... "
    sed -i "/MPlayer-MEncoder/s/$EXISTING/$TO_USE/g" pyradio/win.py
    echo -e "${cGreen}done"${cReset}
}

function get_mpv(){
    local EXISTING
    local FOUND
    local TO_USE
    echo -e "  >>> ${cMagenta}MPV${cReset}"
    echo -en "    Reading in file ${cMagenta}MPV${cReset} version ... "
    EXISTING=$(grep mpv-player-windows pyradio/win.py | grep download | grep -v latest | sed -e "s/.*'h/h/" -e "s/',//")
    EXISTING=$(basename $(dirname "$EXISTING"))
    echo -e "${cYellow}$EXISTING${cReset}"

    echo -en "    Reading latest ${cMagenta}MPV${cReset} version  ... "
    FOUND=$(curl -s -L https://sourceforge.net/projects/mpv-player-windows/files/64bit/ 2>/dev/null | grep '"url":"/projects' | sed 's/"url":/\n&/g'  |grep '^"url"' | sed '2,$d' | sed -e 's/"url":"//' -e 's|/","full_path":".*||')

    if [ $? -ne 0 ]
    then
        echo -e '\n  Error accessing SourceForge...'
        exit 1
    fi
    FOUND=$(basename "$FOUND")

    if [ "$FOUND" != "$EXISTING" ]
    then
        echo "${cRed}$FOUND"
        echo -ne "    Updating in file ${cMagenta}MPV${cReset} version ... "
        sed -i "s/$EXISTING/$FOUND/" pyradio/win.py
        echo -e "${cGreen}done${cReset}"
    else
        echo "${cYellow}$FOUND${cReset}"
        echo -e "    Latest ${cMagenta}MPV${cReset} version already used..."
    fi
}

echo -e "Working on ${cBlue}pyradio/win.py${cReset}"
get_mpv
get_mplayer
exit 0
