1#!/bin/sh
2
3export LC_ALL=POSIX
4export LC_CTYPE=POSIX
5
6prefix=$1
7if [ -z "$prefix" ]; then
8	echo "usage: applets/install.sh DESTINATION TYPE [OPTS ...]"
9	echo "  TYPE is one of: --symlinks --hardlinks --binaries --scriptwrapper --none"
10	echo "  OPTS is one or more of: --cleanup --noclobber"
11	exit 1
12fi
13shift # Keep only remaining options
14
15# Source the configuration
16. ./.config
17
18h=`sort busybox.links | uniq`
19
20sharedlib_dir="0_lib"
21
22linkopts=""
23scriptwrapper="n"
24binaries="n"
25cleanup="0"
26noclobber="0"
27while [ ${#} -gt 0 ]; do
28	case "$1" in
29		--hardlinks)     linkopts="-f";;
30		--symlinks)      linkopts="-fs";;
31		--binaries)      binaries="y";;
32		--scriptwrapper) scriptwrapper="y"; swrapall="y";;
33		--sw-sh-hard)    scriptwrapper="y"; linkopts="-f";;
34		--sw-sh-sym)     scriptwrapper="y"; linkopts="-fs";;
35		--cleanup)       cleanup="1";;
36		--noclobber)     noclobber="1";;
37		--none)          h="";;
38		*)               echo "Unknown install option: $1"; exit 1;;
39	esac
40	shift
41done
42
43if [ -n "$DO_INSTALL_LIBS" ] && [ x"$DO_INSTALL_LIBS" != x"n" ]; then
44	# get the target dir for the libs
45	# assume it starts with lib
46	libdir=$($CC -print-file-name=libc.so | \
47		 sed -n 's%^.*\(/lib[^\/]*\)/libc.so%\1%p')
48	if test -z "$libdir"; then
49		libdir=/lib
50	fi
51
52	mkdir -p "$prefix/$libdir" || exit 1
53	for i in $DO_INSTALL_LIBS; do
54		rm -f "$prefix/$libdir/$i" || exit 1
55		if [ -f "$i" ]; then
56			echo "   Installing $i to the target at $prefix/$libdir/"
57			cp -pPR "$i" "$prefix/$libdir/" || exit 1
58			chmod 0644 "$prefix/$libdir/`basename $i`" || exit 1
59		fi
60	done
61fi
62
63if [ x"$cleanup" = x"1" ] && [ -e "$prefix/bin/busybox" ]; then
64	inode=`ls -i "$prefix/bin/busybox" | awk '{print $1}'`
65	sub_shell_it=`
66		cd "$prefix"
67		for d in usr/sbin usr/bin sbin bin; do
68			pd=$PWD
69			if [ -d "$d" ]; then
70				cd "$d"
71				ls -iL . | grep "^ *$inode" | awk '{print $2}' | env -i xargs rm -f
72			fi
73			cd "$pd"
74		done
75		`
76	exit 0
77fi
78
79rm -f "$prefix/bin/busybox" || exit 1
80mkdir -p "$prefix/bin" || exit 1
81install -m 755 busybox "$prefix/bin/busybox" || exit 1
82
83for i in $h; do
84	appdir=`dirname "$i"`
85	app=`basename "$i"`
86	if [ x"$noclobber" = x"1" ] && ([ -e "$prefix/$i" ] || [ -h "$prefix/$i" ]); then
87		echo "  $prefix/$i already exists"
88		continue
89	fi
90	mkdir -p "$prefix/$appdir" || exit 1
91	if [ x"$scriptwrapper" = x"y" ]; then
92		if [ x"$swrapall" != x"y" ] && [ x"$i" = x"/bin/sh" ]; then
93			ln $linkopts busybox "$prefix/$i" || exit 1
94		else
95			rm -f "$prefix/$i"
96			echo "#!/bin/busybox" >"$prefix/$i"
97			chmod +x "$prefix/$i"
98		fi
99		echo "	$prefix/$i"
100	elif [ x"$binaries" = x"y" ]; then
101		# Copy the binary over rather
102		if [ -e "$sharedlib_dir/$app" ]; then
103			echo "   Copying $sharedlib_dir/$app to $prefix/$i"
104			cp -pPR "$sharedlib_dir/$app" "$prefix/$i" || exit 1
105		else
106			echo "Error: Could not find $sharedlib_dir/$app"
107			exit 1
108		fi
109	else
110		if [ x"$linkopts" = x"-f" ]; then
111			bb_path="$prefix/bin/busybox"
112		else
113			case "$appdir" in
114			/)
115				bb_path="bin/busybox"
116			;;
117			/bin)
118				bb_path="busybox"
119			;;
120			/sbin)
121				bb_path="../bin/busybox"
122			;;
123			/usr/bin | /usr/sbin)
124				bb_path="../../bin/busybox"
125			;;
126			*)
127				echo "Unknown installation directory: $appdir"
128				exit 1
129			;;
130			esac
131		fi
132		echo "  $prefix/$i -> $bb_path"
133		ln $linkopts "$bb_path" "$prefix/$i" || exit 1
134	fi
135done
136
137exit 0
138