Build Bazel debian source package using Bazel

--
Change-Id: I8c0b6adf08a4ca64ad41e0454cb30842c133fa22
Reviewed-on: https://bazel-review.googlesource.com/#/c/4161
MOS_MIGRATED_REVID=128465441
diff --git a/scripts/packages/BUILD b/scripts/packages/BUILD
index 44b4e1c..745e0c7 100644
--- a/scripts/packages/BUILD
+++ b/scripts/packages/BUILD
@@ -142,3 +142,47 @@
     package = "bazel",
     version_file = ":version.txt",
 )
+
+filegroup(
+    name = "debian-files",
+    srcs = [
+        "debian/compat",
+        "debian/control",
+        "debian/copyright",
+        "debian/rules",
+    ],
+)
+
+genrule(
+    name = "generate-changelog-file",
+    srcs = [
+        "convert_changelog.py",
+        "//:changelog-file",
+    ],
+    outs = ["changelog"],
+    cmd = "python $(location convert_changelog.py) $(location //:changelog-file) $(location changelog)",
+)
+
+genrule(
+    name = "bazel-debian-src",
+    srcs = [
+        "//:bazel-srcs",
+        ":debian-files",
+        ":changelog",
+    ],
+    outs = [
+        "bazel.dsc",
+        "bazel.tar.gz",
+    ],
+    cmd = """
+        mkdir -p bazel/debian
+        tar -xf $(location //:bazel-srcs) -C ./bazel
+        for f in $(locations :debian-files); do
+          cp $$f ./bazel/debian/
+        done
+        cp $(location :changelog) ./bazel/debian
+        dpkg-source -b ./bazel
+        cp ./bazel_*.dsc $(location bazel.dsc)
+        cp ./bazel_*.tar.gz $(location bazel.tar.gz)
+        """,
+)
diff --git a/scripts/packages/convert_changelog.py b/scripts/packages/convert_changelog.py
new file mode 100644
index 0000000..78edbda
--- /dev/null
+++ b/scripts/packages/convert_changelog.py
@@ -0,0 +1,60 @@
+# pylint: disable=g-bad-file-header
+# Copyright 2015 The Bazel Authors. All rights reserved.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#    http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+"""This tool convert Bazel's changelog file to debian changelog format."""
+
+from datetime import datetime
+import sys
+
+
+def main(input_file, output_file):
+  changelog_out = open(output_file, "w")
+  time_stamp = None
+  with open(input_file, "r") as changelog_in:
+    for line in changelog_in:
+      line = line.strip()
+
+      if line.startswith("```") or line.startswith("Baseline"):
+        continue
+
+      if line.startswith("## Release"):
+        if time_stamp:
+          changelog_out.write(
+              "\n -- The Bazel Authors <bazel-dev@googlegroups.com>  %s\n\n" %
+              time_stamp)
+        parts = line.split(" ")
+        version = parts[2]
+        time_stamp = datetime.strptime(
+            parts[3], "(%Y-%m-%d)").strftime("%a, %d %b %Y %H:%M:%S +0100")
+        changelog_out.write("bazel (%s) unstable; urgency=low\n" % version)
+
+      elif line.startswith("+") or line.startswith("-"):
+        parts = line.split(" ")
+        line = "*" + line[1:]
+        changelog_out.write("  %s\n" % line)
+
+      elif line.endswith(":"):
+        changelog_out.write("\n  %s\n" % line)
+
+      elif line:
+        changelog_out.write("    %s\n" % line)
+
+    if time_stamp:
+      changelog_out.write(
+          "\n -- The Bazel Authors <bazel-dev@googlegroups.com>  %s\n\n" %
+          time_stamp)
+
+
+if __name__ == "__main__":
+  main(sys.argv[1], sys.argv[2])
diff --git a/scripts/packages/debian/compat b/scripts/packages/debian/compat
new file mode 100644
index 0000000..ec63514
--- /dev/null
+++ b/scripts/packages/debian/compat
@@ -0,0 +1 @@
+9
diff --git a/scripts/packages/debian/control b/scripts/packages/debian/control
new file mode 100644
index 0000000..e03e085
--- /dev/null
+++ b/scripts/packages/debian/control
@@ -0,0 +1,20 @@
+Source: bazel
+Section: contrib/devel
+Priority: optional
+Maintainer: The Bazel Authors <bazel-dev@googlegroups.com>
+Build-Depends: dpkg-dev, devscripts
+Standards-Version: 3.9.6
+
+Package: bazel
+Section: contrib/devel
+Priority: optional
+Architecture: amd64
+Depends: java8-jdk | java8-sdk, pkg-config, zip, g++, zlib1g-dev,
+ unzip, bash-completion
+Conflicts: openjdk-9-jdk
+Description: Bazel is a tool that automates software builds and tests.
+ Supported build tasks include running compilers and linkers to produce
+ executable programs and libraries, and assembling deployable packages
+ for Android, iOS and other target environments. Bazel is similar to
+ other tools like Make, Ant, Gradle, Buck, Pants and Maven.
+Homepage: http://bazel.io
diff --git a/scripts/packages/debian/copyright b/scripts/packages/debian/copyright
new file mode 100644
index 0000000..25981ab
--- /dev/null
+++ b/scripts/packages/debian/copyright
@@ -0,0 +1,35 @@
+Format: http://www.debian.org/doc/packaging-manuals/copyright-format/1.0/
+Upstream-Name: bazel
+Source: https://github.com/bazelbuild/bazel
+
+Licensed under the Apache License, Version 2.0 (the "License");
+you may not use this file except in compliance with the License.
+You may obtain a copy of the License at
+
+  http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
+
+Bazel bundled with software with the following license:
+
+License: 3-clause BSD
+License: 3-clause revised BSD
+License: Apache License 2.0
+License: BSD license
+License: BSD-style license
+License: Eclipse Distribution License 1.0
+License: Eclipse Public License
+License: Eclipse Public License Version 1.0
+License: GNU GPL v2 with Classpath exception
+License: GNU GPL v2 with Classpath exception (plus other licenses, see third_party/java/jdk/langtools/LICENSE file).
+License: GNU GPL v2 with Classpath exception, portions MIT
+License: MIT
+License: MIT license
+License: New BSD License
+License: Public Domain
+
+# Please refer to README.md under third_party/ directory to see which license is used by specific part of code.
diff --git a/scripts/packages/debian/rules b/scripts/packages/debian/rules
new file mode 100644
index 0000000..eaf895e
--- /dev/null
+++ b/scripts/packages/debian/rules
@@ -0,0 +1,17 @@
+#!/usr/bin/make -f
+
+%:
+	dh $@
+
+override_dh_strip:
+
+override_dh_auto_build:
+	./compile.sh
+
+override_dh_auto_clean:
+	dh_auto_clean
+	rm -f bazel-*
+	rm -f output
+
+override_dh_install:
+	install -D -m 0755 output/bazel $$(pwd)/debian/bazel/usr/bin/bazel