kumquat-buildroot/support/misc/relocate-sdk.sh
Gleb Mazovetskiy dd8f8f8e89 support/misc/relocate-sdk.sh: allow relocating to any directory
Currently, relocate-sdk.sh must be run _after_ relocating the SDK. There
are cases where it is useful to already prepare the SDK _before_
relocating. For example, it allows to prepare a tarball that the user
has to extract to a specific, pre-defined location and nothing more than
that, which is simpler for the user than requiring the script to be run.
In addition, it hides the build directory that was used by the SDK
builder (somewhat).

Add an optional argument to relocate-sdk.sh that gives the target
directory.

Signed-off-by: Gleb Mazovetskiy <glex.spb@gmail.com>
Signed-off-by: Arnout Vandecappelle (Essensium/Mind) <arnout@mind.be>
2022-01-06 22:27:56 +01:00

53 lines
1.5 KiB
Bash
Executable File

#!/bin/sh
if [ "$#" -gt 1 ]; then
echo "Usage: $0 [path]"
echo "Run this script to relocate the buildroot SDK to the current location"
echo "If [path] is given, sets the location to [path] (without moving it)"
exit 1
fi
cd "$(dirname "$(readlink -f "$0")")"
if [ "$#" -eq 1 ]; then
NEWPATH="$1"
else
NEWPATH="${PWD}"
fi
LOCFILE="share/buildroot/sdk-location"
if [ ! -r "${LOCFILE}" ]; then
echo "Previous location of the buildroot SDK not found!"
exit 1
fi
OLDPATH="$(cat "${LOCFILE}")"
if [ "${NEWPATH}" = "${OLDPATH}" ]; then
echo "This buildroot SDK has already been relocated!"
exit 0
fi
# Check if the path substitution does work properly, e.g. a tree
# "/a/b/c" copied into "/a/b/c/a/b/c/" would not be allowed.
newpath="$(sed -e "s|${OLDPATH}|${NEWPATH}|g" "${LOCFILE}")"
if [ "${NEWPATH}" != "${newpath}" ]; then
echo "Something went wrong with substituting the path!"
echo "Please choose another location for your SDK!"
exit 1
fi
echo "Relocating the buildroot SDK from ${OLDPATH} to ${NEWPATH} ..."
# Make sure file uses the right language
export LC_ALL=C
# Replace the old path with the new one in all text files
grep -lr "${OLDPATH}" . | while read -r FILE ; do
if file -b --mime-type "${FILE}" | grep -q '^text/' && [ "${FILE}" != "${LOCFILE}" ]
then
sed -i "s|${OLDPATH}|${NEWPATH}|g" "${FILE}"
fi
done
# At the very end, we update the location file to not break the
# SDK if this script gets interruted.
sed -i "s|${OLDPATH}|${NEWPATH}|g" ${LOCFILE}