#!/bin/sh -e

ROOT=$PWD

txt2html() {
    printf '<pre>\n'

    # Convert [0-9] to links
    sed -E 's|(.*[a-z].*)\[([0-9].*)\]|\1<a href=#\2>[\2]</a>|g' "$1" |

    # Add span id to sections
    sed -E 's|^\[([0-9].*)\]|<span id=\1><a href=#\1>[\1]</a></span>|' |

    # Make links clickable
    sed -E "s|(http[s]?://[^[:space:]\)]*)|<a href=\1>\1</a>|g"

    printf '</pre>\n'
}

gettitle() {
    unset title
    case "$(sed -n 2p "$1")" in =*=|-*-) title="$(sed -n 1p "$1")"; esac
    file=${1##*/} file=${file%.*}
    printf '%s\n' "${title:-$file}${2:+ | Carbs Linux}"
}

genpages() (
    cd src || return 1
    find . ! -name .git | while read -r file; do

    # If this is a directory, create the directory on the destination and
    # return.
    [ -d "$file" ] && {
        mkdir -p "../docs/${file#./}"
        continue
    }

        # We are treating markdown/plaintext files differently.
        case "$file" in
            *.md|*.txt)
                tohtml "$file" > "../docs/${file%.*}.html"
                cp     "$file"   "../docs/${file%.*}.txt"
            ;;

            # If this is not a markdown/txt file copy as-is.
            *)  cp "$file" "../docs/$file"
        esac
    done
)


tohtml() {
    srcfile=${1#.} srcfile=${srcfile%.*}.txt
    case "${1##*.}" in
        md)  markdown -f footnote -f fencedcode "$1" ;;
        txt) txt2html "$1" ;;
    esac |

    sed '/{{ CONTENT }}/r /dev/stdin' "$ROOT/templates/template.html" |

    sed "s#{{ TITLE }}#$(gettitle "$1" html)#" |

    sed '/{{ CONTENT }}/d' |

    sed "s|{{ SRC }}|$srcfile|"
}

blog_index() {
    cat templates/blog-index > src/blog/index.md
    find src/blog -type f ! -name index.md | sort -r | while read -r post ; do
        postdate="${post##*/}"; postdate="${postdate%%.*}"
        printf '* %s - [%s](%s)\n' \
               "$(date --date="$postdate" +%b\ %d\ %Y)" \
               "$(gettitle "$post")" \
               "${postdate}.html"
    done >> src/blog/index.md
}

site_index() {
    cp index/index.md src/index.md
    :> src/news.md
    i=0
    find index -name '*.news' | sort -r | while read -r news; do
        [ "$(( i += 1 ))" -lt 6 ] && {
            printf '\n'
            cat "$news"
        } >> src/index.md
        [ "$i" -eq 1 ] || printf '\n' >> src/news.md
        cat "$news" >> src/news.md
    done
}


genrss() {
    find "$1" -type f ! -name index.md | sort -r | while read -r post ; do
        postdate="${post##*/}" ; postdate="${postdate%.*}"
        cat <<EOF
<item>
<title>$(gettitle "$post")</title>
<pubDate>$(date --date="$postdate" +%a,\ %d\ %b\ %Y)</pubDate>
<dc:creator>Cem Keylan</dc:creator>
<link>https://carbslinux.org/${3:-blog/$(printf "${post##*/}" | sed 's/.md/.html/')}</link>
<description>$(grep -v '^Title: ' "$post" | markdown -f cdata)</description>
</item>
EOF
    done |

    sed '/{{ CONTENT }}/r /dev/stdin' "$ROOT/templates/rss.xml" |

    sed '/{{ CONTENT }}/d' |

    sed "s|{{ SRC }}|${2:-rss}.xml|" |

    sed "s|{{ DATE }}|$(date -u "+%a %b %d %Y %H:00")|"
}

main() {
    # Recreate docs directory
    rm -rf docs; mkdir -p docs

    # Generate the indexes.
    site_index; blog_index

    # Generate rss feeds
    genrss index news index.html > src/news.xml
    genrss src/blog > src/rss.xml

    # Generate htmldocs
    command -v makeinfo && {
        git submodule update --init --remote -f
        make -s -C texidocs DESTDIR="$PWD/src/docs" htmldocs
    }

    # Generate pages
    genpages
}

main "$@"