1#!/bin/sh
2
3. ./.config || exit 1
4
5target="$1"
6custom_loc="$2"
7applet_loc="$3"
8
9test "$target" || exit 1
10test "$SED" || SED=sed
11test "$DD" || DD=dd
12
13if [ x"$CONFIG_FEATURE_SH_EMBEDDED_SCRIPTS" != x"y" ]
14then
15	printf '#define NUM_SCRIPTS 0\n' >"$target"
16	exit 0
17fi
18
19# Some people were bitten by their system lacking a (proper) od
20od -v -b </dev/null >/dev/null
21if test $? != 0; then
22	echo 'od tool is not installed or cannot accept "-v -b" options'
23	exit 1
24fi
25
26custom_scripts=""
27if [ -d "$custom_loc" ]
28then
29	custom_scripts=$(cd $custom_loc; ls * 2>/dev/null)
30fi
31all_scripts=$($srctree/applets/busybox.mkscripts)
32
33# all_scripts includes applet scripts and custom scripts, sort them out
34applet_scripts=""
35for i in $all_scripts
36do
37    found=0
38	for j in $custom_scripts
39	do
40		if [ "$i" = "$j" ]
41		then
42			found=1
43			break;
44		fi
45	done
46	if [ $found -eq 0 ]
47	then
48		# anything that isn't a custom script is an applet script
49		applet_scripts="$applet_scripts $i"
50	fi
51done
52
53# we know the custom scripts are present but applet scripts might have
54# become detached from their configuration
55for i in $applet_scripts
56do
57	#if [ ! -f "$applet_loc/$i" -a ! -f "$custom_loc/$i" ]
58	if [ ! -f "$applet_loc/$i" ]
59	then
60		echo "missing applet script $i"
61		exit 1
62	fi
63done
64
65n=$(echo $custom_scripts $applet_scripts | wc -w)
66nall=$(echo $all_scripts | wc -w)
67
68if [ $n -ne $nall ]
69then
70	echo "script mismatch $n != $nall"
71	exit 1
72fi
73
74concatenate_scripts() {
75	for i in $custom_scripts
76	do
77		cat $custom_loc/$i
78		printf '\000'
79	done
80	for i in $applet_scripts
81	do
82		cat $applet_loc/$i
83		printf '\000'
84	done
85}
86
87exec >"$target.$$"
88
89if [ $n -ne 0 ]
90then
91	printf '#ifdef DEFINE_SCRIPT_DATA\n'
92	printf 'const uint16_t applet_numbers[] = {\n'
93	for i in $custom_scripts $applet_scripts
94	do
95		# TODO support applets with names including invalid characters
96		printf '\tAPPLET_NO_%s,\n' $i
97	done
98	printf '};\n'
99	printf '#else\n'
100	printf 'extern const uint16_t applet_numbers[];\n'
101	printf '#endif\n'
102fi
103
104printf "\n"
105printf '#define NUM_SCRIPTS %d\n' $n
106printf "\n"
107
108if [ $n -ne 0 ]
109then
110	printf '#define UNPACKED_SCRIPTS_LENGTH '
111	concatenate_scripts | wc -c
112
113	printf '#define PACKED_SCRIPTS \\\n'
114	concatenate_scripts | bzip2 -1 | $DD bs=2 skip=1 2>/dev/null | \
115	od -v -b \
116	| grep -v '^ ' \
117	| $SED -e 's/^[^ ]*//' \
118		-e 's/ //g' \
119		-e '/^$/d' \
120		-e 's/\(...\)/0\1,/g' \
121		-e 's/$/ \\/'
122	printf '\n'
123fi
124
125mv -- "$target.$$" "$target"
126