#!/bin/sh
set -e
URL="https://files.pythonhosted.org/packages/05/98/cbcb0baab29697375ff2b6613d742d0931084b2b427c657b181d3aa734fc/pyprland-2.4.4-py3-none-any.whl"
SUDO=sudo
PROGRAM=/usr/local/bin/pypr
CACHEDIR=/var/cache/pypr
WHEEL=package.whl

if [ -e "$PROGRAM" ]; then
    echo "$PROGRAM exists, do you really want to continue [y/n] ?"
    read answer
    if [ "$answer" != "y" ]; then
        exit 1
    fi
fi

[ ! -d $CACHEDIR ] && ${SUDO} mkdir $CACHEDIR
cd $CACHEDIR
${SUDO} curl -o $WHEEL $URL

append() {
    echo $@ | ${SUDO} tee -a $PROGRAM
}

${SUDO} rm -fr ${PROGRAM}
${SUDO} touch ${PROGRAM}
${SUDO} chmod 755 ${PROGRAM}
${SUDO} chmod 755 $CACHEDIR
${SUDO} chmod 644 $CACHEDIR/$WHEEL

append "#!/bin/sh"
append "export PYTHONPATH='$CACHEDIR/$WHEEL'"
append 'exec python -m pyprland.command $@'

echo "Installed successfully."