#!/usr/local/bin/bash

#
# Steps:
#
#  1. Download corresponding html file for some README.md:
#       curl -s $1
#
#  2. Discard rows where no substring 'user-content-' (github's markup):
#       gawk '/user-content-/ { ...
#
#  3.1 Get last number in each row like ' ... </span></a>sitemap.js</h1'.
#      It's a level of the current header:
#       substr($0, length($0), 1)
#
#  3.2 Get level from 3.1 and insert corresponding number of spaces before '*'
#      (min_level is the smallest level in the document):
#       sprintf("%*s", (level-min_level)*'"$nb_spaces"', "")
#
#  4. Find head's text without tags and insert it inside "* [ ... ]":
#       substr($0, match($0, /">.*<\/h/)+2, RLENGTH-5)
#       gsub(/<[^>]*>/, "", text)
#
#  5. Find anchor of the head (class="anchor") and insert it inside "(...)":
#       anchor = substr($0, match($0, /class="anchor"[^>]*href="[^"]+"/), RLENGTH)
#       substr(anchor, match(anchor, /href="[^"]+"/)+6, RLENGTH-7)
#

gh_toc_version="0.11.0"

gh_user_agent="gh-md-toc v$gh_toc_version"

#
# Download rendered into html README.md by its url.
#
#
gh_toc_load() {
    local gh_url=$1

    if type curl &>/dev/null; then
        curl --user-agent "$gh_user_agent" -s "$gh_url"
    elif type wget &>/dev/null; then
        wget --user-agent="$gh_user_agent" -qO- "$gh_url"
    else
        echo "Please, install 'curl' or 'wget' and try again."
        exit 1
    fi
}

#
# Converts local md file into html by GitHub
#
# -> curl -X POST --data '{"text": "Hello world github/linguist#1 **cool**, and #1!"}' https://api.github.com/markdown
# <p>Hello world github/linguist#1 <strong>cool</strong>, and #1!</p>'"
gh_toc_md2html() {
    local gh_file_md=$1
    local skip_header=$2

    URL=https://api.github.com/markdown/raw

    if [ -n "$GH_TOC_TOKEN" ]; then
        TOKEN=$GH_TOC_TOKEN
    else
        TOKEN_FILE="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/token.txt"
        if [ -f "$TOKEN_FILE" ]; then
            TOKEN="$(cat "$TOKEN_FILE")"
        fi
    fi
    if [ -n "${TOKEN}" ]; then
        AUTHORIZATION="Authorization: token ${TOKEN}"
    fi

    local gh_tmp_file_md=$gh_file_md
    if [ "$skip_header" = "yes" ]; then
        if ggrep -Fxq "<!--te-->" "$gh_src"; then
          # cut everything before the toc
          gh_tmp_file_md=$gh_file_md~~
          gsed '1,/<!--te-->/d' "$gh_file_md" > "$gh_tmp_file_md"
        fi
    fi

    # echo $URL 1>&2
    # append the HTTP status code as the last line of the output, so API
    # errors are detected by status rather than by the rendered markdown
    OUTPUT=$(curl -s \
        --user-agent "$gh_user_agent" \
        --data-binary @"$gh_tmp_file_md" \
        -H "Content-Type:text/plain" \
        -H "$AUTHORIZATION" \
        -w '\n%{http_code}' \
        "$URL")
    local curl_status=$?
    local http_code=${OUTPUT##*$'\n'}
    OUTPUT=${OUTPUT%$'\n'*}

    rm -f "${gh_file_md}~~"

    if [ "$curl_status" != "0" ]; then
        echo "XXNetworkErrorXX"
    elif [ "$http_code" = "401" ]; then
        echo "XXBadCredentialsXX"
    elif [ "$http_code" != "200" ] && [ "$(echo "${OUTPUT}" | gawk '/API rate limit exceeded/')" != "" ]; then
        echo "XXRateLimitXX"
    elif [ "$http_code" != "200" ]; then
        echo "XXApiErrorXX ${http_code}"
    else
        echo "${OUTPUT}"
    fi
}


#
# Prints an error and exits if gh_toc_md2html returned an error marker
#
gh_toc_check_api_error() {
    if [ "$1" == "XXNetworkErrorXX" ]; then
        echo "Parsing local markdown file requires access to github API"
        echo "Please make sure curl is installed and check your network connectivity"
        exit 1
    elif [ "$1" == "XXRateLimitXX" ]; then
        echo "Parsing local markdown file requires access to github API"
        echo "Error: You exceeded the hourly limit. See: https://developer.github.com/v3/#rate-limiting"
        TOKEN_FILE="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/token.txt"
        echo "or place GitHub auth token here: ${TOKEN_FILE}"
        exit 1
    elif [ "$1" == "XXBadCredentialsXX" ]; then
        echo "Parsing local markdown file requires access to github API"
        echo "Error: GitHub API rejected the auth token (HTTP 401 Bad credentials)"
        if [ -n "$GH_TOC_TOKEN" ]; then
            echo "Check the GitHub auth token in the GH_TOC_TOKEN environment variable"
        else
            TOKEN_FILE="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/token.txt"
            echo "Check the GitHub auth token here: ${TOKEN_FILE}"
        fi
        exit 1
    elif [[ "$1" == "XXApiErrorXX "* ]]; then
        echo "Parsing local markdown file requires access to github API"
        echo "Error: GitHub API request failed (HTTP ${1#XXApiErrorXX })"
        exit 1
    fi
}


#
# Is passed string url
#
gh_is_url() {
    case $1 in
        https* | http*)
            echo "yes";;
        *)
            echo "no";;
    esac
}

