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