relocate-sdk.sh 1.4 KB

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