OSDN Git Service

minstall: Always remove destination file before (re-)creating it.
[android-x86/external-mesa.git] / bin / minstall
1 #!/bin/sh
2
3
4 # A minimal replacement for 'install' that supports installing symbolic links.
5 # Only a limited number of options are supported:
6 # -d dir          Create a directory
7 # -m mode         Sets a file's mode when installing
8
9
10 # If these commands aren't portable, we'll need some "if (arch)" type stuff
11 SYMLINK="ln -s"
12 MKDIR="mkdir -p"
13 RM="rm -f"
14
15 MODE=""
16
17 if [ "$1" = "-d" ] ; then
18         # make a directory path
19         $MKDIR "$2"
20         exit 0
21 fi
22
23 if [ "$1" = "-m" ] ; then
24         # set file mode
25         MODE=$2
26         shift 2
27 fi
28
29 # install file(s) into destination
30 if [ $# -ge 2 ] ; then
31
32         # Last cmd line arg is the dest dir
33         for FILE in $@ ; do
34                 DEST="$FILE"
35         done
36
37         # Loop over args, moving them to DEST directory
38         I=1
39         for FILE in $@ ; do
40                 if [ $I = $# ] ; then
41                         # stop, don't want to install $DEST into $DEST
42                         exit 0
43                 fi
44
45                 # determine file's type
46                 if [ -h "$FILE" ] ; then
47                         #echo $FILE is a symlink
48                         # Unfortunately, cp -d isn't universal so we have to
49                         # use a work-around.
50
51                         # Use ls -l to find the target that the link points to
52                         LL=`ls -l "$FILE"`
53                         for L in $LL ; do
54                                 TARGET=$L
55                         done
56                         #echo $FILE is a symlink pointing to $TARGET
57
58                         FILE=`basename "$FILE"`
59                         # Go to $DEST and make the link
60                         PWDSAVE="$PWD"
61                         cd "$DEST"        # pushd
62                                 $RM "$FILE"
63                                 $SYMLINK "$TARGET" "$FILE"
64                         cd "$PWDSAVE"     # popd
65
66                 elif [ -f "$FILE" ] ; then
67                         #echo "$FILE" is a regular file
68                         $RM "$DEST/$FILE"
69                         cp "$FILE" "$DEST"
70                         if [ $MODE ] ; then
71                                 FILE=`basename "$FILE"`
72                                 chmod $MODE "$DEST/$FILE"
73                         fi
74                 else
75                         echo "Unknown type of argument: " "$FILE"
76                         exit 1
77                 fi
78
79                 I=`expr $I + 1`
80         done
81
82         exit 0
83 fi
84
85 # If we get here, we didn't find anything to do
86 echo "Usage:"
87 echo "  install -d dir                      Create named directory"
88 echo "  install [-m mode] file [...] dest   Install files in destination"
89