#!/bin/bash
SOURCE="$0"
source /usr/lib/elive-tools/functions
EL_REPORTS="1"
#el_make_environment
#set -x
#set -e

get_baseurl(){
    # so like:  http://something.com
    local baseurl

    # first get a better identifier for parse (.com):
    baseurl="$( echo "$@" | sed -e "s|${http}://||g" -e 's|/.*$||g' -e 's|^.*\.|.|g' )"
    # get .com
    baseurl="$( echo "$@" | sed -e "s|${baseurl}/.*$|${baseurl}|g" )"
    # get:  http://something.com
    if [[ -n "$baseurl" ]] ; then
        #if [[ "$baseurl" = http* ]] ; then
            echo "$baseurl"
        #else
            #echo "http://$baseurl"
        #fi
        return 0
    else
        el_error "baseurl not get: $baseurl"
        return 1
    fi
}

main(){
    # pre {{{
    local url file http options

    # }}}

    tmpdir="/tmp/pandoc-${USER}-$$"
    url="$1"

    # Usage
    if [[ -z "${1}" ]] ; then
        echo -e "Usage: $(basename $BASH_SOURCE) weburl"
        exit 1
    fi

    if ! el_dependencies_check pandoc ; then
        el_dependencies_install pandoc
    fi
    el_dependencies_check "wget|sed"

    if [[ "${url}" = "https"* ]] ; then
        http="https"
    else
        http="http"
    fi


    baseurl="$( get_baseurl "$url" )"

    rm -rf "${tmpdir}"
    mkdir -p "${tmpdir}"
    cd "${tmpdir}"

    wget --timeout=320 -A Mozilla "$url" -O downloaded-original.html

    # fix double // for downloads
    sed -i "s|=\"//|=\"/|g" downloaded-original.html

    cat downloaded-original.html | iconv -f ISO-8859-15 -t utf8 | pandoc -s -r html - -o downloaded-original_utf8.text

    cat downloaded-original.html | pandoc -s -r html - -o downloaded-original.text

    cp downloaded-original.text downloaded.text
    cp downloaded-original_utf8.text downloaded_utf8.text

    mkdir -p files

    el_explain 0 "Downloading files:"

    while read -ru 3 line
    do
        line="$( echo "$line" | sed -e 's|).*$||g' -e 's|^.*(||g' -e 's|\].*$||g' -e 's|^.*\[||g' )"

        [[ -z "$line" ]] && continue
        [[ "$line" = "."* ]] && continue
        [[ "$line" != *"/"* ]] && continue


        cd files

        _baseurl="$baseurl"

        dirfile="${line%/*}"
        dirfile="${dirfile##*://}"
        dirfile="${dirfile#//}"
        dirfile="${dirfile#/}"
        dirfile="${dirfile#*/}"

        filename="${line##*/}"
        filename="${filename%]}"
        filename="${filename%)}"
        filename="${filename%%&*}"
        filename="${filename%%\?*}"

        line_clean="http${line##*http}" # since the last http field
        line_clean="${line#http/}" # this in case we did a wrong previous replacement
        line_clean="${line_clean%%&*}" # remove parameters url
        line_clean="${line_clean%%\?*}" # remove parameters url

        # url is somewhere else
        if [[ "$line_clean" = http* ]] && [[ "${line_clean}" != "$baseurl"* ]] ; then
            if wget -nc --timeout=320 -A Mozilla -q "$line_clean" ; then
                _baseurl="$( get_baseurl "$line_clean" )"
            else
                convert -size 32x32 xc:white "$filename"
            fi
        fi

        # this means the url can be something like:  src="//pixel.quantserve.com/pixel/p-FR2ZALpa42jxt.gif", which is not the same domain
        if [[ "${line}" = /* ]] ; then
            line_clean="${line_clean#/}"
            line_clean="${line_clean#/}"

            # try to download externally:
            if wget -nc --timeout=320 -A Mozilla -q "http://${line_clean}" ; then
                # update the baseurl for the later replacement
                _baseurl="$( get_baseurl "$line_clean" )"
            else
                # try to download externally (with httpS):
                if wget -nc --timeout=320 -A Mozilla -q "https://${line_clean}" ; then
                    _baseurl="$( get_baseurl "$line_clean" )"
                else

                    # try to download from the same domain then
                    line_clean="${baseurl}/${line_clean}"

                    if ! wget -nc --timeout=320 -A Mozilla -q "$line_clean" ; then
                        # no way? create an empty image instead:
                        # NOTE: we need an image in any of the cases so the epub creation will not fail (pandoc in general)
                        convert -size 32x32 xc:white "$filename"
                    fi
                fi
            fi
        else
            if ! wget -nc --timeout=320 -A Mozilla -q "$line_clean" ; then
                convert -size 32x32 xc:white "$filename"
            fi
        fi

        # no file? create it
        if ! [[ -s "$filename" ]] ; then
            convert -size 32x32 xc:white "$filename"
        fi

        mkdir -p "$dirfile"
        #cp -f "${filename}" "$dirfile" 2>/dev/null || cp -f *."${filename##*.}" "$dirfile"
        mv -f "${filename}" "$dirfile" 2>/dev/null || mv -f *."${filename##*.}" "$dirfile"

        cd ../
        echo "$line"

        # change the location of the files to here
        sed -i "/${filename}/s|${_baseurl}/|files/|g" downloaded.text
        sed -i "/${filename}/s|${_baseurl}/|files/|g" downloaded_utf8.text

        # fix broken links for images
        sed -i "/${filename}/s|](//|](files/|g" downloaded.text
        sed -i "/${filename}/s|](//|](files/|g" downloaded_utf8.text
        sed -i "/${filename}/s|](/|](files/|g" downloaded.text
        sed -i "/${filename}/s|](/|](files/|g" downloaded_utf8.text
        sed -i "/${filename}/s|files/files/|files/|g" downloaded.text
        sed -i "/${filename}/s|files/files/|files/|g" downloaded_utf8.text

        # just for debug purposes:
        #if [[ "${line_clean}" = *"pyramid2"* ]] ; then
            #read nada
        #fi


    done 3<<< "$( cat "downloaded-original.text" | tr '(' '\n' | tr ')' '\n' | tr '[' '\n' | tr ']' '\n' | grep -Ei "(\.gif|\.jpg|\.jpeg|\.webp|\.png|\.bmp)" )"
    # note: don't use "sort -u" because we may have same file for different places

    # put back any link to the correct path
    sed -i "s|](/|](${baseurl}/|g" downloaded.text
    sed -i "s|](/|](${baseurl}/|g" downloaded_utf8.text

    if el_confirm "\nDo you want to modify the document? useful for remove useless contents" ; then
        if [[ -z "$EDITOR" ]] ; then
            EDITOR="$( which mousepad geany gvim vim | grep -v "not found" | grep "^/" | head -1 )"
        fi

        $EDITOR downloaded.text
    fi


    title="$( head -30 downloaded.text | grep "^% [[:alpha:]]" | head -1 | sed -e 's|^% ||g' )"
    if [[ -z "$title" ]] ; then
        title="Book"
    fi

    el_explain 0 "Converting to different formats:"


    # convert in all the formats !
    for format in epub html pdf docx odt txt
    do
        options=""

        case $format in
            pdf) options="-V geometry:margin=1in" ; ;;
        *) options="" ; ;;
    esac


    # convert

    if ! pandoc downloaded.text -o "${title}.${format}" 2>/dev/null ; then
        if ! pandoc downloaded_utf8.text -o "${title}.${format}" 2>/dev/null ; then
            # logs:
            pandoc downloaded_utf8.text -o "${title}.${format}"
        else
            echo "command used:  pandoc downloaded_utf8.text -o \"${title}.${format}\"" 1>&2
            el_explain 0 "converted to __${format}__"
        fi
    else
        echo "command used:  pandoc downloaded.text -o \"${title}.${format}\"" 1>&2
        el_explain 0 "converted to __${format}__"
    fi

done


mkdir -p files/originals
mv downloaded*text files/originals

echo -e "\n\nDone"
el_explain 0 "\n__Documents generated at__ XX$(pwd)XX\n"



}

#
#  MAIN
#
main "$@"

# vim: set foldmethod=marker :
