1set -e
2
3# parse args
4inputFile="$1"
5oldVersion="$2"
6newVersion="$3"
7
8function usage() {
9  echo "re-version-repo.sh takes a .zip of a Maven repo and rewrites the versions inside it to a new version
10
11Usage: re-version-repo.sh <input-zip> <old-version> <new-version>
12"
13  exit 1
14}
15
16if [ "${inputFile}" == "" ]; then
17  usage
18fi
19if [ "${oldVersion}" == "" ]; then
20  usage
21fi
22if [ "${newVersion}" == "" ]; then
23  usage
24fi
25
26# setup
27if stat "${inputFile}" > /dev/null 2>/dev/null; then
28  echo
29else
30  echo "Input file ${inputFile} does not exist"
31  exit 1
32fi
33inputFile="$(readlink -f ${inputFile})"
34
35if echo "${inputFile}" | grep -v "${oldVersion}" >/dev/null; then
36  echo "The text '${oldVersion}' does not appear in the name of the file, ${inputFile}."
37  echo "This is required as a sanity check (and also facilitates computing the output file name)"
38  exit 1
39fi
40outputFile="$(echo ${inputFile} | sed "s/${oldVersion}/${newVersion}/g")"
41
42tempDir="/tmp/repo"
43rm "${tempDir}" -rf
44mkdir -p "${tempDir}"
45cd "${tempDir}"
46
47# unzip dir
48echo
49echo unzipping "${inputFile}"
50unzippedDir="${tempDir}/unzipped"
51unzip -q "${inputFile}" -d "${unzippedDir}"
52cd "${unzippedDir}"
53
54# make new dirs for new files
55echo
56oldDirs="$(find -type d)"
57newDirs="$(echo ${oldDirs} | sed "s/${oldVersion}/${newVersion}/g")"
58echo "Making new dirs: ${newDirs}"
59echo "${newDirs}" | xargs --no-run-if-empty mkdir -p
60
61# move every file
62echo
63echo moving files
64oldFiles="$(find -type f)"
65moveCommand=""
66for oldFile in ${oldFiles}; do
67  if echo "${oldFile}" | grep "${oldVersion}">/dev/null; then
68    newFile="$(echo ${oldFile} | sed "s/${oldVersion}/${newVersion}/g")"
69    echo "moving ${oldFile} -> ${newFile}"
70    mv "${oldFile}" "${newFile}"
71  fi
72done
73
74# remove old dirs
75echo
76obsoleteDirs="$(find -type d | grep ${oldVersion} | grep -v ${newVersion} | sort -r)"
77echo "Removing dirs: ${obsoleteDirs}"
78echo "${obsoleteDirs}" | xargs -n 1 --no-run-if-empty rmdir
79
80# rewrite .pom files
81echo
82echo rewriting poms
83find -name "*.pom" | xargs sed -i "s/${oldVersion}/${newVersion}/g"
84
85# regenerate .md5 and .sha1 files
86for f in $(find -type f | grep -v '\.sha1$' | grep -v '\.md5'); do
87  md5=$(md5sum $f | sed 's/ .*//')
88  sha1=$(sha1sum $f | sed 's/ .*//')
89
90  echo -n $md5 > "${f}.md5"
91  echo -n $sha1 > "${f}.sha1"
92done
93
94
95echo
96echo rezipping
97rm -f "${outputFile}"
98zip -qr "${outputFile}" .
99
100echo "Done transforming ${inputFile} (${oldVersion}) ->  ${outputFile} (${newVersion})"
101