bin/mkws


#!/bin/sh -e
PATH=./bin:$PATH

usage() {
	>&2 printf "usage: %s url [path]\\n" "$(basename "$0")"
	exit 1
}

test $# -lt 1 && usage

srcdir=${2:-.}
sharedir=${MKWSTHEMEDIR:-"$srcdir"/share}
LANG=${LANG:-en_US.UTF-8}
outputdir=public

# Pattern for sed to strip number prefixes from directories
STRIPPATTERN='s/[0-9].-//g'

if ! test -f $outputdir
then
    mkdir -p $outputdir
fi

if ! test -f "$srcdir"/index.upphtml
then
	>&2 printf "no index.upphtml file found\\n"
	exit 1
fi

echo "Making s.css"
pp "$sharedir"/s.uppcss "$1" > $outputdir/s.css

# Original mkws processing loop
for t in "$srcdir"/*.upphtml
do
	echo "Making $(basename "${t%.upphtml}".html)"
	pp "$sharedir"/l.upphtml "$t" "$1" > \
		$outputdir/"$(basename "${t%.upphtml}".html)"
done

folders="articles creative-writings the-linux-rain notes twts pastebin"

# Loop through our listed $folders and process
echo "$folders" | tr ' ' '\n' | while read item;
do
    for t in "$srcdir"/$item/*
    do
        # Test if our item is a directory and therefore nested
        if test -d $t
        then
            # If directory is appended with numbers (for easy ordering), strip it
            # for the output
            STRIPPEDNAME=$(echo $t | sed $STRIPPATTERN)

            if ! test -d $outputdir/$t
            then
                mkdir -p $outputdir/$STRIPPEDNAME
            fi
                
            TEMPLATEFILE=$t/index.upphtml
            
            if ! test -f $t/index.upphtml
            then
                TEMPLATEFILE="$sharedir"/.indexMarkDown.upphtml
            fi

            echo "Making $t"
            
            unset TITLE DESCRIPTION publishedDate

            if test -f $t/meta
            then
                . $t/meta
            fi

            FOLDER=$t
           
            export FOLDER TITLE DESCRIPTION STRIPPATTERN publishedDate

            pp "$sharedir"/l.upphtml $TEMPLATEFILE "$1" > \
                $outputdir/$STRIPPEDNAME/index.html
        fi

        # Test if our item is a file and therefore standalone
        if test -f $t -a $(basename $t) != "meta"
        then
            FOLDER=${t%/*}

            if ! test -d $outputdir/$FOLDER
            then
                mkdir -p $outputdir/$FOLDER
            fi
                
            TEMPLATEFILE=$FOLDER/index.upphtml

            if ! test -f $FOLDER/index.upphtml && test -f $FOLDER/*.md
            then
                TEMPLATEFILE="$sharedir"/.indexMarkDown.upphtml
            fi

            echo "Making $t"

            unset TITLE DESCRIPTION publishedDate

            if test -f $FOLDER/meta 
            then
                . $FOLDER/meta
            fi

            export FOLDER TITLE DESCRIPTION STRIPPATTERN publishedDate

            pp "$sharedir"/l.upphtml $TEMPLATEFILE "$1" > \
                $outputdir/$FOLDER/index.html
        fi
     done
done


echo "Making sitemap.xml"
pp "$sharedir"/sitemap.uppxml "$1" > $outputdir/sitemap.xml

echo "Making rss.xml"
pp "$sharedir"/rss.uppxml "$1" > $outputdir/rss.xml

if test -e ./static/*;
then
    echo "Merging static assets folder..."
    cp -av ./static/* public/
fi