#!/bin/bash
help(){
echo "Usage: $0 [2/3/-u]

Available options:
  2   build using python v. 2.x
  3   build using python v. 3.x
  -u  uninstall pyradio

If no option is used, will build using system default

Checking python availability:"

            for n in "" 2 3;do
                echo -n "  python${n}"
                [ -z "$n" ] && echo -n " "
                echo -n "   ...   "
                python${n} --version 2>/dev/null 1>&2 && {
                    python${n} --version
                } || echo not found
            done
            echo
}

uninstall(){
    sudo rm -f `which pyradio` 2>/dev/null
    if [ -d /usr/share/doc/pyradio ];then
        sudo rm -rf /usr/share/doc/pyradio 2>/dev/null
    else
        sudo rm -rf /usr/local/share/doc/pyradio 2>/dev/null
    fi
    sudo rm -f /usr/share/man/man1/pyradio.1.gz 2>/dev/null || sudo rm -f /usr/local/share/man/man1/pyradio.1.gz 2>/dev/null
    echo "PyRadio successfully uninstalled"
}

while [[ $# -gt 0 ]]
do
    key="$1"
    case $key in
        -h|--help)
            help
            exit
            ;;
        -u)
            uninstall
            exit
            ;;
        2)
            TO_PYTHON=2
            shift
            ;;
        3)
            TO_PYTHON=3
            shift
            ;;
        *)    # unknown option
            POSITIONAL+=("$1") # save it in an array for later
            shift # past argument
            ;;
    esac
done
set -- "${POSITIONAL[@]}" # restore positional parameters

## check dependencies :)
#for prog in git sed ;do
#    ${prog} --version 2>/dev/null 1>&2 || {
#        echo "Error: ${prog} not found."
#        echo "       Please install it and try again."
#        exit 1
#    }
#done

# check dependencies :)
for prog in git sed ;do
    ${prog} --version 2>/dev/null 1>&2 || {
        if [ "${prog}" = "sed" ];then
            sed 's/a/b/' LICENCE > /dev/null ||{
                echo "Error: ${prog} not found."
                echo "       Please install it and try again."
                exit 1
            }
        else
                echo "Error: ${prog} not found."
                echo "       Please install it and try again."
                exit 1
        fi
    }
done

# delete any files that were left from previous attempt
sudo find . -iname "*.pyc" -delete 2>/dev/null
sudo find . -iname "*.egg" -delete 2>/dev/null

# add git repo info to project
descr=$(git describe --long --tags 2>/dev/null || echo not_from_git)

[ -z "$descr" ] || {
    echo -n "s/git_description = ''/git_description = '" > sed.$$
    echo -n "${descr}" >> sed.$$
    echo "'/" >> sed.$$
    sed -f sed.$$ pyradio/radio.py > tmp-radio.py && mv -f tmp-radio.py pyradio/radio.py || {
        # failed... restore radio.py
        # and build without revision info
        git checkout pyradio/radio.py
        descr=''
    }
}
rm sed.$$ 2>/dev/null

# build and install project
python"${TO_PYTHON}" setup.py build && {
    echo "***** installing..."
    sudo python"${TO_PYTHON}" setup.py install && {
        gzip -k pyradio.1
        sudo mv -f pyradio.1.gz /usr/share/man/man1 2>/dev/null || sudo mv -f pyradio.1.gz /usr/local/share/man/man1
        DOC=/usr/share/doc/pyradio
        sudo mkdir "$DOC" 2>/dev/null
        if [ ! -d "$DOC" ];then
            # Mac OS SIP protects /usr
            DOC=/usr/local/share/doc/pyradio
            mkdir "$DOC"
        fi
        for n in README.* build.*
        do
            sudo cp "$n" "$DOC"
        done
        # copy LICENCE
        if [ "$DOC" = "/usr/share/doc/pyradio" ];then
            sudo mkdir -p /usr/share/licenses/pyradio 2>/dev/null
            sudo cp LICENCE /usr/share/licenses/pyradio
        else
            sudo cp LICENCE "$DOC"
        fi
    }
}

# restore files to original state
[ -z "$descr" ] || {
    echo "s/git_description = '[^']*'/git_description = ''/" > sed.$$
    sed -f  sed.$$ pyradio/radio.py > tmp-radio.py
    mv -f tmp-radio.py pyradio/radio.py
    #git checkout pyradio/radio.py
}
rm sed.$$ 2>/dev/null
