summaryrefslogtreecommitdiff
path: root/wget
blob: d55583afe0e94cf4c5dca538281c7bbe3b575b68 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#!/bin/bash
# wget-curl - drop-in replacement for wget using curl as backend - version @@VERSION@@

# Copyright (c) 2015 Kylie McClain <somasis@exherbo.org>
# Permission to use, copy, modify, and/or distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#
# End of copyright.

# initialization
# variables
VERSION=1.17
sysconfdir=/etc
datadir=/usr/share

# functions
stderr() { # stderr <msg>
    echo "$@" >&2
}

info() {
    stderr "$@"
}

debug() { # debug [msg]
    if [[ -z "$debug" ]];then
        return 1 # debug not set
    elif [[ ! -z "$debug" && "$#" -gt 0 ]];then
        stderr "$@"
    else
        return 0
    fi
}

wget_version() {
    local wget_features="+digest"
    local curl_features=$(curl-config --protocols --features --configure | tr "'" '\n' | grep -v '^ $')
    local   ntlm_type=-ntlm https_type=-https ipv6_type=-ipv6 \
            largefile_type=-large-file ssl_type=-ssl iri_type=-iri \
            nls_type=-nls psl_type=-psl opie_type=-opie digest_type=+digest
    local curl_configure=$(curl-config --configure | tr "'" '\n')
    local cc=$(curl-config --cc)
    local curl_cflags=$(curl-config --cflags)
    local feat
    for feat in ${curl_features};do
        case "${feat}" in
            HTTPS)              https_type=+https           ;;
            NTLM)               ntlm_type=+ntlm             ;;
            IPv6)               ipv6_type=+ipv6             ;;
            --enable-largefile) largefile_type=+large-file  ;;
            --with-ssl)         ssl_type=+ssl/openssl       ;;
            --with-gnutls)      ssl_type=+ssl/gnutls        ;;
            --with-libidn)      iri_type=+iri               ;;
            --with-nls)         nls_type=+nls               ;;
        esac
    done
    for conf in ${curl_configure};do
        case "${conf}" in
            CFLAGS=*)   cflags=${conf/CFLAGS=}      ;;
            CPPFLAGS=*) cppflags=${conf/CPPFLAGS=}  ;;

        esac
    done
    wget_compile="${cc} ${curl_cflags} ${cflags} ${cppflags}"
    wget_features=$(echo $ntlm_type $https_type $ipv6_type $largefile_type $ssl_type $iri_type $nls_type $psl_type $opie_type $digest_type | tr ' ' '\n' | LC_ALL=C sort -d | tr '\n' ' ')
    cat <<EOF
GNU Wget ${VERSION}

${wget_features}

Wgetrc:
EOF
    [[ "${HOME}/.wgetrc" ]] && echo "    ${HOME}/.wgetrc (user)"
    [[ "${sysconfdir}"/wgetrc ]] && echo "    ${sysconfdir}/wgetrc (system)"
    cat <<EOF
Locale:
    ${datadir}/locale
Compile:
    ${wget_compile}
Link:
    ${wget_compile}
EOF
}

wget_help() {
    echo "yep"
}

echo "$0 $@" >&2
set -- $(getopt -n wget -o 'VhcO:T:t:qH:' -l 'version,help,continue,output-document:,timeout:,tries:,quiet,post-data:,post-file:,header:' -- $@)
echo "$@" >&2
while [[ $# -ne 0 ]];do
    case "$1" in
        --) shift;urls="$@" ;;
        -V|--version)           wget_version                            ;;
        -h|--help)              wget_help                               ;;
        -c|--continue)          continue="-C -"                         ;;
        -O|--output-document)   shift;output="-o ${1}"                  ;;
        -T|--timeout)           shift;timeout="--connect-timeout ${1}"  ;;
        -t|--tries)             shift;tries="--retry ${1}"              ;;
        -q|--quiet)             quiet='-s'                              ;;
        --post-data)            shift;post_data="--data-raw ${1}"       ;;
        --post-file)            shift;post_file="--data @${1}"          ;;
        -H|--header)            shift;header="--header ${1}"            ;;
    esac
    shift
done

default='-L --remote-name-all -i'

eval curl ${default} ${continue} ${output} ${timeout} ${tries} ${quiet} ${post_data} ${post_file} -- ${urls}