## Variables
author="jcorporation"
package="myMPD"
version="${1:-"6.7.0"}"
url="https://github.com/$author/$package/archive/v$version.tar.gz"
archive="$(basename "$url")"
package_lower="$(echo "$package" | tr '[:upper:]' '[:lower:]')"
port="${2:-8080}"

## Download, build and install
# https://github.com/jcorporation/myMPD#quick-build-instructions
# https://github.com/jcorporation/myMPD/wiki/Building-myMPD#packaging
(
    # Create and change to temporary working directory.
    tmp_dir="$(mktemp -d)"
    trap 'rm -rf "$tmp_dir"' EXIT
    cd "$tmp_dir"
    # Download and extract.
    wget "$url"
    tar -xvf "$archive"
    cd "$package-$version"
    # Install dependencies, build and install.
    sudo ./build.sh installdeps
    sudo apt-get install debhelper lintian
    # ./build.sh release
    # sudo ./build.sh install
    package_lower="$(echo "$package" | tr '[:upper:]' '[:lower:]')"
    ./build.sh pkgdebian
    sudo dpkg --install "package/${package_lower}_${version}-1_"*".deb"
)

## Configure.
# https://github.com/jcorporation/myMPD/wiki/myMPD-behind-a-reverse-proxy
sudo sed -i "/etc/mympd.conf" \
    -e 's/^\(ssl *=\).*/\1 false/' \
    -e 's/^\(webport *=\).*/\1 127.0.0.1:'"$port"'/'

# TODO:
# - Auto play.
# - Pagination: 200.
# - `smartpls = true`
# - `streamport = 8000`