#!/usr/bin/env bash
function help(){
echo "Usage: $0 [2/-R]"
echo "
Available options:
     2   build using python v. 2.7 (linux only)
    -R   remove (uninstall) pyradio
    -t   Use this terminal for Desktop file (linux only)

If no option is used, will build using Python 3
and will install in your HOME directory

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
}

function pipx_error(){
cat <<END
The installation has failed!!!
Please report this at https://github.com/coderholic/pyradio/issues

END
exit 1
}

function pip_error(){
cat <<END
Your installation is missing python's package manager (pip)
Please install it and try again...

END
exit 1
}

function find_paths(){
local APATH
python devel/site.py 2>/dev/null
python2 devel/site.py 2>/dev/null
python3 devel/site.py 2>/dev/null
APATH=$(python -m site --user-site 2>/dev/null) && echo "$APATH"
APATH=$(python2 -m site --user-site 2>/dev/null) && echo "$APATH"
APATH=$(python3 -m site --user-site 2>/dev/null) && echo "$APATH"
}

function remove_paths(){
echo "Looking for python installed files..."
find_paths | sort -u > /tmp/pyradio-uninstall.$$
while read line;do
    [ -d "$line" ] && {
    sudo find "$line" -type d -name "pyradio*egg" >> /tmp/pyradio-delete.$$
    }
done < /tmp/pyradio-uninstall.$$
while read line;do
    [ -d "$line" ] && {
        echo -n "** Removing \"$(basename $line)\" ... "
        sudo rm -rf "$line"
        echo done
    }
done < /tmp/pyradio-delete.$$
rm -f /tmp/pyradio-uninstall.$$ /tmp/pyradio-delete.$$ 2>/dev/null
}

function remove_pip_sudo_install(){
    sudo python -m pip uninstall -y pyradio 2>/dev/null 1>&2 || pip_error
    sudo python2 -m pip uninstall -y pyradio 2>/dev/null 1>&2
    sudo python3 -m pip uninstall -y pyradio 2>/dev/null 1>&2
}

function uninstall(){
    echo 'Uninstalling PyRadio'
    echo -n '  ** Removing python files ... '
    pipx list 2>/dev/null 1>&2 && PIPX=1
    if [ -z "${PIPX}" ]
    then
        python -m pip uninstall -y pyradio 2>/dev/null 1>&2
        python2 -m pip uninstall -y pyradio 2>/dev/null 1>&2
        python3 -m pip uninstall -y pyradio 2>/dev/null 1>&2
    else
        pipx uninstall pyradio 2>/dev/null 1>&2
    fi
    echo 'done'
    echo -n '  ** Removing help files ... '
    if [ -d ~/.local/share/doc/pyradio ]
    then
        rm -rf ~/.local/share/doc/pyradio 2>/dev/null
    else
        rm -rf ~/.local/local/share/doc/pyradio 2>/dev/null
    fi
    rm -f ~/.local/share/man/man1/pyradio*gz 2>/dev/null
    rm -f ~/.local/share/applications/pyradio.desktop 2>/dev/null
    echo 'done'
    if [ $(uname -s) = "Darwin" ] || [ $(uname -s) = "darwin" ]
    then
        # delete link on mac
        [ -h ~/bin/pyradio ] && {
            echo -n '  ** Removing link to executable ... '
            rm ~/bin/pyradio 2>/dev/null
            echo 'done'
        }
    fi
    rm ~/.config/pyradio/data/INSTALLATION_TYPE 2>/dev/null
    echo "PyRadio successfully uninstalled"
}

function uninstall_sudo(){
    local user
    [ -z "$1" ] && {
        user=$(which pyradio 2>/dev/null) || {
            echo "PyRadio is not installed..."
            exit
        }
    }
    sudo echo 'Uninstalling PyRadio'
    echo -n '  ** Removing executable ... '
    sudo rm -f `which pyradio 2>/dev/null` 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
    echo done
    echo -n '  ** Removing help files ... '
    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
    sudo rm -f /usr/share/man/man1/pyradio_rb.1.gz 2>/dev/null || sudo rm -f /usr/local/share/man/man1/pyradio_rb.1.gz 2>/dev/null
    sudo rm -f /usr/share/man/man1/pyradio_server.1.gz 2>/dev/null || sudo rm -f /usr/local/share/man/man1/pyradio_server.1.gz 2>/dev/null
    echo done
#set -x
    remove_paths
    remove_pip_sudo_install
    if [ $(uname -s) = "Darwin" ] || [ $(uname -s) = "darwin" ]
    then
        # delete link on mac
        [ -h ~/bin/pyradio ] && rm ~/bin/pyradio 2>/dev/null
    fi
    echo "PyRadio successfully uninstalled"
}

function get_cur_version(){
    cur_version=$(grep 'version_info = ' pyradio/__init__.py | sed 's/version_info = (//;s/, /./g;s/)//')
    cur_state=$(grep 'app_state = ' pyradio/__init__.py | sed "s/app_state = //;s/'//g")
    [[ -z "$cur_state" ]] || cur_version="$cur_version"-"$cur_state"
    #echo "$cur_version"
}

