]> git.sur5r.net Git - cc65/blob - make/install-sh
Changes from Greg King
[cc65] / make / install-sh
1 #!/bin/sh
2 #
3 # install-sh -- install a program, script, or data-file.
4 #
5 # This isn't a full install-script; it does only what is needed by the cc65
6 # package.  It can install only one file at a time.
7
8 # If the system has an "install" command, then use it; otherwise, emulate it.
9 if type install >/dev/null 2>&1
10 then exec install "$@"
11 fi
12
13 # Don't use ":-" because 4.3BSD and earlier shells don't like it.
14 # Put in absolute paths if you don't have these commands in your PATH;
15 # or, set these upper-case variables in your environment.
16 cpprog="${CPPROG-cp}"
17 mvprog="${MVPROG-mv}"
18 rmprog="${RMPROG-rm}"
19 stripprog="${STRIPPROG-strip}"
20 chmodprog="${CHMODPROG-chmod}"
21
22 instcmd="$cpprog"
23 stripcmd=""
24 chmodcmd=""
25 rmcmd="$rmprog -f"
26 mvcmd="$mvprog"
27 src=""
28 dst=""
29
30 while [ x"$1" != x ]; do
31     case $1 in
32         -c) ;;
33
34         -m) chmodcmd="$chmodprog $2"
35             shift
36             ;;
37
38         -s) stripcmd="$stripprog"
39             ;;
40
41         # The first name is the source; the last name is the destination.
42         *)  if [ x"$src" = x ]
43                 then src="$1"
44                 else dst="$1"
45                 fi
46         esac
47     shift
48     done
49
50 [ x"$src" != x ] || { echo "$0: no input file was named." >&2; exit 1;}
51 [ x"$dst" != x ] || { echo "$0: no destination was named." >&2; exit 1;}
52
53 [ -e "$src" ] || { echo "$0: \"$src\" doesn't exist." >&2; exit 1;}
54
55 # Make a temporary file-name in the proper directory.
56 dsttmp="$dst/#inst.$$#"
57
58 # Append the input filename to the destination directory.
59 dst="$dst"/`basename "$src"`
60
61 # Trap to remove the temporary file if it isn't renamed.
62 trap 'status=$?; $rmcmd "$dsttmp" && exit $status' 0
63 trap '(exit $?); exit' 1 2 3 13 15
64
65 # Copy the source file to the temporary name.
66 $instcmd "$src" "$dsttmp" &&
67
68 if [ x"$stripcmd" != x ]
69     then $stripcmd "$dsttmp"
70     fi &&
71 if [ x"$chmodcmd" != x ]
72     then $chmodcmd "$dsttmp"
73     fi &&
74
75 # Remove an old file (only if the temporary file was created successfully).
76 if [ -f "$dst" ]
77     then $rmcmd "$dst" 2>/dev/null ||
78     { echo "$0: can't remove \"$dst\"" >&2
79       (exit 1); exit
80       }
81     fi &&
82
83 # Rename the temporary file to the real name.
84 $mvcmd "$dsttmp" "$dst" &&
85
86 # The final little trick to pass "correctly" the exit status to exit traps.
87 { (exit 0); exit;}