~m-grant-prg/bintray-debian-upload/focal

« back to all changes in this revision

Viewing changes to src/prg/bash/bin-deb-upl.sh.in

  • Committer: Mark Grant
  • Date: 2018-10-16 12:33:34 UTC
  • Revision ID: m.grant.prg@gmail.com-20181016123334-na7bmrd75wch9dv4
Tags: upstream-1.0.1
ImportĀ upstreamĀ versionĀ 1.0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#! /usr/bin/env bash
 
2
#########################################################################
 
3
#                                                                       #
 
4
#       bin-deb-upl.sh is automatically generated,                      #
 
5
#               please do not modify!                                   #
 
6
#                                                                       #
 
7
#########################################################################
 
8
 
 
9
#########################################################################
 
10
#                                                                       #
 
11
# Script ID: bin-deb-upl.sh                                             #
 
12
# Author: Copyright (C) 2018  Mark Grant                                #
 
13
#                                                                       #
 
14
# Released under the GPLv3 only.                                        #
 
15
# SPDX-License-Identifier: GPL-3.0                                      #
 
16
#                                                                       #
 
17
# Purpose:                                                              #
 
18
# To upload debian packages to bintray.                                 #
 
19
#                                                                       #
 
20
# Syntax:       bin-deb-upl.sh [ -h --help || -v --verbose ||           #
 
21
#                               -V --version ]                          #
 
22
#                                                                       #
 
23
# Exit Codes:   0 - success                                             #
 
24
#               1 - failure                                             #
 
25
#                                                                       #
 
26
# Further Info:                                                         #
 
27
# This script looks for debian packages in the current directory,       #
 
28
# parses their names for package name, version and architecture, and    #
 
29
# then uploads the package to Bintray.                                  #
 
30
#                                                                       #
 
31
#########################################################################
 
32
 
 
33
#########################################################################
 
34
#                                                                       #
 
35
# Changelog                                                             #
 
36
#                                                                       #
 
37
# Date          Author  Version Description                             #
 
38
#                                                                       #
 
39
# 15/10/2018    MG      1.0.1   First release.                          #
 
40
#                                                                       #
 
41
#########################################################################
 
42
 
 
43
##################
 
44
# Init variables #
 
45
##################
 
46
script_exit_code=0
 
47
outputprefix="bin-deb-upl.sh: "
 
48
version="1.0.1"                         # set version variable
 
49
etclocation=@sysconfdir@                # Path to etc directory
 
50
packageversion=@pkgversion@             # Version of the complete package
 
51
verbose=FALSE
 
52
 
 
53
#############
 
54
# Functions #
 
55
#############
 
56
 
 
57
# Output $1 to stdout or stderr depending on $2.
 
58
# Syntax = output $1 (The message) $2 (0 = stdout, > 0 = stderr)
 
59
output()
 
60
{
 
61
        if [ $2 = 0 ]; then
 
62
                echo "$outputprefix$1"
 
63
        else
 
64
                echo "$outputprefix$1" 1>&2
 
65
        fi
 
66
}
 
67
 
 
68
# Standard function to test command error ($1 is $?) and exit if non-zero
 
69
std_cmd_err_handler()
 
70
{
 
71
        if [ $1 != 0 ]; then
 
72
                script_exit_code=$1
 
73
                script_exit
 
74
        fi
 
75
}
 
76
 
 
77
# Standard function to tidy up and return exit code
 
78
script_exit()
 
79
{
 
80
        exit $script_exit_code
 
81
}
 
82
 
 
83
# Standard trap exit function
 
84
trap_exit()
 
85
{
 
86
        script_exit_code=1
 
87
        output "Script terminating due to trap received." $script_exit_code
 
88
        script_exit
 
89
}
 
90
 
 
91
# Setup trap
 
92
trap trap_exit SIGHUP SIGINT SIGTERM
 
93
 
 
94
 
 
95
# Create ~/.bintray.conf
 
96
create_bintray_conf()
 
