summaryrefslogtreecommitdiff
path: root/doc/manual/dita-ot-copy-exec.sh
blob: f917258feccd8957802b9cdbb352de02e69b8a80 (plain)
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#!/usr/bin/env kmk_ash
# $Id$
## @file
# Helper Script for copying the DITA-OT toolkit from $1 to $2,
# run the following command line, nuke $2.
#
# DITA-OT-1.8.5 updates around 40 files everytime it is started in an manner
# that will cause race conditions when starting multiple dost.jar instances
# concurrently.  So, we work around that by running each job of a unqiue and
# temporary copy.  Extra effort is taken to using hardlinking when possible
# and to reduce the number of files and directory we copy.
#

#
# Copyright (C) 2023 Oracle and/or its affiliates.
#
# This file is part of VirtualBox base platform packages, as
# available from https://www.virtualbox.org.
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation, in version 3 of the
# License.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, see <https://www.gnu.org/licenses>.
#
# SPDX-License-Identifier: GPL-3.0-only
#

#
# Some constants.
#
MY_INSTALL="kmk_install"
MY_MKDIR="kmk_mkdir"
MY_RM="kmk_rm"
MY_SED="kmk_sed"
MY_CP="kmk_cp"

# Debug
MY_DEBUG_INSTALL="-v"
MY_DEBUG_INSTALL=""

MY_DEBUG_MKDIR="-v"
MY_DEBUG_MKDIR=""

MY_DEBUG_RM="-v"
MY_DEBUG_RM=""

#
# Pop off the source and target directories from the input arguments.
# We're not trying to be very userfriendly here, just reasonably safe.
#
if test $# -lt 5; then
    echo "syntax error: too few arguments" 1>&2;
    exit 2;
fi
MY_SRCDIR="$1"
MY_DSTDIR="$2"
if test "$3" != "--"; then
    echo "syntax error: Expected '--' as the 3rd parameter, got: $3" 1>&2;
    exit 2;
fi
shift 3
if ! test -d "${MY_SRCDIR}"; then
    echo "error: Source directory does not exists or is not a directory: ${MY_SRCDIR}" 1>&2;
    exit 1;
fi
if test -e "${MY_DSTDIR}"; then
    echo "error: Destination directory already exists: ${MY_DSTDIR}" 1>&2;
    exit 1;
fi

#
# Helper.
#
CopyTree()
{
    MY_SRCTREE="$1"
    MY_DSTTREE="$2"

    # Gather files and directories.
    cd "${MY_SRCTREE}"
    MY_DIRS=""
    MY_DIRS_PAIRS=""
    MY_FILES=""
    MY_EXEC_FILES=""
    for MY_FILE in *;
    do
        if test "${MY_FILE}" = "*"; then
            # For empty directories we get '*' back. Ignore it.
        elif test -d "${MY_SRCTREE}/${MY_FILE}"; then
            case "${MY_SRCTREE}/${MY_FILE}" in
                *\ *)
                    echo "Unexpected space in dir/subdir: ${MY_SRCTREE}/${MY_FILE}" 1>&2;
                    exit 1
                    ;;
            esac
            MY_DIRS="${MY_DIRS} ${MY_FILE}"
            MY_DIRS_PAIRS="${MY_DIRS_PAIRS} ${MY_SRCTREE}/${MY_FILE} ${MY_DSTTREE}/${MY_FILE}"
        elif test -f "${MY_SRCTREE}/${MY_FILE}"; then
            case "${MY_SRCTREE}/${MY_FILE}" in
                *\ *)
                    echo "Unexpected space in dir/filename: ${MY_SRCTREE}/${MY_FILE}" 1>&2;
                    exit 1
                    ;;

                *org.dita.dost.platform/plugin.properties)
                    ;;
                *_template.xml)
                    MY_FILES="${MY_FILES} ${MY_FILE}"
                    MY_TEMPLATED_FILES="${MY_TEMPLATED_FILES} ${MY_DSTTREE}/${MY_FILE%_template.xml}.xml"
                    ;;
                *_template.xsl)
                    MY_FILES="${MY_FILES} ${MY_FILE}"
                    MY_TEMPLATED_FILES="${MY_TEMPLATED_FILES} ${MY_DSTTREE}/${MY_FILE%_template.xsl}.xsl"
                    ;;
                *)
                    if test -x "${MY_SRCTREE}/${MY_FILE}"; then
                        MY_EXEC_FILES="${MY_EXEC_FILES} ${MY_FILE}"
                    else
                        MY_FILES="${MY_FILES} ${MY_FILE}"
                    fi
                    ;;
            esac
        else
            echo "Unexpected file type: ${MY_SRCTREE}/${MY_FILE}" 1>&2;
            exit 1
        fi
    done

    # Install all the files in one go.
    if test -n "${MY_FILES}"; then
        "${MY_INSTALL}" ${MY_DEBUG_INSTALL} --hard-link-files-when-possible -m644 -- ${MY_FILES} "${MY_DSTTREE}/"
    fi
    if test -n "${MY_EXEC_FILES}"; then
        "${MY_INSTALL}" ${MY_DEBUG_INSTALL} --hard-link-files-when-possible -m755 -- ${MY_EXEC_FILES} "${MY_DSTTREE}/"
    fi

    # Create all subdirs and recurse into each.
    if test -n "${MY_DIRS}"; then
        cd "${MY_DSTTREE}"
        "${MY_MKDIR}" -p ${MY_DEBUG_MKDIR} -- ${MY_DIRS}

        # Now transfer MY_DIRS_PAIRS to the argument list, so they're preseved across recursive calls.
        # (the local command/keyword is busted in current binaries, though fixed in kBuild svn.)
        set -- ${MY_DIRS_PAIRS};
        while test "$#" -ge 2;
        do
            CopyTree "${1}" "${2}"
            shift  2
        done
    fi
}

#
# Copy the files over.
#
set -e
MY_SAVED_CWD=`pwd`
"${MY_MKDIR}" -p ${MY_DEBUG_MKDIR} -- "${MY_DSTDIR}"

# Selected root files.
"${MY_INSTALL}" ${MY_DEBUG_INSTALL} --hard-link-files-when-possible -m644 -- \
    "${MY_SRCDIR}/build_template.xml" \
    "${MY_SRCDIR}/catalog-dita.txt" \
    "${MY_SRCDIR}/catalog-dita_template.xml" \
    "${MY_SRCDIR}/integrator.properties" \
    "${MY_SRCDIR}/integrator.xml" \
    "${MY_DSTDIR}/"

# Selected directory trees.
MY_TEMPLATED_FILES=""
for MY_SUBDIR in css dtd lib plugins resource schema tools xsl;
do
    "${MY_MKDIR}" -p ${MY_DEBUG_MKDIR} -- "${MY_DSTDIR}/${MY_SUBDIR}"
    CopyTree "${MY_SRCDIR}/${MY_SUBDIR}" "${MY_DSTDIR}/${MY_SUBDIR}"
done

# Now remove all files that has a _template.xml/xsl variant, just to ensure
# that if hardlinked the copy in the tree won't be modified.
if test -n "${MY_TEMPLATED_FILES}"; then
    "${MY_RM}" -f ${MY_DEBUG_RM} -- ${MY_TEMPLATED_FILES}
fi

cd "${MY_SAVED_CWD}"
set +e

#
# Execute the command.
#
"$@"
MY_EXITCODE=$?

#
# Cleanup and exit.
#
#"${MY_RM}" -Rf ${MY_DEBUG_RM} -- "${MY_DSTDIR}"
exit ${MY_EXITCODE}