about summary refs log tree commit diff stats
path: root/shin
blob: ddd9f0e093875b440554f7c470c139cee2673b06 (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
#!/bin/sh
# SHIN --- include files in shell scripts
# Copyright (C) Case Duckworth <acdw@acdw.net>

_shin_check_SHINPATH() {
	printf "%s\n" "${SHINPATH:-.}" |
		tr : '\n' |
		while read -r path
		do
			if test -f "$path/$1"
			then
				printf '%s\n' "$path/$1"
				return 0
			fi
		done
	return 1
}

_shin_resolve_file() {
	f=
	case "$1" in
		~/*) f="$HOME/${1#~/}" ;;
		/*) f="/${1#/}" ;;
		*) f="$(_shin_check_SHINPATH "$1")" ;;
	esac

	if test -f "$f"
	then
		printf '%s\n' "$f"
	elif test -f "$f.sh"
	then
		printf '%s\n' "$f.sh"
	else
		return 1
	fi
	return 0
}

_shin_include() { # _shin_include FILE ...
	for lib
	do
		lib="$(_shin_resolve_file "$lib")"
		test -f "$lib" && . "$lib"
	done
}

_shin_cleanup() {
	if "$_shin_rmtemp" && test -f "$_shin_temp"
	then
		rm -f "$_shin_temp"
	fi
}

_shin_build() ( # _shin_build FILE ...
	for file
	do
		file="$(_shin_resolve_file "$file")"

		# Determine output file
		if test -z "$_shin_output"
		then
			case "$file" in
				# file.shin -> file.sh
				*.shin) _shin_output="${file%in}" ;;
				# file.sh -> file
				*.sh) _shin_output="${file%.sh}" ;;
				# file.ext -> file.ext.sh
				*) _shin_output="$file.sh" ;;
			esac
		fi

		while read -r line
		do
			case "$line" in
				# skip lines where the user sources the library
				.*shin) ;;
				.*shin.sh) ;;
				# where the user invokes shin, replace with file
				shin*) # cursed
					eval \
						for file in ${line#shin}\; \
						do \
						test -f \"\$file\" \&\& \
						sed \"/^#!/d\" \"\$file\"\; \
						done
					;;
				# else, print the line
				*) printf '%s\n' "$line" ;;
			esac
		done < "$file" > "$_shin_temp"

		ec=$?

		if test "x$_shin_output" = x-
		then # already output to standard out
			cat "$_shin_temp"
			continue
		elif test $ec -ne 0
		then # uh oh we messed up
			echo >&2 "shin: errors building."
			echo >&2 "shin: part-built file: $_shin_temp"
			_shin_rmtemp=false
		elif test -f "$_shin_output" && ! "$_shin_force"
		then # can't overwrite sumthin without -f
			echo >&2 "shin: file already exists: $_shin_output"
			echo >&2 "shin: part-built file: $_shin_temp"
			_shin_rmtemp=false
		else # move the temp file to the output
			if mv "$_shin_temp" "$_shin_output"
			then
				printf >&2 '%s\n' "$_shin_output"
			else
				echo >&2 "shin: errors moving $_shin_output"
				echo >&2 "shin: part-built file: $_shin_temp"
				_shin_rmtemp=false
			fi
		fi
	done
)

shin() {
	_shin_func="${_shin_func:-_shin_include}"
	_shin_temp=/tmp/shinf
	_shin_rmtemp=true
	_shin_force=false
	_shin_debug=false

	trap _shin_cleanup INT EXIT QUIT

	while getopts dfcio: OPT
	do
		case "$OPT" in
			c) _shin_func=_shin_build ;;
			i) _shin_func=_shin_include ;;
			o) # output file implies build.
				_shin_func=_shin_build
				_shin_output="$OPTARG"
				echo "$_shin_output"
				;;
			f) _shin_force=true ;;
			d) _shin_debug=true ;;
			*) exit 1 ;;
		esac
	done
	shift $((OPTIND - 1))
	OPTIND=0

	if "$_shin_debug"
	then
		cat >&2 <<EOF
SHIN DEBUGGING: ON
VARIABLES:
	_shin_func	$_shin_func
	_shin_output	$_shin_output
	_shin_temp	$_shin_temp

	_shin_rmtemp	$_shin_rmtemp
	_shin_force	$_shin_force
EOF
		set -x
	fi

	"$_shin_func" "$@"
}