1#!/usr/bin/env bash
2#
3# Copyright 2019, David Runge
4#
5# This program is free software: you can redistribute it and/or modify
6# it under the terms of the GNU General Public License as published by
7# the Free Software Foundation, either version 3 of the License, or
8# (at your option) any later version.
9#
10# This program is distributed in the hope that it will be useful,
11# but WITHOUT ANY WARRANTY; without even the implied warranty of
12# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13# GNU General Public License for more details.
14#
15# You should have received a copy of the GNU General Public License
16# along with this program.  If not, see <https://www.gnu.org/licenses/>.
17#
18# Creates assets for $upstream in the form of
19# ${package_name}-$version.tar.bz2' and moves the file to the current working
20# directory (aka. $(pwd)).
21# Optionally creates a detached PGP signature for the tarball.
22# Requires a writable /tmp folder.
23
24set -euo pipefail
25
26get_absolute_path() {
27  cd "$(dirname "$0")" && pwd -P
28}
29
30remove_source_dir() {
31  rm -rf "${source_dir:?}/${package_name}"*
32}
33
34checkout_project() {
35  remove_source_dir
36  cd "${source_dir}"
37  git clone "$upstream" --recursive
38  cd "${package_name}"
39  git checkout "${version}"
40}
41
42clean_sources() {
43  cd "${source_dir}/${package_name}"
44  rm -rfv .gitignore \
45          .gitmodules \
46          .clang-format \
47          .travis.yml \
48          .git/ \
49          create_assets.sh
50}
51
52rename_sources() {
53  cd "${source_dir}"
54  mv -v "${package_name}" "${package_name}-${version}"
55}
56
57compress_sources() {
58  cd "${source_dir}"
59  tar -cjvf "${package_name}-${version}.tar.bz2" \
60    "${package_name}-${version}"
61}
62
63move_sources() {
64  cd "${source_dir}"
65  mv -v "${package_name}-${version}.tar.bz2" "${output_dir}/"
66}
67
68sign_sources() {
69  cd "${output_dir}"
70  gpg2 --default-key "${signer}" \
71       --output "${package_name}-${version}.tar.bz2.asc" \
72       --detach-sign "${package_name}-${version}.tar.bz2"
73}
74
75cleanup_source_dir() {
76  cd "${source_dir}"
77  rm -rf "${package_name}-${version}"
78}
79
80print_help() {
81  echo "Usage: $0 -v <version tag> -s <signature email>"
82  exit 1
83}
84
85upstream="https://github.com/flaviotordini/minitube"
86package_name="minitube"
87source_dir="/tmp"
88version=$(date "+%Y-%m-%d")
89signer=""
90output_dir=$(get_absolute_path "$0")
91
92
93if [ ${#@} -gt 0 ]; then
94  while getopts 'hv:s:' flag; do
95    case "${flag}" in
96      h) print_help
97          ;;
98      s) signer=$OPTARG
99          ;;
100      v) version=$OPTARG
101          ;;
102      *)
103        echo "Error! Try '${0} -h'."
104        exit 1
105        ;;
106    esac
107  done
108else
109  print_help
110fi
111
112checkout_project
113clean_sources
114rename_sources
115compress_sources
116move_sources
117if [ -n "${signer}" ]; then
118  sign_sources
119fi
120cleanup_source_dir
121
122cd "${output_dir}"
123gh release create $version -t "" && \
124gh release upload $version "${output_dir}/${package_name}-${version}.tar.bz2" --clobber || exit 1
125
126exit 0
127
128# vim:set ts=2 sw=2 et:
129