Make rehost script more automated

Rip off http/https automatically, and make sure the permissions end up
right when we are done.

Change-Id: I8f33ba9b2aab89151dcf36d73961985552fd9d50
Signed-off-by: Austin Schuh <austin.linux@gmail.com>
diff --git a/tools/rehost.sh b/tools/rehost.sh
index 3031aba..0f04017 100755
--- a/tools/rehost.sh
+++ b/tools/rehost.sh
@@ -4,18 +4,20 @@
 # we are self hosted everywhere consistently.
 
 # ./rehost.sh github.com/bazelbuild/rules_nodejs/releases/download/4.4.6/rules_nodejs-4.4.6.tar.gz
+set -e
 
 readonly LOCALPATH="$(basename "${1}")"
 
-if echo "${1}" | grep --quiet http; then
-  echo "Argument must not include the scheme (remove the https://)" >&2
-  exit 1
-fi
+# Strip off any http/https prefix to make things easy below.
+STRIPPED_URL="${1#"https://"}"
+STRIPPED_URL="${STRIPPED_URL#"http://"}"
 
-set -e
-curl -L "https://${1}" -o "${LOCALPATH}"
+curl -L "https://${STRIPPED_URL}" -o "${LOCALPATH}"
 
-echo "https://software.frc971.org/Build-Dependencies/${1}"
+echo "https://software.frc971.org/Build-Dependencies/${STRIPPED_URL}"
 
-ssh software.frc971.org mkdir -p "$(dirname "/data/files/frc971/Build-Dependencies/${1}")"
-rsync --progress -v --ignore-existing "${LOCALPATH}" software.frc971.org:"/data/files/frc971/Build-Dependencies/${1}"
+readonly TARGET_DIR="/data/files/frc971/Build-Dependencies"
+
+ssh software.frc971.org mkdir --m=775 -p "$(dirname "${TARGET_DIR}/${STRIPPED_URL}")"
+rsync --progress -v --ignore-existing "${LOCALPATH}" software.frc971.org:"${TARGET_DIR}/${STRIPPED_URL}"
+ssh software.frc971.org "find ${TARGET_DIR}/ -type f ! -user www-data ! -group www-data -exec chmod 444 {} \; && find ${TARGET_DIR}/ -type d ! -user www-data ! -group www-data -exec chmod 775 {} \; && find ${TARGET_DIR}/ -type d ! -user www-data ! -group www-data -exec chown www-data:www-data {} \;"