#
# Prints the path of a new temporary file
#
gh_toc_mktemp() {
    if [ -z "$TMPDIR" ]; then
        TMPDIR="/tmp"
    elif [ -n "$TMPDIR" ] && [ ! -d "$TMPDIR" ]; then
        mkdir -p "$TMPDIR"
    fi
    local gh_tmp_md
    if [ "$(uname -s)" == "OS/390" ]; then
        local timestamp
        timestamp=$(date +%m%d%Y%H%M%S)
        # pid and random part: --stdout - makes two files in the same second
        gh_tmp_md="$TMPDIR/tmp.$timestamp.$$.$RANDOM"
    else
        gh_tmp_md=$(mktemp "$TMPDIR/tmp.XXXXXX")
    fi
    echo "$gh_tmp_md"
}

#
# TOC generator
#
gh_toc(){
    local gh_src=$1
    local gh_src_copy=$1
    local gh_ttl_docs=$2
    local need_replace=$3
    local no_backup=$4
    local no_footer=$5
    local indent=$6
    local skip_header=$7
    local depth=$8
    local numbered=$9
    local bullet=${10}

    if [ "$gh_src" = "" ]; then
        echo "Please, enter URL or local path for a README.md"
        exit 1
    fi


    # Show "TOC" string only if working with one document
    if [ "$gh_ttl_docs" = "1" ]; then

        echo "Table of Contents"
        echo "================="
        echo ""
        gh_src_copy=""

    fi

    if [ "$(gh_is_url "$gh_src")" == "yes" ]; then
        gh_toc_load "$gh_src" | gh_toc_grab "$gh_src_copy" "$indent" "$depth" "$numbered" "$bullet"
        if [ "${PIPESTATUS[0]}" != "0" ]; then
            echo "Could not load remote document."
            echo "Please check your url or network connectivity"
            exit 1
        fi
        if [ "$need_replace" = "yes" ]; then
            echo
            echo "!! '$gh_src' is not a local file"
            echo "!! Can't insert the TOC into it."
            echo
        fi
    else
        local rawhtml
        rawhtml=$(gh_toc_md2html "$gh_src" "$skip_header")
        gh_toc_check_api_error "$rawhtml"
        local toc
        toc=$(echo "$rawhtml" | gh_toc_grab "$gh_src_copy" "$indent" "$depth" "$numbered" "$bullet")
        echo "$toc"
        if [ "$need_replace" = "yes" ]; then
            if ggrep -Fxq "<!--ts-->" "$gh_src" && ggrep -Fxq "<!--te-->" "$gh_src"; then
                echo "Found markers"
            else
                echo "You don't have <!--ts--> or <!--te--> in your file...exiting"
                exit 1
            fi
            local ts="<\!--ts-->"
            local te="<\!--te-->"
            local dt
            dt=$(date +'%F_%H%M%S')
            local ext=".orig.${dt}"
            local toc_path="${gh_src}.toc.${dt}"
            local toc_createdby="<!-- Created by https://github.com/ekalinin/github-markdown-toc -->"
            local toc_footer
            toc_footer="<!-- Added by: $(whoami), at: $(date) -->"
            # http://fahdshariff.blogspot.ru/2012/12/sed-mutli-line-replacement-between-two.html
            # clear old TOC
            gsed -i"${ext}" "/${ts}/,/${te}/{//!d;}" "$gh_src"
            # create toc file
            echo "${toc}" > "${toc_path}"
            if [ "${no_footer}" != "yes" ]; then
                echo -e "\n${toc_createdby}\n${toc_footer}\n" >> "$toc_path"
            fi

            # insert toc file
            if ! gsed --version > /dev/null 2>&1; then
                gsed -i "" "/${ts}/r ${toc_path}" "$gh_src"
            else
                gsed -i "/${ts}/r ${toc_path}" "$gh_src"
            fi
            echo
            if [ "${no_backup}" = "yes" ]; then
                rm "$toc_path" "$gh_src$ext"
            fi
            echo "!! TOC was added into: '$gh_src'"
            if [ -z "${no_backup}" ]; then
                echo "!! Origin version of the file: '${gh_src}${ext}'"
                echo "!! TOC added into a separate file: '${toc_path}'"
        fi
            echo
        fi
    fi
}

