relocate-sdk.sh 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. #!/bin/sh
  2. if [ "$#" -gt 1 ]; then
  3. echo "Usage: $0 [path]"
  4. echo "Run this script to relocate the buildroot SDK to the current location"
  5. echo "If [path] is given, sets the location to [path] (without moving it)"
  6. exit 1
  7. fi
  8. cd "$(dirname "$(readlink -f "$0")")"
  9. if [ "$#" -eq 1 ]; then
  10. NEWPATH="$1"
  11. else
  12. NEWPATH="${PWD}"
  13. fi
  14. LOCFILE="share/buildroot/sdk-location"
  15. if [ ! -r "${LOCFILE}" ]; then
  16. echo "Previous location of the buildroot SDK not found!"
  17. exit 1
  18. fi
  19. OLDPATH="$(cat "${LOCFILE}")"
  20. if [ "${NEWPATH}" = "${OLDPATH}" ]; then
  21. echo "This buildroot SDK has already been relocated!"
  22. exit 0
  23. fi
  24. # Check if the path substitution does work properly, e.g. a tree
  25. # "/a/b/c" copied into "/a/b/c/a/b/c/" would not be allowed.
  26. newpath="$(sed -e "s|${OLDPATH}|${NEWPATH}|g" "${LOCFILE}")"
  27. if [ "${NEWPATH}" != "${newpath}" ]; then
  28. echo "Something went wrong with substituting the path!"
  29. echo "Please choose another location for your SDK!"
  30. exit 1
  31. fi
  32. echo "Relocating the buildroot SDK from ${OLDPATH} to ${NEWPATH} ..."
  33. # Make sure file uses the right language
  34. export LC_ALL=C
  35. # Replace the old path with the new one in all text files
  36. grep -lr "${OLDPATH}" . | while read -r FILE ; do
  37. if file -b --mime-type "${FILE}" | grep -q '^text/' && [ "${FILE}" != "${LOCFILE}" ]
  38. then
  39. sed -i "s|${OLDPATH}|${NEWPATH}|g" "${FILE}"
  40. fi
  41. done
  42. # At the very end, we update the location file to not break the
  43. # SDK if this script gets interruted.
  44. sed -i "s|${OLDPATH}|${NEWPATH}|g" ${LOCFILE}