1#!/bin/bash -e
2
3# Copyright (c) 2012 The Chromium Authors. All rights reserved.
4# Use of this source code is governed by a BSD-style license that can be
5# found in the LICENSE file.
6
7SCRIPTDIR="$(dirname "$(readlink -f "$0")")"
8PACKAGE="chrome-remote-desktop"
9ARCHITECTURE=$(dpkg-architecture | awk -F '=' '/DEB_BUILD_ARCH=/{print $2}')
10REPOCONFIG="deb http://dl.google.com/linux/chrome-remote-desktop/deb/ stable main"
11
12source ${SCRIPTDIR}/../../../../chrome/installer/linux/common/installer.include
13
14guess_filename() {
15  VERSION_FULL=$(get_version_full)
16  echo ${PACKAGE}_${VERSION_FULL}_${ARCHITECTURE}.deb
17}
18
19get_version_full() {
20  src_root=${src_root:-./../../../..}
21  remoting_version_path=$src_root/remoting/VERSION
22  chrome_version_path=$src_root/chrome/VERSION
23  version_helper=$src_root/build/util/version.py
24
25  # TODO(lambroslambrou): Refactor to share the logic with remoting.gyp.
26  version_major=$($version_helper -f $chrome_version_path \
27                  -f $remoting_version_path -t "@MAJOR@")
28  version_minor=$($version_helper -f $remoting_version_path \
29                  -t "@REMOTING_PATCH@")
30  version_build=$($version_helper -f $chrome_version_path \
31                  -f $remoting_version_path -t "@BUILD@")
32  version_patch=$($version_helper -f $chrome_version_path \
33                  -f $remoting_version_path -t "@PATCH@")
34  version_full="$version_major.$version_minor.$version_build.$version_patch"
35  echo $version_full
36}
37
38usage() {
39  echo "usage: $(basename $0) [-hp] [-o path] [-s path]"
40  echo "-h     this help message"
41  echo "-p     just print the expected DEB filename that this will build."
42  echo "-s     path to the top of the src tree."
43  echo "-o     path to write the DEB file to."
44}
45
46while getopts ":s:o:ph" OPTNAME
47do
48  case $OPTNAME in
49    s )
50      src_root="$(readlink -f "$OPTARG")"
51      ;;
52    o )
53      OUTPUT_PATH="$(readlink -f "$OPTARG")"
54      ;;
55    p )
56      PRINTDEBNAME=1
57      ;;
58    h )
59      usage
60      exit 0
61      ;;
62    \: )
63      echo "'-$OPTARG' needs an argument."
64      usage
65      exit 1
66      ;;
67    * )
68      echo "invalid command-line option: $OPTARG"
69      usage
70      exit 1
71      ;;
72  esac
73done
74shift $(($OPTIND - 1))
75
76# This just prints the expected package filename, then exits. It's needed so the
77# gyp packaging target can track the output file, to know whether or not it
78# needs to be built/rebuilt.
79if [[ -n "$PRINTDEBNAME" ]]; then
80  guess_filename
81  exit 0
82fi
83
84# TODO: Make this all happen in a temp dir to keep intermediate files out of the
85# build tree?
86cd "$SCRIPTDIR"
87
88if [[ -z "$version_full" ]]; then
89  version_full=$(get_version_full)
90fi
91
92if [[ ! "$version_full" =~ ^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
93  echo "Error: Invalid \$version_full value: $version_full" >&2
94  exit 1
95fi
96
97# Include revision information in changelog when building from a local
98# git-based checkout.
99merge_head="$(git merge-base HEAD origin/git-svn 2>/dev/null || true)"
100if [[ -n "$merge_head" ]]; then
101  revision="$(git svn find-rev "$merge_head" 2>/dev/null || true)"
102else
103  # Official builders still use svn-based builds.
104  revision="$(svn info . | awk '/^Revision: /{print $2}')"
105fi
106if [[ -n "$revision" ]]; then
107  revision_text="(r$revision)"
108fi
109
110echo "Building version $version_full $revision_text"
111
112# Create a fresh debian/changelog.
113export DEBEMAIL="The Chromium Authors <chromium-dev@chromium.org>"
114rm -f debian/changelog
115debchange --create \
116  --package "$PACKAGE" \
117  --newversion "$version_full" \
118  --force-distribution \
119  --distribution unstable \
120  "New Debian package $revision_text"
121
122
123CRON_SCRIPT_DIR="${SCRIPTDIR}/../../../../out/Release/remoting/installer/cron"
124mkdir -p ${CRON_SCRIPT_DIR}
125process_template \
126    "${SCRIPTDIR}/../../../../chrome/installer/linux/common/repo.cron" \
127    "${CRON_SCRIPT_DIR}/chrome-remote-desktop"
128
129# TODO(mmoss): This is a workaround for a problem where dpkg-shlibdeps was
130# resolving deps using some of our build output shlibs (i.e.
131# out/Release/lib.target/libfreetype.so.6), and was then failing with:
132#   dpkg-shlibdeps: error: no dependency information found for ...
133# It's not clear if we ever want to look in LD_LIBRARY_PATH to resolve deps,
134# but it seems that we don't currently, so this is the most expediant fix.
135SAVE_LDLP=$LD_LIBRARY_PATH
136unset LD_LIBRARY_PATH
137dpkg-buildpackage -b -us -uc
138LD_LIBRARY_PATH=$SAVE_LDLP
139
140if [[ "$OUTPUT_PATH" ]]; then
141  mv ../${PACKAGE}_*.deb "$OUTPUT_PATH"/
142  mv ../${PACKAGE}_*.changes "$OUTPUT_PATH"/
143fi
144