Unifying simple & complex resources.
[aquarium] / make-dist.sh
1 #!/usr/bin/env bash
2
3 # For debugging
4 clear
5 # set -x
6 LOG=build.log
7
8 fail() {
9     echo "failed while $1"
10     echo "last log lines are"
11     tail -n 15 $LOG
12     echo "see file $LOG for details"
13     cleanup
14     exit 1
15 }
16
17 cleanup() {
18     git checkout master 2>>$LOG 1>>$LOG
19     if [ -f stashed ]; then
20         git stash pop 2>>$LOG 1>>$LOG
21         rm stashed
22     fi
23 }
24
25 # Check if there are pending changes
26 status=`git status --porcelain|grep -v "??"`
27 if [ ! -z "$status" ]
28 then
29     echo "The following files are not committed:"
30     echo 
31     echo $status
32     echo
33     echo "Stashing them"
34     git stash save 2>&1 >>$LOG
35     touch stashed
36 fi
37
38 # Get version or tag to checkout from cmd-line
39 if [ ! -z $1 ]
40 then
41     echo -n "checking commit $1... "
42     out=`git show $1 2>&1|grep "fatal: ambiguous argument '$1'"`
43     if [ -z "$out" ]
44     then
45         tag=$1
46         echo "success"
47     else
48         fail "retrieving info about commit $1"
49     fi
50 else
51     echo -n "checking out latest tag ("
52     tag=`git tag |tail -n 1`
53     echo "$tag)"
54 fi
55
56 # Tags are marked as aquarium-*
57 if [[ "$tag" =~ "aquarium-" ]];
58 then
59     DIR=$tag
60 else
61     DIR="aquarium-$tag"
62 fi
63
64 # Creating dist dirs
65 mkdir -p $DIR
66 mkdir -p $DIR/bin
67 mkdir -p $DIR/lib
68 mkdir -p $DIR/conf
69 mkdir -p $DIR/logs
70
71 echo "Checking out $tag"
72 git checkout $tag 2>>$LOG 1>>$LOG || fail "checking out"
73
74 echo "Building $tag"
75 mvn clean install -DskipTests=true >>build.log || fail "building project"
76
77 echo "Collecting dependencies"
78 mvn dependency:copy-dependencies >> build.log ||  fail "collecting dependencies"
79 cp target/dependency/*.jar $DIR/lib || fail "copying dependencies"
80
81 echo "Copying Aquarium classes"
82 aquariumjar=`find target -type f|egrep "aquarium-[0-9\.]+(-SNAPSHOT)?\.jar"`
83 cp $aquariumjar $DIR/lib || fail "copying $aquariumjar"
84
85 echo "Copying scripts and config files"
86 cp dist/aquarium.sh $DIR/bin || fail "copying aquarium.sh"
87 cp dist/log4j.properties $DIR/conf|| fail "copying log4j.properties"
88 cp dist/aquarium.properties $DIR/conf || fail "copying aquarium.properties"
89 cp dist/policy.yaml $DIR/conf || fail "copying policy.yaml"
90
91 echo "Creating archive"
92 tar zcvf $DIR.tar.gz $DIR >> build.log 2>&1 || fail "creating archive"
93
94 echo "Cleaning up"
95 rm -Rf $DIR
96 cleanup
97 rm $LOG
98
99 echo "File $tag.tar.gz created succesfully"
100
101 # vim: set sta sts=4 shiftwidth=4 sw=4 et ai :