summaryrefslogtreecommitdiff
path: root/src/util/shmk.sh
blob: 75e1445a2ff1756c502d19ca9cdc4dfbc7a90fdd (plain)
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
#!/bin/sh
# 
# A tool to build a single executable from a collection of shell scripts

#include colors

PREFIX=${PREFIX:-/usr}

usage () {
    cat << EOF
${BLUE}Available Options:

EOF
}

find_file () {
    for p in $search_path; do 
        for f in $p/$1.sh $p/$1; do 
            $verbose \
                && printf "${LIGHT_BLACK}checking: $f\n" 1>&2

            [ -f "$f" ] && {
                echo "$f" 
                return 0
            }
        done
    done
    return 1
}

parse_lines () {
    included=""
    while IFS= read -r line; do 
        case "$line" in 
            "#include "*)
                file="$(find_file ${line#\#include})"
                case "$included" in 
                    *"$file"*)
                        ;;
                    *)
                        cat $file | parse_lines
                        included="$included $file"
                esac
                ;;
            "#>"*)
                eval ${line#'#>'}
                ;;
            "#!"*)
                printf "%s\n" "$line"
                ;;
            "#"*);;
            *)
                printf "%s\n" "$line"
                ;;
        esac
    done 
}

build_shmk () {
    [ -f "$1" ] || return 1
    [ ! -z "$2" ] && output="$2"
    [ -z "$output" ] && output=$(basename $1) 
    output=./${output%.*}

    $clean && {
        rm -f $output
        exit
    }

    $verbose && echo "building $1 to $output"

    [ ! -d "${output%/*}" ] && mkdir -p "${output%/*}"

    cat $1 \
        | parse_lines > ${output} &&
    [ ! -z ${entry_function} ] && echo "$entry_function" >> ${output} 
    chmod +x ${output}
}

interpret_shmk () {
    . $1
    local cmdlist=""

    DIST="./dist"
    PROGS="$PROGS $(sed -rn "s/^prog_(.*)\s*\(\)\s*\{/\1/p" $1)"
    LIBS="$LIBS $(sed -rn "s/^lib_(.*)\s*\(\)\s*\{/\1/p" $1)"
    CHECKS="$CHECKS $(sed -rn "s/^check_(.*)\s*\(\)\s*\{/\1/p" $1)"

    shift
    [ -z "$1" ] && set -- clean build check install
    while [ ! -z "$1" ]; do 
        case "$1" in
            install)
                for prog in $PROGS; do 
                    prog=$(basename $prog)
                    prog="${prog%.*}"
                    cmdlist="$cmdlist
                    install -Dm755 $DIST/$prog ${DESTDIR}/${PREFIX}/bin/$prog #Install program $prog"
                done
                for lib in $LIBS; do 
                    lib=$(basename $lib)
                    lib="${lib%.*}"
                    cmdlist="$cmdlist
                    install -Dm755 $DIST/$lib ${DESTDIR}/${PREFIX}/lib/$lib.sh #Install library $lib"
                done
                ;;
            uninstall)
                for prog in $PROGS; do 
                    prog=$(basename $prog)
                    prog="${prog%.*}"
                    cmdlist="$cmdlist
                    rm ${DESTDIR}/${PREFIX}/bin/$prog #Uninstall program $prog"
                done
                for lib in $LIBS; do 
                    lib=$(basename $lib)
                    lib="${lib%.*}"
                    cmdlist="$cmdlist
                    rm $DIST/$lib ${DESTDIR}/${PREFIX}/lib/$lib.sh #Uninstall library $lib"
                done
                ;;
            clean)
                [ -d "$DIST" ] && cmdlist="$cmdlist
                rm -r $DIST #Clean"
                ;;
            check)
                for check in $CHECKS; do
                    command -v check_$check > /dev/null 2>&1 && cmdlist="$cmdlist
                    check_$check #Check $check"
                done
                ;;
            *) # build all programs
                search_path="$DIST $search_path"

                for lib in $LIBS; do 
                    name=$(basename $lib)
                    build_cmd=lib_$lib
                    command -v $build_cmd > /dev/null 2>&1 || {
                        build_cmd="build_shmk $lib $DIST/$name"
                    }
                    cmdlist="$cmdlist
                    $build_cmd #Build library $name"
                done

                for prog in $PROGS; do 
                    name=$(basename $prog)
                    build_cmd=prog_$prog
                    command -v $build_cmd > /dev/null 2>&1 || {
                        build_cmd="build_shmk $prog $DIST/$name"
                    }
                    cmdlist="$cmdlist
                    $build_cmd #Build program $name"
                done

                # build all libs
                ;;
        esac
        shift
    done

    local out len i
    $verbose && out=/dev/stdout || out=/dev/null
    len=$(($(echo "$cmdlist" | wc -l)-1))
    i=-1
    echo "$cmdlist" | while read -r cmd; do
        i=$((i+1))
        printf "${LIGHT_BLACK}[${LIGHT_BLUE}%s${LIGHT_BLACK}/${LIGHT_BLUE}%s${LIGHT_BLACK}] ${LIGHT_WHITE}%s${RESET}%s" "$i" "$len" "${cmd#*#}" "$($verbose && printf "\n" || printf "\r")"
        ${cmd%#*} 2>&1 > $out || {
            printf "\n${RED}Error $?\n"
            exit 1
        }
    done
    printf "\n"
}

search_path=".
/usr/lib
/usr/local/lib
/usr/share/shmk
"

verbose=false
clean=false

while getopts ":e:I:o:chv" opt; do
    case "${opt}" in 
        e)
            entry_function="$OPTARG"
            ;;
        o)
            output="$OPTARG"
            ;;
        c)
            clean=true
            ;;
        I)
            search_path="$search_path
            $OPTARG"
            ;;
        v)
            verbose=true
            ;;
        h)
            usage && exit 0
            ;;
    esac
done

shift $((OPTIND-1))

[ -z $1 ] && {
    exit 1
}

echo "$0 $*"
[ -f "$1" ] && shebang="$(head -1 $1)"
case "$shebang" in 
    "#!"*"shmk"*)
        interpret_shmk $@
    ;;
    *)
        build_shmk $@
    ;;
esac