1#!/bin/bash
2set -e
3
4stateDir="$1"
5moveArg="$2"
6
7scriptPath="$(cd $(dirname $0) && pwd)"
8supportRoot="$(cd $scriptPath/../../.. && pwd)"
9checkoutRoot="$(cd $supportRoot/../.. && pwd)"
10
11function usage() {
12  echo "usage: $0 <statePath>"
13  echo "Restores build state from <statePath> into the places where the build will look for it"
14  exit 1
15}
16
17if [ "$stateDir" == "" ]; then
18  usage
19fi
20
21move=false
22if [ "$moveArg" == "--move" ]; then
23  move=true
24fi
25
26if [ "$stateDir" != "/dev/null" ]; then
27  stateDir="$(cd $stateDir && pwd)"
28fi
29if [ "$OUT_DIR" == "" ]; then
30  OUT_DIR="$checkoutRoot/out"
31else
32  GRADLE_USER_HOME="$OUT_DIR/.gradle"
33fi
34if [ "$DIST_DIR" == "" ]; then
35  DIST_DIR="$OUT_DIR/dist"
36fi
37if [ "$GRADLE_USER_HOME" == "" ]; then
38  GRADLE_USER_HOME="$(cd ~ && pwd)/.gradle"
39fi
40
41# makes the contents of $2 match the contents of $1
42function copy() {
43  from="$1"
44  to="$2"
45  rm "$to" -rf
46  if [ -e "$from" ]; then
47    mkdir -p "$(dirname $to)"
48    if [ "$move" == "true" ]; then
49      mv "$from" "$to"
50    else
51      cp --preserve=all -rT "$from" "$to"
52    fi
53  else
54    rm "$to" -rf
55  fi
56}
57
58function restoreState() {
59  backupDir="$1"
60  echo "Restoring state from $backupDir"
61  copy "$backupDir/out"              "$OUT_DIR"
62  copy "$backupDir/dist"             "$DIST_DIR"         # might be inside OUT_DIR
63  copy "$backupDir/gradleUserHome"   "$GRADLE_USER_HOME" # might be inside OUT_DIR
64  copy "$backupDir/support/.gradle"  "$supportRoot/.gradle"
65  copy "$backupDir/buildSrc/.gradle" "$supportRoot/buildSrc/.gradle"
66  copy "$backupDir/local.properties" "$supportRoot/local.properties"
67}
68
69restoreState $stateDir
70
71