97
{
 
98
        echo "Creating ~/.bintray.conf, please enter the requested information"
 
99
        read -p "Bintray user name: " binusr
 
100
        read -p "Bintray API Key: " binapikey
 
101
        read -p "Bintray repository URL: " binrepourl
 
102
        echo "binusr=$binusr" >> ~/.bintray.conf
 
103
        std_cmd_err_handler $?
 
104
        echo "binapikey=$binapikey" >> ~/.bintray.conf
 
105
        std_cmd_err_handler $?
 
106
        echo "binrepourl=$binrepourl" >> ~/.bintray.conf
 
107
        std_cmd_err_handler $?
 
108
        # Ensure there is a new line
 
109
        echo "" >> ~/.bintray.conf
 
110
        std_cmd_err_handler $?
 
111
        echo "~/.bintray.conf successfully created."
 
112
}
 
113
 
 
114
 
 
115
# Read ~/.bintray.conf
 
116
# Returns 0 on success, 1 on failure.
 
117
read_bintray_conf()
 
118
{
 
119
        local input=()
 
120
        local oldIFS=$IFS
 
121
        IFS="="
 
122
 
 
123
        exec 3<~/.bintray.conf
 
124
        while read -u3 -ra input
 
125
        do
 
126
                # Ignore comment lines and blank lines.
 
127
                if [[ ${input[0]} = "#"* || ${input[0]} = "" ]]; then
 
128
                        continue
 
129
                fi
 
130
 
 
131
                case ${input[0]} in
 
132
                binusr)
 
133
                        binusr=${input[1]}
 
134
                        ;;
 
135
                binapikey)
 
136
                        binapikey=${input[1]}
 
137
                        ;;
 
138
                binrepourl)
 
139
                        binrepourl=${input[1]}
 
140
                        ;;
 
141
                *)
 
142
                        output "Unrecognized field in ~/.bintray.conf" 1
 
143
                        return 1
 
144
                        ;;
 
145
                esac
 
146
        done
 
147
        exec 3<&-
 
148
 
 
149
        IFS=$oldIFS
 
150
        return 0
 
151
}
 
152
 
 
153
 
 
154
# Lookup ditribution name using distribution number.
 
155
# Returns 0 on success, 1 on failure.
 
156
retrieve_dist_name()
 
157
{
 
158
        local input=()
 
159
        local oldIFS=$IFS
 
160
        IFS="="
 
161
        debdistname=""
 
162
 
 
163
        exec 3<$etclocation/bintray-debian-upload.conf
 
164
        while read -u3 -ra input
 
165
        do
 
166
                # Ignore comment lines and blank lines.
 
167
                if [[ ${input[0]} = "#"* || ${input[0]} = "" ]]; then
 
168
                        continue
 
169
                fi
 
170
 
 
171
                if [[ ${input[0]} = $debdistnum ]]; then
 
172
                        debdistname=${input[1]}
 
173
                        break
 
174
                fi
 
175
        done
 
176
        exec 3<&-
 
177
 
 
178
        IFS=$oldIFS
 
179
 
 
180
        if [[ $debdistname = "" ]]; then
 
181
                output "Unrecognized Debian distribution number." 1
 
182
                return 1
 
183
        fi
 
184
        return 0
 
185
}
 
186
 
 
187
# Parse the filename to get package name, version string, architecture and
 
188
# debian distribution number.
 
189
# Returns 0 on success, 1 on failure.
 
190
parse_filename()
 
191
{
 
192
        local filevar=${1%".deb"}
 
193
        local elements=$(echo $filevar | tr "_" " ")
 
194
        local count=0
 
195
        local err=0
 
196
        local el
 
197
        pkgname=""
 
198
        pkgver=""
 
199
        debdistnum=""
 
200
        pkgarch=""
 
201
 
 
202
        for el in $elements
 
203
        do
 
204
                case "$count" in
 
205
                0)
 
206
                        pkgname=$el
 
207
                        ;;
 
208
                1)
 
209
                        pkgver=$el
 
210
                        debdistnum=${el##*b}
 
211
                        debdistnum=${debdistnum%u*}
 
212
                        ;;
 
213
                2)
 
214
                        pkgarch=$el
 
215
                        ;;
 
216
                *)
 
217
                        err=1
 
218
                        ;;
 
219
                esac
 
220
                count=$((count + 1))
 
221
        done
 
222
        if [[ $pkgname == "" || $pkgver == "" || $debdistnum == "" \
 
223
                || $pkgarch == "" || $err == 1 ]]; then
 
