aboutsummaryrefslogtreecommitdiff
path: root/contrib/bmake/mk/stage-install.sh
blob: 674652d1d48247422755f2cd46c43f80582a9a7e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#!/bin/sh

# NAME:
#	stage-install.sh - wrapper around install
#
# SYNOPSIS:
#	stage-install.sh [variable="value"] "args" "dest"
#
# DESCRIPTION:
#	This script is a wrapper around the normal install(1).
#	Its role is to add '.dirdep' files to the destination.
#	The variables we might use are:
#
#	INSTALL
#		Path to actual install(1), default is
#		$REAL_INSTALL
#
#	OBJDIR
#		Path to the dir where '.dirdep' was generated,
#		default is '.'
#
#	_DIRDEP
#		Path to actual '.dirdep' file, default is
#		$OBJDIR/.dirdep
#
#	The "args" and "dest" are passed as is to install(1), and if a
#	'.dirdep' file exists it will be linked or copied to each
#	"file".dirdep placed in "dest" or "dest".dirdep if it happed
#	to be a file rather than a directory.
#
#	Before we run install(1), we check if "dest" needs to be a
#	directory (more than one file in "args") and create it
#	if necessary.
#
# SEE ALSO:
#	meta.stage.mk
#

# RCSid:
#	$Id: stage-install.sh,v 1.9 2020/08/28 01:04:13 sjg Exp $
#
#	@(#) Copyright (c) 2013-2020, Simon J. Gerraty
#
#	This file is provided in the hope that it will
#	be of use.  There is absolutely NO WARRANTY.
#	Permission to copy, redistribute or otherwise
#	use this file is hereby granted provided that
#	the above copyright notice and this notice are
#	left intact.
#
#	Please send copies of changes and bug-fixes to:
#	sjg@crufty.net
#

INSTALL=${REAL_INSTALL:-install}
OBJDIR=.

while :
do
    case "$1" in
    *=*) eval "$1"; shift;;
    *) break;;
    esac
done

# get last entry from "$@" without side effects
last_entry() {
    while [ $# -gt 8 ]
    do
        shift 8
    done
    eval last=\$$#
    echo $last
}

# mkdir $dest if needed (more than one file)
mkdir_if_needed() {
    (
        lf=
        while [ $# -gt 8 ]
        do
            shift 4
        done
        for f in "$@"
        do
            [ -f $f ] || continue
            [ $f = $dest ] && continue
            if [ -n "$lf" ]; then
                # dest must be a directory
                mkdir -p $dest
                break
            fi
            lf=$f
        done
    )
}

args="$@"
dest=`last_entry "$@"`
case " $args " in
*" -d "*) ;;
*) [ -e $dest ] || mkdir_if_needed "$@";;
esac

# if .dirdep doesn't exist, just run install and be done
_DIRDEP=${_DIRDEP:-$OBJDIR/.dirdep}
[ -s $_DIRDEP ] && EXEC= || EXEC=exec
$EXEC $INSTALL "$@" || exit 1

# from meta.stage.mk
LnCp() {
    rm -f $2 2> /dev/null
    ln $1 $2 2> /dev/null || cp -p $1 $2
}

StageDirdep() {
  t=$1
  if [ -s $t.dirdep ]; then
      cmp -s $_DIRDEP $t.dirdep && return
      echo "ERROR: $t installed by `cat $t.dirdep` not `cat $_DIRDEP`" >&2
      exit 1
  fi
  LnCp $_DIRDEP $t.dirdep || exit 1
}

if [ -f $dest ]; then
    # a file, there can be only one .dirdep needed
    StageDirdep $dest
elif [ -d $dest ]; then
    for f in $args
    do
        test -f $f || continue
        StageDirdep $dest/${f##*/}
    done
fi