~sircmpwn/wersh

ref: bcc3fbaf8d1e19c262290916967a524cb2111d37 wersh/wersh -rwxr-xr-x 1.7 KiB
bcc3fbafDrew DeVault Initial commit 3 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#!/bin/sh
set -e

function usage() {
    echo "Usage: $0 input/ output/"
    exit 1
}

_single=
_watch=
_n=0
for arg in "$@"
do
    case $arg in
        -s)
            _single=t
            _n=$(($_n + 1))
            ;;
        -w)
            _watch=t
            _n=$(($_n + 1))
            ;;
        -*)
            usage
            ;;
    esac
done
[ $_n -ne 0 ] && shift $_n

function escape() {
    sed -e 's/</\&lt;/g' -e 's/>/\&gt;/g'
}

if [ -n "$_single" ]
then
    [ $# -ne 4 ] && usage
    root=$(readlink -f $1)
    outputdir=$(readlink -f $2)
    output=$3
    _input=$(readlink -f "$4")
    cd $root
    [ -e "$root"/_config.sh ] && . "$root"/_config.sh
    . "$_input"
elif [ -n "$_watch" ]
then
    [ $# -gt 2 ] && usage
    root=${1:-.}
    $0 "$@"
    while inotifywait -qqre close_write "$root"
    do
        sleep 1
        $0 "$@"
    done
else
    [ $# -gt 2 ] && usage
    root=${1:-.}
    _outputdir=${2:-_site}
    if [ ! -d $_outputdir ] && [ -e $_outputdir ]
    then
        echo "$_outputdir exists and is not a directory"
        exit 1
    fi
    find "$root" -type f -name "*.sh" -not -path "*/_*/*" -not -name "_*" | \
    while IFS= read -r _input
    do
        _output=$_outputdir${_input#$root}
        _output=${_output%.sh}
        echo "Generating $_output from $_input"
        mkdir -p $(dirname "$_output")
        $0 -s "$root" "$_outputdir" "$_output" "$_input" > "$_output"
    done
    find "$root" -type f -not -name "*.sh" -not -path "*/_*/*" -not -name "_*" | \
    while IFS= read -r _input
    do
        _output=$_outputdir${_input#$root}
        echo "Copying $_input to $_output"
        mkdir -p $(dirname "$_output")
        cp "$_input" "$_output"
    done
fi