#!/bin/bash -
#
# Skript zum Runterladen eines www.mikrocontroller.net Threads inklusive aller Attachments.
# Vorraussetzungen sind wget und bash; unter Windows kann https://www.msys2.org/ verwendet werden.
#
# Version 2026-08-06
# Verbesserungen und Fragen an <udo.krebelder@gmx.at>
#
# Aufruf:
# ./uC_download.bash https://www.mikrocontroller.net/news/mikrocontroller-net-design-update-august-2026 https://www.mikrocontroller.net/topic/586551#new
#
# Die Threads sind nacher im lokalen Verzeichnis www.mikrocontroller.net/news und www.mikrocontroller/topic gespeichert.
#
#

#accept_regex='(https://www.mikrocontroller.net/attachment/.*/.*)|(https://www.mikrocontroller.net/.*/.*page)'
accept_regex='(https://www.mikrocontroller.net/(topicX|forumX|assets|images)/.*)|(https://www.mikrocontroller.net/attachment/.*/.*)|(https://www.mikrocontroller.net/.*/.*page.*)|(https://www.mikrocontroller.net/favicon.ico))'

reject_regex='(https://www.mikrocontroller.net/topic/.*\?(edit|token)=.*)|(https://www.mikrocontroller.net/topic/(vote_post|merge).*)|(https://www.mikrocontroller.net/topic/.*reply_to=.*)|(https://www.mikrocontroller.net/topic/(goto_post|set_sticky_true|lock_for_guests|move|subscribe|merge|delete_post)/.*)|(https://www.mikrocontroller.net/articles/Spezial.*)'

for url in "$@"
do
    # remove ?page=2 or ?goto=new#new
    url="${url%%\?*}"
    # remove #xxxxxxx
    url="${url%%#*}"

    wget -e robots=off -N --no-if-modified-since -r -l3 -k -E --accept-regex "$accept_regex" --reject-regex="$reject_regex" "$url" 

    file="${url#https://}"
    #file="${file/\?/@}"
    #echo $file

    for f in "${file}"*
    do
        echo "Remove ad-scripts: $f"
        sed -i -e 's/src\=\(.\)https:/src\=\1Xhttps/' -e 's/src\=\(.*4977.js\)/\1X/' "$f"
    done

done
