Manual for Pijul
#!/usr/bin/env bash


function make_md {
    echo $1
    mkdir -p $(dirname $1)

    local FIRST_LINE=true
    local SECTION=""
    pijul ${@:2} --help | while read; do
        if [ "$FIRST_LINE" = true ]; then
            echo "# $(echo $REPLY | sed -e "s/-/ /g")" > $1
        elif [[ -z "${REPLY//}" ]]; then
            echo >> $1
        elif [ "$REPLY" = "USAGE:" ] || [ "$REPLY" = "ARGS:" ] || [ "$REPLY" = "FLAGS:" ] || [ "$REPLY" = "OPTIONS:" ] || [ "$REPLY" = "SUBCOMMANDS:" ]; then

            SECTION=$(echo $REPLY | cut -f 1 -d ":")
            echo "## " $(echo $SECTION | awk '{print toupper(substr($0, 1, 1))tolower(substr($0, 2))}') >> $1
            echo >> $1

        elif [ "$SECTION" = "USAGE" ]; then
            echo >> $1
            echo "\`\`\`" >> $1
            echo "$REPLY" >> $1
            echo "\`\`\`" >> $1
            echo >> $1
        elif [[ "x$REPLY" =~ "x            " ]]; then
            echo "$REPLY" | sed -e "s/^ *//" >> $1
        elif [[ "x$REPLY" =~ "x        --" ]]; then
            echo >> $1
            echo "\`$(echo "$REPLY" | sed -e "s/^ *//" | sed -e "s/  .*//g")\`" >> $1
            echo >> $1
            echo "$(echo "$REPLY" | sed -e "s/^ *[^ ].*   *\(.*\)/\1/;t;d")" >> $1

        elif [[ "x$REPLY" =~ "x    " ]]; then
            echo >> $1
            echo "\`$(echo "$REPLY" | sed -e "s/^ *//" | sed -e "s/  .*//g")\`" >> $1
            echo >> $1
            echo "$(echo "$REPLY" | sed -e "s/^ *[^ ].*   *\(.*\)/\1/;t;d")" >> $1
        else
            echo "$REPLY" >> $1
        fi
        FIRST_LINE=false
    done

    dispatch ${1%.*} ${@:2}
}

function dispatch {

    local IS_SUBCOMMAND=false

    pijul help ${@:2} | while read i; do
        if [[ $i = "debug" ]]; then
            true
        elif [ "$IS_SUBCOMMAND" = true ]; then

            CMD=$(echo $i | awk '{print $1}')

            if [ $CMD != 'help' ]; then
               make_md $1/$CMD.md ${@:2} $CMD
            fi
        elif [[ $i = "SUBCOMMANDS:" ]]; then
            IS_SUBCOMMAND=true
        fi
    done
}

dispatch src/reference