#!/bin/sh

get_option() {
  uci get -q proxy_config.$1 | tr -d '\n'
}

get_http_proxy() {
    export AUTH=""
    if [ "$(get_option http.enable)" = "true" ]; then
        if [ -n "$(get_option http.username)" ]; then
            if [ -n "$(get_option http.password)" ]; then
                AUTH=$(get_option http.username):$(get_option http.password)@
            else
                AUTH=$(get_option http.username)@
            fi
        else
            AUTH=""
        fi
        echo -n "http://$AUTH$(get_option http.host):$(get_option http.port)"
    fi
}


get_https_proxy() { 
    export AUTH=""
    if [ "$(get_option https.enable)" = "http" ]; then
        get_http_proxy
    elif [ "$(get_option https.enable)" = "true" ]; then
        if [ -n "$(get_option https.username)" ]; then
            if [ -n "$(get_option https.password)" ]; then
                AUTH=$(get_option https.username):$(get_option http.password)@
            else
                AUTH=$(get_option https.username)@
            fi
        else
            AUTH=""
        fi
        echo -n "https://$AUTH$(get_option https.host):$(get_option https.port)"
    fi
}

usage()
{
    echo "Usage:"
    echo "  export http_proxy=\$(get-proxy-env http)"
}


if [ $# -ne 1 ]; then
    usage
elif [ "$1" = "http" ]; then
  get_http_proxy
else
  usage        
fi

 