224
                return 1
 
225
        fi
 
226
        return 0
 
227
}
 
228
 
 
229
 
 
230
########
 
231
# Main #
 
232
########
 
233
# Process command line arguments with GNU getopt.
 
234
GETOPTTEMP=`getopt -o hvV --long help,verbose,version -n "$0" -- "$@"`
 
235
std_cmd_err_handler $?
 
236
 
 
237
eval set -- "$GETOPTTEMP"
 
238
std_cmd_err_handler $?
 
239
 
 
240
while true
 
241
do
 
242
        case "$1" in
 
243
        -h|--help)
 
244
                echo "Usage is $0 [options]"
 
245
                echo "  -h or --help displays usage information"
 
246
                echo "  OR"
 
247
                echo "  -v or --verbose produce more output"
 
248
                echo "  OR"
 
249
                echo "  -V or --version displays version information"
 
250
                shift
 
251
                script_exit_code=0
 
252
                script_exit
 
253
                ;;
 
254
        -v|--verbose)
 
255
                verbose=TRUE
 
256
                shift
 
257
                ;;
 
258
        -V|--version)
 
259
                echo "$0 Script version "$version
 
260
                echo "$0 Package version "$packageversion
 
261
                shift
 
262
                script_exit_code=0
 
263
                script_exit
 
264
                ;;
 
265
        --)     shift
 
266
                break
 
267
                ;;
 
268
        *)      script_exit_code=1
 
269
                output "Internal error." 1
 
270
                script_exit
 
271
                ;;
 
272
        esac
 
273
done
 
274
 
 
275
# Script does not accept other arguments.
 
276
if [ $# -gt 0 ]; then
 
277
        script_exit_code=1
 
278
        output "Invalid argument." 1
 
279
        script_exit
 
280
fi
 
281
 
 
282
# Check bintray-debian-upload.conf
 
283
if [ ! -f "$etclocation/bintray-debian-upload.conf" ]; then
 
284
        output "$etclocation/bintray-debian-upload.conf does not exist." 1
 
285
        script_exit_code=1
 
286
        script_exit
 
287
fi
 
288
 
 
289
 
 
290
# Read or create ~/.bintray.conf
 
291
if [ -f ~/.bintray.conf ]; then
 
292
        read_bintray_conf
 
293
        std_cmd_err_handler $?
 
294
else
 
295
        create_bintray_conf
 
296
fi
 
297
 
 
298
 
 
299
# Process debian packages
 
300
pkgfilelist=$(ls *.deb)
 
301
std_cmd_err_handler $?
 
302
 
 
303
for pkgfilename in $pkgfilelist
 
304
do
 
305
        parse_filename $pkgfilename
 
306
        std_cmd_err_handler $?
 
307
 
 
308
        output "Attempting upload of $pkgfilename" 0
 
309
 
 
310
        if [ $verbose = TRUE ]; then
 
311
                echo "Package is $pkgname"
 
312
                echo "Package version is $pkgver"
 
313
                echo "Debian distribution number is $debdistnum"
 
314
                echo "Arch is $pkgarch"
 
315
        fi
 
316
 
 
317
        retrieve_dist_name
 
318
        std_cmd_err_handler $?
 
319
 
 
320
        if [ $verbose = TRUE ]; then
 
321
                echo "Debian ditribution name $debdistname" && echo
 
322
        fi
 
323
 
 
324
        # Build upload command line.
 
325
        uploadCL="curl -T $pkgfilename -u$binusr:$binapikey \"$binrepourl"
 
326
        uploadCL+="/$pkgname/$pkgver/pool/stable/${pkgname:0:1}/$pkgfilename;"
 
327
        uploadCL+="deb_distribution=$debdistname;deb_component=stable;"
 
328
        uploadCL+="deb_architecture=$pkgarch;publish=1\""
 
329
 
 
330
        if [ $verbose = TRUE ]; then
 
331
                echo $uploadCL && echo
 
332
        fi
 
333
 
 
334
        eval "$uploadCL"
 
335
        std_cmd_err_handler $?
 
336
done
 
337
 
 
338
 
 
339
script_exit_code=0
 
340
output "script completed." 0
 
341
script_exit