#
# Prints the whole document with the new TOC between <!--ts--> and <!--te-->.
# The document is not changed, the errors go to stderr.
#
# $1 - a local path, or "-" for STDIN.
#
gh_toc_stdout(){
    # gh_toc_md2html reads gh_src for --skip-header
    local gh_src=$1
    local no_footer=$2
    local indent=$3
    local skip_header=$4
    local depth=$5
    local numbered=$6
    local bullet=$7

    # global: the trap runs on exit, after this function has returned
    gh_tmp_stdin=""
    gh_tmp_toc=""
    trap 'rm -f ${gh_tmp_stdin:+"$gh_tmp_stdin"} ${gh_tmp_toc:+"$gh_tmp_toc"}' EXIT

    if [ "$gh_src" = "-" ]; then
        gh_tmp_stdin=$(gh_toc_mktemp)
        cat > "$gh_tmp_stdin"
        gh_src=$gh_tmp_stdin
    fi

    if ! ggrep -Fxq "<!--ts-->" "$gh_src" || ! ggrep -Fxq "<!--te-->" "$gh_src"; then
        echo "You don't have <!--ts--> or <!--te--> in your file...exiting" >&2
        exit 1
    fi

    local rawhtml
    rawhtml=$(gh_toc_md2html "$gh_src" "$skip_header")
    gh_toc_check_api_error "$rawhtml" >&2
    local toc
    toc=$(echo "$rawhtml" | gh_toc_grab "" "$indent" "$depth" "$numbered" "$bullet")

    # the same TOC block and gsed expressions as --insert, without -i
    local ts="<\!--ts-->"
    local te="<\!--te-->"
    local toc_createdby="<!-- Created by https://github.com/ekalinin/github-markdown-toc -->"
    local toc_footer
    toc_footer="<!-- Added by: $(whoami), at: $(date) -->"
    gh_tmp_toc=$(gh_toc_mktemp)
    echo "${toc}" > "${gh_tmp_toc}"
    if [ "${no_footer}" != "yes" ]; then
        echo -e "\n${toc_createdby}\n${toc_footer}\n" >> "$gh_tmp_toc"
    fi
    gsed "/${ts}/,/${te}/{//!d;}" "$gh_src" | gsed "/${ts}/r ${gh_tmp_toc}"
}

