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