function do_dev(){
    if [ -z "${NO_DEV}" ]
    then
        [ -z "$DEVEL" ] && {
            rm pyradio/config.pydev 2>/dev/null
            return
        }
        sed -i "s/git_description = ''/git_description = '$DEVEL'/" pyradio/config.py
    fi
}

function do_undev(){
    if [ -e pyradio/config.pydev ]
    then
        mv pyradio/config.pydev pyradio/config.py
    fi
}

function do_exit(){
    # remove instasllation directory
    if [ ! -z "$REMOVE_DIR" ]
    then
        if [ "$REMOVE_DIR" = "$HOME/pyradio" ] || \
                [ "$REMOVE_DIR" = "$HOME/tmp-pyradio" ] || \
                [ "$REMOVE_DIR" = "/tmp/pyradio" ] || \
                [ "$REMOVE_DIR" = "/tmp/tmp-pyradio" ]
        then
            rm -fr "$REMOVE_DIR"
        fi
    fi
}

function check_installation_type(){
    if [ -z "$PIPX" ]
    then
        echo
        echo 'Error: Inconsistent installation type!'
        echo
        echo 'A previous pipx installation has been retected,'
        echo 'but pipx is not installed in your system anymore.'
        echo 'Please install pipx and try again!'
        exit 1
    fi
}

function create_installation_type_file(){
    mkdir -p ~/.config/pyradio/data && \
        echo "$inst_type" > ~/.config/pyradio/data/INSTALLATION_TYPE
    if (( $inst_type > 0 ))
    then
        OLD=$(find ~/.local/lib -type d -name "pyradio*" 2>/dev/null | grep -v pipx | grep -v venvs)
        if [ ! -z "$OLD" ]
        then
            if [ "$TO_PYTHON" = "2" ]
            then
        echo
        echo
        echo "The following pyradio directories have been found:
$OLD

They probably belong to an older non-pipx installation
and they should be deleted.
"
        else
            msg="

The following [magenta]pyradio[/magenta] directories have been found:
[red]$OLD[/red]

They probably belong to an older non-pipx installation
and they [red]should be deleted[/red].
"
            python${TO_PYTHON} -c \
                'import sys; from rich import print; print(sys.argv[1])' \
                "${msg}"
        fi
        fi
    fi
}

function check_root(){
    if [ "$UID" = "0" ] || [ "$USER" = "root" ]
    then
        echo "Error: You must not run this script as root"
        echo
        exit 1
    fi
}

check_root
rm -rf build pyradio.egg-info 2>/dev/null
TO_PYTHON=3
TO_PYTHON_FROM_X=''
NO_DEV=''
while [[ $# -gt 0 ]]
do
    key="$1"
    case $key in
        -h|--help)
            help
            exit
            ;;
        -t)
            shift
            if [ -z "$1" ]
            then
                echo "Error: No terminal specified..."
                echo
                exit 1
            fi
            REQ_TERMINAL="$1"
            shift
            ;;
        --warning)
            # macos only; execute "put pyradio in PATH" and
            # display warning (not error) if pyradio executable
            # is not found
            #
            # DO NOT USE IT on any other circumstance
            #
            WARNING=1
            shift
            ;;
        --isolate|-i)
            PIPX_ISOLATE=1
            shift
            ;;
        -R)
            uninstall
            exit
            ;;
        2)
            [ -z "${TO_PYTHON_FROM_X}" ] && TO_PYTHON=2
            if [ $(uname -s) = "Darwin" ] || [ $(uname -s) = "darwin" ]
            then
                echo
                echo
                echo "Error: PyRadio does not support python 2 any more."
                echo "Please install python 3 and try again."
                exit 1
            fi
            shift
            ;;
        -x)
            #
            # Hidden parameter; to be used by
            #     pyradio -U, -R
            # command line parameters
            #
            # DO NOT USE IT on any other circumstance
            #
            REMOVE_DIR=$(dirname "$PWD")
            shift
            TO_PYTHON=${1/python/}
            TO_PYTHON_FROM_X=1
            shift
            ;;
        -n)
            NO_DEV=1
            shift
            ;;
        *)    # unknown option
            POSITIONAL+=("$1") # save it in an array for later
            shift # past argument
            ;;
    esac
done
set -- "${POSITIONAL[@]}" # restore positional parameters

# if [ -z "$NO_DEV" ]
# then
#     cd pyradio
#     DEVEL=$(python -c 'from install import get_github_long_description_for_script; get_github_long_description_for_script()')
#     # DEVEL=$(python -c 'from install import get_devel_version; print(get_devel_version())')
#     echo "Devel version: $DEVEL"
#     cd ..
# fi


## 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
for prog in 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

# uninstall previous versions
# uninstall 1

# # 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

# echo "***** installing for user..."
do_dev

