#!/bin/sh
# This is the location of ./i2prouter
I2P="" # If lib/i2p.jar not found please specifiy location, example: I2P="/home/user/apps/i2p"

 # I2P Variable not set, try and set our own location
if [ -z "$I2P" ]; then
    I2P="$(cd "$(dirname "$0")" && pwd)"
fi

# Missing file
if [ ! -f "$I2P/lib/i2p.jar" ]; then
    echo "File $I2P/lib/i2p.jar not found, please edit $0 and change the I2P variable(generally set to the i2prouter location)."
    exit 1
fi

 # Send help
send_help() {
    java -cp "$I2P/lib/i2p.jar" net.i2p.util.EepGet
    if [ "$1" ]; then exit $1; else exit 0; fi
}

 # Requesting help
if [ -z "$1" ] || [ "$1" = "-h" ] || [ "$1" = "--help" ]; then
    send_help
fi

 # Parse arguments
args=""
for a in $@; do
    case $a in
        -c) # -c no proxy
            no_proxy=true
            args="$args -c "
            shift
            ;;
        -e) # -e etag
            shift
            args="$args -e $1 "
            shift
            ;;
        -h) # -h header=value
            shift
            args="$args -h $1 "
            shift
            ;;
        -l) # -l lineLen
            shift
            linelen="$(echo $1 | grep -Eo '[0-9]{1,9}')"
            if [ -z "$linelen" ]; then
                echo " ✖ lineLen, must be specified in numbers."
                send_help 1
            else
                args="$args -l $linelen "
            fi
            shift
            ;;
        -m) # -m markSize
            shift
            marksize="$(echo $1 | grep -Eo '[0-9]{1,9}')"
            if [ -z "$marksize" ]; then
                echo " ✖ markSize, must be specified in numbers."
                send_help 1
            else
                args="$args -m $marksize "
            fi
            shift
            ;;
        -n) # -n # of retries
            shift
            retries="$(echo $1 | grep -Eo '[0-9]{1,9}')"
            if [ -z "$retries" ]; then
                echo " ✖ retries, must be specified in numbers."
                send_help 1
            else
                args="$args -n $retries "
            fi
            shift
            ;;
        -o) # -o output filename
            shift
            output="$1"
            args="$args -o $output "
            shift
            ;;
        -p) # -p proxy:port
            shift
            if [ "$1" = ":0" ]; then
                no_proxy=true
            fi
            proxy="$1"
            args="$args -p $proxy "
            shift
            ;;
        -t) # -t timeout(seconds)
            shift
            timeout="$(echo $1 | grep -Eo '[0-9]{1,9}')"
            if [ -z "$timeout" ]; then
                echo " ✖ timeout, must be specified in seconds."
                send_help 1
            else
                args="$args -t $timeout "
            fi
            shift
            ;;
        -u) # -u username
            shift
            username="$1"
            args="$args -u $username "
            shift
            ;;
        -x) # -x password
            shift
            password="$1"
            args="$args -x $password "
            shift
            ;;
        http*://*)
            #eep_url="$(echo $1 | grep -iEo '(http|https)://[a-z0-9./?=_%:-]*')"
            eep_url="$(echo $1)"
            args="$args $eep_url "
            ;;
    esac
done

if [ "$no_proxy" ] && [ "$proxy" ]; then
    echo " ✖ arguments -c and -p can not be used together."
    send_help 1
fi

if [ -z "$eep_url" ]; then
    echo " ✖ Invalid or missing URL, eg. $0 http://skank.i2p/dev/i2pupdate.zip"
    send_help 1
fi

echo " ➤ Attempting eepget retrieval of $eep_url…"

cmd="java -cp "$I2P/lib/i2p.jar" net.i2p.util.EepGet "$args""
eval ${cmd}
exit $?
