Summary Files Commits Branches @commit @diff @stats
path: root / UniConfX.sh
blob: 0ccb9d06d6dc8039f8a5611cfac2b3ed7d59f729 (plain) (blame)
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
#!/bin/sh

: ${UCX_PATH:=$(pwd)/std}
export UCX_PATH

case "$1" in
	(help)
		echo "UniConfX.sh ACTION FILE"
		exit 0
esac

UCX_FILE="$(realpath $2)"

# 0) Prepare work directory
WORKDIR="/tmp/ucx_$$"
mkdir -p "$WORKDIR/execution/usr/share/bin"
cp Xpath "$WORKDIR/execution/usr/share/bin"
export PATH="$PATH:$WORKDIR/execution/usr/share/bin"
cd "$WORKDIR/execution"

# cp -L /bin/sh /usr/bin/env $(command -v find) $(command -v awk) ./bin
# cp -L $(command -v git) $(command -v git-receive-pack) $(command -v git-shell) $(command -v git-upload-archive) $(command -v git-upload-pack) $(command -v git-http-backend) ./bin
# cp -rL /lib /lib64 .
#
# if command -v ldd >/dev/null 2>&1
# then
# 	while read -r f
# 	do
# 		deps="$(ldd "./$f" 2>/dev/null | awk '$3~/\/lib\//{print$3}')"
# 		[ -n "$deps" ] && cp -Lf $deps ./lib
# 		deps="$(ldd "./$f" 2>/dev/null | awk '$3~/\/lib64\//{print$3}')"
# 		[ -n "$deps" ] && cp -Lf $deps ./lib64
# 	done <<LIBS
# $(find . -type f)
# LIBS
# fi
# if command -v patchelf >/dev/null 2>&1
# then
# 	while read -r f
# 	do
# 		chmod +w $f
# 		patchelf --set-interpreter /lib64/ld-linux-x86-64.so.2 "$f" 2>/dev/null || continue
# 		patchelf --set-rpath /lib:/lib64 "$f" 2>/dev/null || continue
# 	done <<LIBS
# $(find ./bin ./lib -type f)
# LIBS
# fi
if command -v mount >/dev/null 2>&1
then
	mkdir bin
	sudo mount --bind /run/current-system/sw/bin ./bin
	mkdir lib
	sudo mount --bind /lib ./lib
	mkdir lib64
	sudo mount --bind /lib64 ./lib64
	mkdir dev
	sudo mount --bind /dev ./dev
fi

cd "$WORKDIR"
echo $WORKDIR

safe_exit() {
	chmod -R +w ./execution/bin
	rm -rf "$WORKDIR"
	exit "$1"
}

REG_NAME='/^[ \t\v]*name[ \t\v]*:=[ \t\v]*/'
REG_IMPORT='/^[ \t\v]*import[ \t\v]*:=[ \t\v]*/'

# 0) Get name
export SELF=$(awk '
	match($0, '"$REG_NAME"') {
		name = substr($0, RSTART + RLENGTH)
	}
	END {
		print name
	}
' "$UCX_FILE")

# 1) Emplace includes
cp "$UCX_FILE" "$SELF.ucx"
until awk '
		BEGIN {
			status = 0
			while ((getline < ARGV[1]) > 0) {
				if (match($0, '"$REG_IMPORT"')) {
					status = 1
					"Xpath " substr($0, RSTART + RLENGTH) | getline imf
					while ((getline < imf) > 0) {
						if ($0 !~ '"$REG_NAME"')
							print
					}
					close(imf)
				}
				else
					print
			}
			close(ARGV[1])
			exit status
		}
	' "$SELF.ucx" > temp.ucx
do
	mv temp.ucx "$SELF.ucx"
done
rm temp.ucx

# 3) Hash
UCX_HASH="$(cksum "$SELF.ucx" | awk '{print$1}')"

case "$1" in
	(hash)
		echo "$UCX_HASH"
		safe_exit 0
		;;
esac

# 3) Split into components
mkdir -p ./execution/sections/
awk '
	/^#-- / {
		section = substr($0, 5)
		if (outf != "")
			close(outf)
		outf = "./execution/sections/" ENVIRON["SELF"] "_" section ".ucxs"
		next
	}
	outf != "" {
		print > outf
	}
' "$SELF.ucx"

# 4) Execute
sudo chroot ./execution /bin/env -i PATH="$PATH:/usr/share/bin" SELF="$SELF" /bin/sh "/sections/${SELF}_do.ucxs" || exit 1
echo $?

# 5) Install

safe_exit 0