if [ "${TO_PYTHON}" == "2" ];
then
    tar czf pyradio.tar.gz pyradio
    for a_file in pyradio/*.py
    do
        sed -i "/open/s/, encoding='utf-8'//" "${a_file}"
        sed -i 's/console.print/# &/' "${a_file}"
        sed -i 's/from rich import print/# &/' "${a_file}"
        sed -i '/##### NO PYTHON2/,/##### END NO PYTHON2/d' "${a_file}"
    done
else
    # Use pipx by default, if installed
    pipx list 2>/dev/null 1>&2 && PIPX=1
fi


if [ $(uname -s) = "Darwin" ] || [ $(uname -s) = "darwin" ]
then
    [ -z "$PIPX" ] && {
        echo
        echo
        echo "Error: pipx not found."
        echo "       Please install pipx and try again"
        exit 1
    }
fi


if [ "${TO_PYTHON}" != "2" ];
then
    # Make an editable installation if
    #       ~/.pyradio-editable
    # exists
    test -e ~/.pyradio-editable && PIPX_EDITABLE="-e"

    inst_type=0
    if [ -e ~/.config/pyradio/data/INSTALLATION_TYPE ]
    then
        # we have an update
        # read INSTALLATION_TYPE and identify installation file
        read inst_type < ~/.config/pyradio/data/INSTALLATION_TYPE
        if [ ! -z "$inst_type" ]
        then
            if [ "$inst_type" = "1" ]
            then
                # pipx installation without isolation
                check_installation_type
            elif [ "$inst_type" = "2" ]
            then
                # pipx installation with isolation
                check_installation_type
                PIPX_ISOLATE=1
            else
                # normal pip installation
                if [ ! -z "$PIPX" ]
                then
                    inst_type=1
                fi
            fi
        fi
    fi
fi

if [ -z "$PIPX" ]
then
    # Normanl, old style installation
    if [ -e requirements.txt ]
    then
        python"${TO_PYTHON}" -m pip install -r requirements.txt . || pip_error
    else
        python"${TO_PYTHON}" -m pip install . || pip_error
    fi
else
    # pipx installation
    # MacOS will always permorm an fully isolated installation
    if [ $(uname -s) == "Darwin" ] || [ $(uname -s) == "darwin" ]
    then
        PIPX_ISOLATE=1
    fi
    if [ -z "$PIPX_ISOLATE" ]
    then
        pipx install $PIPX_EDITABLE --system-site-packages --force . || pipx_error
        inst_type=1
    else
        pipx install $PIPX_EDITABLE --force . || pipx_error
        [ -z "$PIPX_EDITABLE" ] && \
            cat requirements_pipx.txt | sed -e 's/#.*//' | xargs pipx inject pyradio
        inst_type=2
    fi
fi

if [ "${TO_PYTHON}" == "2" ];
then
    tar xzf pyradio.tar.gz
    rm pyradio.tar.gz
fi

do_undev

if [ $? -eq 0 ]
then
    [ -z "${NO_DEV}" ] && [ -e pyradio/config.py.dev ] && mv pyradio/config.py.dev pyradio/config.py
    gzip -k pyradio.1
    gzip -k pyradio_rb.1
    gzip -k pyradio_server.1
    mkdir -p ~/.local/share/man/man1 2>/dev/null
    mv -f pyradio.1.gz ~/.local/share/man/man1 2>/dev/null || mv -f pyradio.1.gz ~/.local/local/share/man/man1
    mv -f pyradio_rb.1.gz ~/.local/share/man/man1 2>/dev/null || mv -f pyradio_rb.1.gz ~/.local/local/share/man/man1
    mv -f pyradio_server.1.gz ~/.local/share/man/man1 2>/dev/null || mv -f pyradio_server.1.gz ~/.local/local/share/man/man1
    DOC=~/.local/share/doc/pyradio
    mkdir -p "$DOC" 2>/dev/null
    if [ ! -d "$DOC" ]
    then
        # Mac OS SIP protects /usr
        DOC=~/.local/local/share/doc/pyradio
        mkdir "$DOC"
    fi
    for n in *.md *.html
    do
        cp "$n" "$DOC"
    done
    # copy LICENCE
    mkdir -p ~/.local/share/licenses 2>/dev/null
    if [ "$DOC" = "~/.local/share/doc/pyradio" ]
    then
        mkdir -p ~/.local/share/licenses/pyradio 2>/dev/null
        cp LICENCE ~/.local/share/licenses/pyradio
    else
        cp LICENCE "$DOC"
    fi
    if [ $(uname -s) != "Darwin" ] && [ $(uname -s) != "darwin" ]
    then
        if [ ! -z "$(grep 'distro = None' pyradio/config)" ]
        then
            if [ ! -e ~/.local/share/applications/pyradio.desktop ]
            then
                mkdir -p ~/.local/share/applications/ 2>/dev/null
                cp devel/pyradio.desktop ~/.local/share/applications/pyradio.desktop
            fi
            chmod +x ./devel/fix_pyradio_desktop_file
            if [ -z "${REQ_TERMINAL}" ]
            then
                ./devel/fix_pyradio_desktop_file -i
            else
                ./devel/fix_pyradio_desktop_file -i -t "${REQ_TERMINAL}"
            fi
        fi
    fi
    mkdir -p ~/.config/pyradio/data
    cp devel/pyradio.png ~/.config/pyradio/data
    [ -z $PIPX  ] || create_installation_type_file
else
    do_exit
    exit 1
fi

do_exit
exit 0