#
# Grabber of the TOC from rendered html
#
# $1 - a source url of document.
#      It's need if TOC is generated for multiple documents.
# $2 - number of spaces used to indent.
# $3 - max heading level to include. 0 means all levels.
# $4 - numbering type: "list", "outline" or empty for bullets.
# $5 - bullet character: "*", "-", "+" or empty for "*".
#
gh_toc_grab() {

    # the heading's own anchor, not a link in the heading text, see #40
    href_regex="/class=\"anchor\"[^>]*href=\"[^\"]+\"/"
    common_awk_script='
                     if (depth+0 > 0 && level+0 > depth+0) next
                     # remove tags of links, formatting and inline html, see #40
                     gsub(/<[^>]*>/, "", text)
                     # outline number: pop the entries down to the parent,
                     # the last popped one is the previous sibling
                     prev = 0
                     while (top > 0 && lvls[top] >= level+0) { prev = nums[top]; top-- }
                     # a skipped level: GitHub can render the entry as text of
                     # the parent entry, so warn on stderr, see #75
                     if (top > 0 && level+0 > lvls[top] + 1)
                         print (gh_url != "" ? gh_url ": " : "") \
                             "Warning: heading \047" text "\047 (level " level ") skips a level" \
                             " after \047" txts[top] "\047 (level " lvls[top] ")," \
                             " see https://github.com/ekalinin/github-markdown-toc/issues/75" | "cat 1>&2"
                     top++; lvls[top] = level+0; nums[top] = prev + 1; txts[top] = text
                     num = ""
                     for (k = 1; k <= top; k++) num = num nums[k] "."
                     modified_href = ""
                     split(href, chars, "")
                     for (i=1;i <= length(href); i++) {
                         c = chars[i]
                         res = ""
                         if (c == "+") {
                             res = " "
                         } else {
                             if (c == "%") {
                                 res = "\\x"
                             } else {
                                 res = c ""
                             }
                         }
                         modified_href = modified_href res
                    }
                    marker = "* "
                    if (bullet != "") marker = bullet " "
                    prefix = ""
                    if (numbered == "list") marker = "1. "
                    if (numbered == "outline") prefix = num " "
                    # keep the entry, the indent is known only after the last one
                    count++
                    entry_level[count] = level+0
                    entry[count] = marker "[" prefix text "](" gh_url  modified_href ")"
                    if (count == 1 || level+0 < min_level) min_level = level+0
                    '
    # indent relative to the top level of the document, see #30
    common_awk_end='
                END {
                     for (i = 1; i <= count; i++)
                         print sprintf("%*s", (entry_level[i]-min_level)*'"$2"', "") entry[i]
                }'
    if [ "$(uname -s)" == "OS/390" ]; then
        grepcmd="pcregrep -o"
        echoargs=""
        # shellcheck disable=SC2016
        awkscript='{
                     level = substr($0, 3, 1)
                     text = substr($0, match($0, /<\/span><\/a>[^<]*<\/h/)+11, RLENGTH-14)
                     anchor = substr($0, match($0, '$href_regex'), RLENGTH)
                     href = substr(anchor, match(anchor, /href="[^"]+"/)+6, RLENGTH-7)
                     '"$common_awk_script"'
                }'"$common_awk_end"
    else
        grepcmd="ggrep -Eo"
        echoargs="-e"
        # shellcheck disable=SC2016
        awkscript='{
                     level = substr($0, 3, 1)
                     text = substr($0, match($0, /">.*<\/h/)+2, RLENGTH-5)
                     anchor = substr($0, match($0, '$href_regex'), RLENGTH)
                     href = substr(anchor, match(anchor, /href="[^"]+"/)+6, RLENGTH-7)
                     '"$common_awk_script"'
                }'"$common_awk_end"
    fi

    # if closed <h[1-6]> is on the new line, then move it on the prev line
    # for example:
    #   was: The command <code>foo1</code>
    #        </h1>
    #   became: The command <code>foo1</code></h1>
    gsed -e ':a' -e 'N' -e '$!ba' -e 's/\n<\/h/<\/h/g' |

    # Sometimes a line can start with <span>. Fix that.
    gsed -e ':a' -e 'N' -e '$!ba' -e 's/\n<span/<span/g' |

    # find strings that corresponds to template
    $grepcmd '<h[1-6][^>]*class="heading-element".*</a' |

    # remove code tags
    gsed 's/<code>//g' | gsed 's/<\/code>//g' |

    # remove g-emoji
    gsed 's/<g-emoji[^>]*[^<]*<\/g-emoji> //g' |

    # now all rows are like:
    #   <h1 class="heading-element">title</h1><a href="..."><span>..</span></a>
    # format result line
    #   * $0 - whole string
    #   * last element of each row: "</hN" where N in (1,2,3,...)
    echo $echoargs "$(gawk -v "gh_url=$1" -v "depth=$3" -v "numbered=$4" -v "bullet=$5" "$awkscript")"
}

        # perl -lpE 's/(\[[^\]]*\]\()(.*?)(\))/my ($pre, $in, $post)=($1, $2, $3) ; $in =~ s{\+}{ }g; $in =~ s{%}{\\x}g; $pre.$in.$post/ems')"

#
# Returns filename only from full path or url
#
gh_toc_get_filename() {
    echo "${1##*/}"
}

show_version() {
    echo "$gh_toc_version"
    echo
    echo "os:     $(uname -rs)"
    echo "arch:   $(uname -m)"
    echo "kernel: $(uname -i)"
    echo "shell:  $(bash --version)"
    echo
    for tool in curl wget ggrep gawk gsed; do
        printf "%-5s: " $tool
        if type $tool &>/dev/null; then
            $tool --version | head -n 1
        else
            echo "not installed"
        fi
    done
}

show_help() {
    local app_name
    app_name=$(basename "$0")
    echo "GitHub TOC generator ($app_name): $gh_toc_version"
    echo ""
    echo "Usage:"
    echo "  $app_name [options] src [src]   Create TOC for a README file (url or local path)"
    echo "  $app_name -                     Create TOC for markdown from STDIN"
    echo "  $app_name --help                Show help"
    echo "  $app_name --version             Show version"
    echo ""
    echo "Options:"
    echo "  --indent <NUM>      Set indent size. Default: 3."
    echo "  --depth <NUM>       Max heading level to include into TOC. Default: 0 (all levels)."
    echo "  --numbered <TYPE>   Number TOC entries: list (ordered list) or outline (1.1. in text). Default: none."
    echo "  --bullet <CHAR>     Bullet of TOC entries: *, - or +. Default: *."
    echo "  --insert            Insert new TOC into original file. For local files only. Default: false."
    echo "                      See https://github.com/ekalinin/github-markdown-toc/issues/41 for details."
    echo "  --no-backup         Remove backup file. Set --insert as well. Default: false."
    echo "  --hide-footer       Do not write date & author of the last TOC update. Set --insert as well. Default: false."
    echo "  --stdout            Print the whole file with the new TOC to stdout. The file is not changed. Default: false."
    echo "  --skip-header       Hide entry of the topmost headlines. Default: false."
    echo "                      See https://github.com/ekalinin/github-markdown-toc/issues/125 for details."
    echo ""
}

#
# Options handlers
#
gh_toc_app() {
    local need_replace="no"
    local indent=3
    local depth=0
    local numbered=""
    local bullet=""
    local to_stdout=""

    if [ "$1" = '--help' ] || [ $# -eq 0 ] ; then
        show_help
        return
    fi

    if [ "$1" = '--version' ]; then
        show_version
        return
    fi

    if [ "$1" = '--indent' ]; then
        indent="$2"
        shift 2
    fi

    if [ "$1" = '--depth' ]; then
        depth="$2"
        shift 2
    fi

    if [ "$1" = '--numbered' ]; then
        numbered="$2"
        shift 2
    fi

    if [ "$1" = '--bullet' ]; then
        bullet="$2"
        shift 2
    fi

    case $numbered in
        "" | list | outline) ;;
        *)
            echo "Unknown type for --numbered: '$numbered'. Use 'list' or 'outline'."
            exit 1;;
    esac

    # "*" is quoted to match only the asterisk itself
    case $bullet in
        "" | "*" | - | +) ;;
        *)
            echo "Unknown bullet for --bullet: '$bullet'. Use '*', '-' or '+'."
            exit 1;;
    esac

    if [ "$numbered" = "list" ] && [ "$indent" -lt 3 ] 2>/dev/null; then
        echo "--numbered list requires --indent 3 or more, got '$indent'."
        exit 1
    fi

    if [ "$numbered" = "list" ] && [ -n "$bullet" ]; then
        echo "--bullet cannot be used with --numbered list."
        exit 1
    fi

    if [ "$1" = "-" ]; then
        local gh_tmp_md
        gh_tmp_md=$(gh_toc_mktemp)
        cat > "$gh_tmp_md"
        local rawhtml
        rawhtml=$(gh_toc_md2html "$gh_tmp_md")
        gh_toc_check_api_error "$rawhtml"
        echo "$rawhtml" | gh_toc_grab "" "$indent" "$depth" "$numbered" "$bullet"
        return
    fi

    if [ "$1" = '--insert' ]; then
        need_replace="yes"
        shift
    fi

    if [ "$1" = '--no-backup' ]; then
        need_replace="yes"
        no_backup="yes"
        shift
    fi

    if [ "$1" = '--hide-footer' ]; then
        need_replace="yes"
        no_footer="yes"
        shift
    fi

    if [ "$1" = '--stdout' ]; then
        to_stdout="yes"
        shift
    fi

    if [ "$1" = '--skip-header' ]; then
        skip_header="yes"
        shift
    fi

    if [ "$to_stdout" = "yes" ]; then
        if [ $# -ne 1 ] || [ "$(gh_is_url "$1")" == "yes" ]; then
            echo "--stdout works with one local file or '-' (STDIN) only." >&2
            exit 1
        fi
        gh_toc_stdout "$1" "$no_footer" "$indent" "$skip_header" "$depth" "$numbered" "$bullet"
        return
    fi


    for md in "$@"
    do
        echo ""
        gh_toc "$md" "$#" "$need_replace" "$no_backup" "$no_footer" "$indent" "$skip_header" "$depth" "$numbered" "$bullet"
    done

    echo ""
    echo "<!-- Created by https://github.com/ekalinin/github-markdown-toc -->"
}

#
# Entry point
#
gh_toc_app "$@"
