#!/bin/sh

updates="https://raw.github.com/javier-lopez/learn/master/sh/is/apt-proxy"

_header() {
    clear
    printf "%b\\n" "\033[1m-------------------------\033[7m Setup apt proxy \033[0m\033[1m----------------------------\033[0m"
    printf "%b\\n" "\033[1mUpdates:\033[0m ${updates}"
    printf "%b\\n" "\033[1m----------------------------------------------------------------------\033[0m"
}

_whereis() {
    [ -z "${1}" ] && return 1
    [ -n "${2}" ] && return 1
    if ! command -v "${1}" 2>/dev/null; then
        return 1
    fi
}

_existaptproxy() {
    avahi-browse -a  -t | grep apt-cacher-ng >/dev/null && return 0
    return 1
}

_animcui() {
    [ -z "${1}" ] && { printf "%5s\n" ""; return 1; }

    if ! printf "%s" "$(pidof "${1}")" | grep "[0-9].*" >/dev/null; then
        printf "%5s\n" ""
        return 1;
    fi

    _animcui__animation_state="1"

    if [ ! "$(ps -p "$(pidof "${1}")" -o comm= 2>/dev/null)" ]; then
        printf "%5s\n" ""
        return 1
    fi

    printf "%5s" ""

    while [ "$(ps -p "$(pidof "${1}")" -o comm= 2>/dev/null)" ]; do
        printf "%b" "\b\b\b\b\b"
        case "${_animcui__animation_state}" in
            1) printf "%s" '\o@o\'
               _animcui__animation_state="2" ;;
            2) printf "%s" '|o@o|'
               _animcui__animation_state="3" ;;
            3) printf "%s" '/o@o/'
               _animcui__animation_state="4" ;;
            4) printf "%s" '|o@o|'
               _animcui__animation_state="1" ;;
        esac
        sleep 1
    done
    printf "%b" "\b\b\b\b\b" && printf "%5s\n" ""
}

_getroot() {
    if [ ! X"${LOGNAME}" = X"root" ]; then
        printf "%s\\n" "Detecting user ${LOGNAME} (non-root) ..."
        printf "%s\\n" "Checking if sudo is available ..."

        if command -v "sudo" >/dev/null 2>&1; then
            sudo -K

            if [ -n "${sudopwd}" ]; then
                # password check
                _getroot__test="$(printf "%s\\n" "${sudopwd}" | sudo -S ls 2>&1)"
                _getroot__status="${?}"
                _getroot__not_allowed="$(printf "%s" "${_getroot__test}" | \
                                         grep -i "sudoers")"

                if [ -n "${_getroot__not_allowed}" ]; then
                    printf "%s %s\\n" "You're not allowed to use sudo," \
                    "get in contact with your local administrator"
                    exit
                fi

                if [ X"${_getroot__status}" != X"0" ]; then
                    sudopwd=""
                    printf "%s\\n" "Incorrect preseed password"
                    exit
                else
                    sudocmd="sudo -S"
                fi
                printf "%s\\n" "    - all set ..."
                return
            fi

            i=0 ; while [ "${i}" -lt "3" ]; do
                i="$((${i} + 1))"
                printf "%s" "   - enter sudo password: "
                stty -echo
                read sudopwd
                stty echo

                # password check
                _getroot__test="$(printf "%s\\n" "${sudopwd}" | sudo -S ls 2>&1)"
                _getroot__status="${?}"
                _getroot__not_allowed="$(printf "%s" "${_getroot__test}" | \
                                         grep -i "sudoers")"

                if [ -n "${_getroot__not_allowed}" ]; then
                    printf "\\n%s %s\\n" "You're not allowed to use sudo," \
                    "get in contact with your local administrator"
                    exit
                fi

                printf "\\n"
                if [ X"${_getroot__status}" != X"0" ]; then
                    sudopwd=""
                else
                    sudocmd="sudo -S"
                    break
                fi
            done

            if [ -z "${sudopwd}" ]; then
                printf "%s\\n" "Failed authentication"
                exit
            fi
        else
            printf "%s %s\\n" "You're not root and sudo isn't available." \
            "Please run this script as root!"
            exit
        fi
    fi
}

_cmdsudo() {
    [ -z "${1}" ] && return 1

    printf "%s \\n" "    $ sudo ${*}"
    _cmdsudo__output="$(printf "%s\\n" "${sudopwd}" | ${sudocmd} sh -c "eval ${*}" 2>&1)"
    _cmdsudo__status="${?}"

    if [ X"${_cmdsudo__status}" != X"0" ]; then
        printf "> %s:%s\\n" "${*}" "${_cmdsudo__output}"
        printf "\\n"
        exit "${_cmdsudo__status}"
    else
        return "${_cmdsudo__status}";
    fi
}

_waitforsudo() {
    [ -z "${1}" ] && return 1

    printf "%s " "    $ sudo ${@} ..."
    printf "%s\\n" "${sudopwd}" | ${sudocmd} sh -c "eval ${*}" >/dev/null 2>&1 &
    sleep 1s

    if [ X"${1}" = X"DEBIAN_FRONTEND=noninteractive" ]; then
        _animcui "${2}"
    else
        _animcui "${1}"
    fi
}

_header
_getroot

printf "%s\\n" "[+] setting up an apt-get proxy ..."

if ! _whereis avahi-browse >/dev/null; then
    _waitforsudo apt-get update
    _waitforsudo apt-get install --no-install-recommends -y avahi-utils
fi

if _existaptproxy; then
    apt_proxy_server="$(avahi-browse -a -t -r -p | grep apt-cacher-ng | grep = | cut -d";" -f8)"
    printf "%s\\n" "[+] exists an apt-get proxy in the network at ${apt_proxy_server}, using it ..."
    [ ! -f /usr/share/squid-deb-proxy-client/apt-avahi-discover ] && _waitforsudo apt-get install --no-install-recommends -y squid-deb-proxy-client
else
    printf "%s\\n" "[+] no apt-get proxy found, installing one locally ..."
    _waitforsudo apt-get install --no-install-recommends -y squid-deb-proxy-client apt-cacher-ng
    [ ! -f /etc/avahi/services/apt-cacher-ng.service ] && _waitforsudo wget http://javier.io/mirror/apt-cacher-ng.service -O /etc/avahi/services/apt-cacher-ng.service
    if [ -d "${HOME}"/misc/ubuntu/proxy/apt-cacher-ng/ ]; then
        printf "%s\\n" "[+] exporting files ..."
        _cmdsudo rm -rf /var/cache/apt-cacher-ng
        _cmdsudo ln -s "${HOME}"/misc/ubuntu/proxy/apt-cacher-ng/ /var/cache/apt-cacher-ng
    fi
fi

printf "%s\\n" "[+] done!, have fun!"