#!/bin/bash
# -*- coding: utf-8 mode: sh -*- vim:sw=4:sts=4:et:ai:si:sta:fenc=utf-8
source "$(dirname -- "$0")/lib/ulib/auto" || exit 1

function display_help() {
    uecho "$scriptname: faire un miroir d'un site web

USAGE
    $scriptname [options] url [wget_options]

OPTIONS
    -l, --local
        Convertir les liens pour consultation locale
    -g, --no-robots
        Ne pas tenir compte du fichier robots.txt
    -n, --no-index
        Ne pas télécharger les fichiers de la forme 'index.html*'. Utile pour
        faire un miroir d'un site ftp qui est servi en http."
}

local=
no_robots=
no_index=
parse_opts + "${PRETTYOPTS[@]}" \
    --help '$exit_with display_help' \
    -l,--local local=1 \
    -g,--no-robots no_robots=1 \
    -n,--no-index no_index=1 \
    @ args -- "$@" && set -- "${args[@]}" || die "$args"

url="$1"; shift
[ -n "$url" ] || exit_with display_help

if ! ask_yesno -c "\
Voulez-vous télécharger et faire un mirroir du site${local:+ (les liens seront convertis pour consultation locale)}
'$url'?" O; then
    exit 1
fi

args=(-m -p -np)
[ -n "$local" ] && args=("${args[@]}" -k -K)
[ -n "$no_robots" ] && args=("${args[@]}" -e robots=off)
[ -n "$no_index" ] && args=("${args[@]}" -R "index.html*")
wget "${args[@]}" "$@" "$url"