#!/usr/bin/bash
#
# I'm new to bash scripting, so give me a break.
# I know that this is probably crap, but it's cheap, dirty, and does the job.

green='\e[32m'
cyan='\e[36m'

src_prefix='src/'

# The "a" is just to make this work if no characters are present in $@
args=$@
args+=('a')

if [ $(expr length "$args") -gt 1 ]
then
    args=$@
else
    args='inflate style copy conv-img'
fi

# Cheap patch for copying in case the paths aren't present.
mkdir -p target

for x in $args
do
    files=()

    target_ext=''
    target_opt=''

    if [ "$x" == 'inflate' ]
    then
        cmd='python balloon.py'

        files=(`
            find src -not\
                \(\
                    \(\
                        -path "src/.well-known/*" -o\
                        -path "src/feed/*" -o\
                        -path "src/img/*" -o\
                        -path "src/includes/*" -o\
                        -path "src/partials/*" -o\
                        -path "src/templates/*"\
                    \)\
                -prune \)\
                -a\
                -type f\
                -name "*.html"\
                -print
        `)

        target_ext='.html'

        action=Inflating...

    elif [ "$x" == 'style' ]
    then
        cmd='sass --update'

        files=(`
            find src -not\
            \(\
                \(\
                    -path "src/.well-known/*" -o\
                    -path "src/feed/*" -o\
                    -path "src/img/*" -o\
                    -path "src/includes/*" -o\
                    -path "src/partials/*" -o\
                    -path "src/templates/*"\
                \)\
            -prune \)\
            -a\
            -type f\
            -name "*.scss"\
            -print
        `)

        target_ext='.css'

        action=Styling...

    elif [ "$x" == 'copy' ]
    then
        cmd='cp -R'

        files=(
            'src/.well-known/security.txt'
            'src/feed/rss.xml'
            'src/robots.txt'
        )

        mkdir -p target/.well-known
        mkdir -p target/feed

        action=Copying...

    elif [ "$x" == 'conv-img' ]
    then
        cmd='cwebp -z 9 -m 6'

        files=(`find src/img -type f`)

        target_ext='.webp'
        target_opt='-o'

        mkdir -p target/img/blog/posts

        action='Converting Images...'

    elif [ "$x" == 'clean' ]
    then
        echo -e "$green"Cleaning...

        rm -Rv target

        break

    else
        echo -e "$green"Usage:"$cyan" build.sh [OPTIONS] [COMMAND]
        echo
        echo -e "$green"Options:"$cyan"
        echo '  -h, --help          Print help'
        echo
        echo
        echo -e "$green"Commands:"$cyan"
        echo '  conv-img            Convert images to target'
        echo '  copy                Copy assets to target'
        echo '  inflate             Inflate the HTML source'
        echo '  style               Compile SCSS to CSS'

        echo
        echo '  clean               Wipe the target directory'

        break
    fi

    echo -e "$green$action"

    for src in "${files[@]}"
    do
        filename=$(basename $src)
        path="${src//$src_prefix}"
        path="${path//$filename}"
        target="target/$path${filename%.*}$target_ext"

        echo -e "$cyan  $src -> $target"

        eval "$cmd $src $target_opt $target"
    done
done

echo -e "$green"Done.
