]> Git in Space - ssg/blob - ssg
Add $FILE feature and remove test files
[ssg] / ssg
1 #!/usr/bin/env bash
2
3 set -e
4
5 function usage() {
6 echo "Usage: $0 FILE"
7 exit
8 }
9
10 function error() {
11 echo "$1"
12 exit 1
13 }
14
15 # make sure args are right
16 [[ -z "$1" ]] && usage
17 [[ -n "$2" ]] && usage
18
19 # make sure file exists
20 [[ -d "$1" ]] && error "'$1' is a directory"
21 [[ -r "$1" ]] || error "File '$1' does not exist or cannot be read"
22
23 # get working path and file
24 dir="$(dirname "$1")"
25 cd $dir
26 echo "Working in '$(pwd)'" > /dev/stderr
27 file="$(basename "$1")"
28
29 # load stock functions
30 libdir="${XDG_DATA_HOME:-$HOME/.local/share}/ssg"
31 echo "Loading libraries from '$libdir'" > /dev/stderr
32 for f in $libdir/** ; do
33 [[ -r "$f" ]] && source "$f" && echo "Loaded '$f'" > /dev/stderr
34 done
35
36 # evaluate
37 cat PREFIX "$file" SUFFIX 2>/dev/null | while IFS= read l ; do
38 if [[ "$l" == ';;'* ]] ; then
39 echo "running command '${l:2}'" > /dev/stderr
40 eval "FILE='$file' ${l:2}"
41 else
42 echo "$l"
43 fi
44 done