Browse Source

Add everything

Alois Mahdal 7 years ago
parent
commit
b8002a5136

+ 2
- 0
.gitignore View File

@@ -0,0 +1,2 @@
1
+.mkit/autoclean
2
+imapdomo-*.tar.gz

+ 165
- 0
LICENSE View File

@@ -0,0 +1,165 @@
1
+                   GNU LESSER GENERAL PUBLIC LICENSE
2
+                       Version 3, 29 June 2007
3
+
4
+ Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>
5
+ Everyone is permitted to copy and distribute verbatim copies
6
+ of this license document, but changing it is not allowed.
7
+
8
+
9
+  This version of the GNU Lesser General Public License incorporates
10
+the terms and conditions of version 3 of the GNU General Public
11
+License, supplemented by the additional permissions listed below.
12
+
13
+  0. Additional Definitions.
14
+
15
+  As used herein, "this License" refers to version 3 of the GNU Lesser
16
+General Public License, and the "GNU GPL" refers to version 3 of the GNU
17
+General Public License.
18
+
19
+  "The Library" refers to a covered work governed by this License,
20
+other than an Application or a Combined Work as defined below.
21
+
22
+  An "Application" is any work that makes use of an interface provided
23
+by the Library, but which is not otherwise based on the Library.
24
+Defining a subclass of a class defined by the Library is deemed a mode
25
+of using an interface provided by the Library.
26
+
27
+  A "Combined Work" is a work produced by combining or linking an
28
+Application with the Library.  The particular version of the Library
29
+with which the Combined Work was made is also called the "Linked
30
+Version".
31
+
32
+  The "Minimal Corresponding Source" for a Combined Work means the
33
+Corresponding Source for the Combined Work, excluding any source code
34
+for portions of the Combined Work that, considered in isolation, are
35
+based on the Application, and not on the Linked Version.
36
+
37
+  The "Corresponding Application Code" for a Combined Work means the
38
+object code and/or source code for the Application, including any data
39
+and utility programs needed for reproducing the Combined Work from the
40
+Application, but excluding the System Libraries of the Combined Work.
41
+
42
+  1. Exception to Section 3 of the GNU GPL.
43
+
44
+  You may convey a covered work under sections 3 and 4 of this License
45
+without being bound by section 3 of the GNU GPL.
46
+
47
+  2. Conveying Modified Versions.
48
+
49
+  If you modify a copy of the Library, and, in your modifications, a
50
+facility refers to a function or data to be supplied by an Application
51
+that uses the facility (other than as an argument passed when the
52
+facility is invoked), then you may convey a copy of the modified
53
+version:
54
+
55
+   a) under this License, provided that you make a good faith effort to
56
+   ensure that, in the event an Application does not supply the
57
+   function or data, the facility still operates, and performs
58
+   whatever part of its purpose remains meaningful, or
59
+
60
+   b) under the GNU GPL, with none of the additional permissions of
61
+   this License applicable to that copy.
62
+
63
+  3. Object Code Incorporating Material from Library Header Files.
64
+
65
+  The object code form of an Application may incorporate material from
66
+a header file that is part of the Library.  You may convey such object
67
+code under terms of your choice, provided that, if the incorporated
68
+material is not limited to numerical parameters, data structure
69
+layouts and accessors, or small macros, inline functions and templates
70
+(ten or fewer lines in length), you do both of the following:
71
+
72
+   a) Give prominent notice with each copy of the object code that the
73
+   Library is used in it and that the Library and its use are
74
+   covered by this License.
75
+
76
+   b) Accompany the object code with a copy of the GNU GPL and this license
77
+   document.
78
+
79
+  4. Combined Works.
80
+
81
+  You may convey a Combined Work under terms of your choice that,
82
+taken together, effectively do not restrict modification of the
83
+portions of the Library contained in the Combined Work and reverse
84
+engineering for debugging such modifications, if you also do each of
85
+the following:
86
+
87
+   a) Give prominent notice with each copy of the Combined Work that
88
+   the Library is used in it and that the Library and its use are
89
+   covered by this License.
90
+
91
+   b) Accompany the Combined Work with a copy of the GNU GPL and this license
92
+   document.
93
+
94
+   c) For a Combined Work that displays copyright notices during
95
+   execution, include the copyright notice for the Library among
96
+   these notices, as well as a reference directing the user to the
97
+   copies of the GNU GPL and this license document.
98
+
99
+   d) Do one of the following:
100
+
101
+       0) Convey the Minimal Corresponding Source under the terms of this
102
+       License, and the Corresponding Application Code in a form
103
+       suitable for, and under terms that permit, the user to
104
+       recombine or relink the Application with a modified version of
105
+       the Linked Version to produce a modified Combined Work, in the
106
+       manner specified by section 6 of the GNU GPL for conveying
107
+       Corresponding Source.
108
+
109
+       1) Use a suitable shared library mechanism for linking with the
110
+       Library.  A suitable mechanism is one that (a) uses at run time
111
+       a copy of the Library already present on the user's computer
112
+       system, and (b) will operate properly with a modified version
113
+       of the Library that is interface-compatible with the Linked
114
+       Version.
115
+
116
+   e) Provide Installation Information, but only if you would otherwise
117
+   be required to provide such information under section 6 of the
118
+   GNU GPL, and only to the extent that such information is
119
+   necessary to install and execute a modified version of the
120
+   Combined Work produced by recombining or relinking the
121
+   Application with a modified version of the Linked Version. (If
122
+   you use option 4d0, the Installation Information must accompany
123
+   the Minimal Corresponding Source and Corresponding Application
124
+   Code. If you use option 4d1, you must provide the Installation
125
+   Information in the manner specified by section 6 of the GNU GPL
126
+   for conveying Corresponding Source.)
127
+
128
+  5. Combined Libraries.
129
+
130
+  You may place library facilities that are a work based on the
131
+Library side by side in a single library together with other library
132
+facilities that are not Applications and are not covered by this
133
+License, and convey such a combined library under terms of your
134
+choice, if you do both of the following:
135
+
136
+   a) Accompany the combined library with a copy of the same work based
137
+   on the Library, uncombined with any other library facilities,
138
+   conveyed under the terms of this License.
139
+
140
+   b) Give prominent notice with the combined library that part of it
141
+   is a work based on the Library, and explaining where to find the
142
+   accompanying uncombined form of the same work.
143
+
144
+  6. Revised Versions of the GNU Lesser General Public License.
145
+
146
+  The Free Software Foundation may publish revised and/or new versions
147
+of the GNU Lesser General Public License from time to time. Such new
148
+versions will be similar in spirit to the present version, but may
149
+differ in detail to address new problems or concerns.
150
+
151
+  Each version is given a distinguishing version number. If the
152
+Library as you received it specifies that a certain numbered version
153
+of the GNU Lesser General Public License "or any later version"
154
+applies to it, you have the option of following the terms and
155
+conditions either of that published version or of any later version
156
+published by the Free Software Foundation. If the Library as you
157
+received it does not specify a version number of the GNU Lesser
158
+General Public License, you may choose any version of the GNU Lesser
159
+General Public License ever published by the Free Software Foundation.
160
+
161
+  If the Library as you received it specifies that a proxy can decide
162
+whether future versions of the GNU Lesser General Public License shall
163
+apply, that proxy's public statement of acceptance of any version is
164
+permanent authorization for you to choose that version for the
165
+Library.

+ 5
- 0
Makefile View File

@@ -0,0 +1,5 @@
1
+# imapdomo - imapfilter convenience wrapper
2
+# See LICENSE file for copyright and license details.
3
+
4
+MKIT_DIR=utils/mkit
5
+include $(MKIT_DIR)/mkit.mk

+ 40
- 0
mkit.ini View File

@@ -0,0 +1,40 @@
1
+#mkit version=0.0.14
2
+
3
+[project]
4
+    version     = 0.0.0
5
+    name        = imapdomo
6
+    pkgname     = imapdomo
7
+    tagline     = imapfilter convenience wrapper
8
+    relsrc      = master
9
+
10
+[dist]
11
+    tarball = LICENSE
12
+    tarball = Makefile
13
+    tarball = README.md
14
+    tarball = mkit.ini
15
+    tarball = src
16
+    tarball = utils
17
+    rpmstuff = packaging/imapdomo.spec
18
+    debstuff = packaging/debian
19
+
20
+[ENV]
21
+    PREFIX = /usr
22
+
23
+[roots]
24
+    bin     = [ENV:PREFIX]/bin
25
+    share   = [ENV:PREFIX]/share/imapdomo
26
+    etc     = /etc/imapdomo
27
+
28
+[tokens]
29
+    __IMAPDOMO_CONFIG_USER__    = $HOME/.config/imapdomo
30
+    __IMAPDOMO_CONFIG_LOCAL__   = [roots:etc]
31
+    __IMAPDOMO_SHARE__          = [roots:share]
32
+    __IMAPDOMO_USER_CACHE__     = $HOME/.local/share/imapdomo
33
+
34
+[modes]
35
+    bin     = 755
36
+
37
+[files]
38
+    bin     = src/imapdomo
39
+    share   = src/main.lua
40
+    share   = src/common.lua

+ 5
- 0
packaging/debian/changelog View File

@@ -0,0 +1,5 @@
1
+bmo (__MKIT_PROJ_VERSION__-1) UNRELEASED; urgency=medium
2
+
3
+  * Initial release. (Closes: #XXXXXX)
4
+
5
+ -- Alois Mahdal <netvor@vornet.cz>  Sat, 27 Feb 2016 01:38:03 +0100

+ 1
- 0
packaging/debian/compat View File

@@ -0,0 +1 @@
1
+9

+ 17
- 0
packaging/debian/control View File

@@ -0,0 +1,17 @@
1
+Source: __MKIT_PROJ_PKGNAME__
2
+Maintainer: Alois Mahdal <netvor@vornet.cz>
3
+Vcs-Browser: http://github.com/AloisMahdal/imapdomo
4
+Section: misc
5
+Priority: extra
6
+Standards-Version: 3.9.2
7
+Build-Depends: debhelper (>= 9)
8
+
9
+Package: __MKIT_PROJ_PKGNAME__
10
+Architecture: all
11
+Depends: bash, imapfilter, lua, shellfu-bash, shellfu-bash-core, shellfu-bash-extras
12
+Description: __MKIT_PROJ_TAGLINE__
13
+ imapdomo is simple convenience wrapper around imapfilter that
14
+ adds few more Lua functions and suggests some system to way how you can
15
+ organize your mail filtering and/or migrations, etc.
16
+
17
+# control file built with MKit __MKIT_SELF_VERSION__

+ 1
- 0
packaging/debian/copyright View File

@@ -0,0 +1 @@
1
+

+ 3
- 0
packaging/debian/imapdomo.install View File

@@ -0,0 +1,3 @@
1
+/usr/bin/imapdomo
2
+/usr/share/imapdomo/common.lua
3
+/usr/share/imapdomo/main.lua

+ 13
- 0
packaging/debian/rules View File

@@ -0,0 +1,13 @@
1
+#!/usr/bin/make -f
2
+
3
+%:
4
+
5
+	dh $@
6
+
7
+override_dh_auto_install:
8
+
9
+	make install DESTDIR=debian/tmp
10
+
11
+override_dh_usrlocal:
12
+
13
+	@true

+ 1
- 0
packaging/debian/source/format View File

@@ -0,0 +1 @@
1
+3.0 (quilt)

+ 40
- 0
packaging/imapdomo.spec View File

@@ -0,0 +1,40 @@
1
+Name:		__MKIT_PROJ_PKGNAME__
2
+Version:	__MKIT_PROJ_VERSION__
3
+Release:	1%{?dist}
4
+Summary:	__MKIT_PROJ_PKGNAME__ - __MKIT_PROJ_TAGLINE__
5
+
6
+License:	LGPLv2
7
+Source0:	%{name}-%{version}.tar.gz
8
+
9
+BuildArch:	noarch
10
+BuildRequires:	coreutils git
11
+Requires:	imapfilter
12
+Requires:	shellfu-bash
13
+Requires:	shellfu-bash-core
14
+Requires:	shellfu-bash-extras
15
+
16
+%description
17
+imapdomo is simple convenience wrapper around imapfilter that
18
+adds few more Lua functions and suggests some system to way how you can
19
+organize your mail filtering and/or migrations, etc.
20
+
21
+%prep
22
+%setup -q
23
+
24
+
25
+%build
26
+# %%configure
27
+make %{?_smp_mflags}
28
+
29
+%install
30
+%make_install
31
+
32
+%files
33
+%dir /usr/share/imapdomo
34
+/usr/bin/imapdomo
35
+/usr/share/imapdomo/common.lua
36
+/usr/share/imapdomo/main.lua
37
+
38
+%changelog
39
+
40
+# specfile built with MKit __MKIT_SELF_VERSION__

+ 190
- 0
src/common.lua View File

@@ -0,0 +1,190 @@
1
+os = require "os"
2
+socket = require "socket"
3
+
4
+
5
+------------------------------------------------------------------------------
6
+-- config and handling                                                      --
7
+------------------------------------------------------------------------------
8
+
9
+function shortname()
10
+    --
11
+    -- Short hostname
12
+    --
13
+    local long = socket.dns.gethostname()
14
+    return string.gmatch(long, '[^.]+')()
15
+end
16
+
17
+function file_exists(name)
18
+    --
19
+    -- True if file *name* exists
20
+    --
21
+    local f = io.open(name,"r")
22
+    if f ~= nil then
23
+        io.close(f)
24
+        return true
25
+    else
26
+        return false
27
+    end
28
+end
29
+
30
+function init_host(session)
31
+    --
32
+    -- Do host-specific init, if exists
33
+    --
34
+    local init = session.dirs.host .. "/init.lua"
35
+    if file_exists(init) then
36
+        dofile(init)
37
+        return
38
+    end
39
+end
40
+
41
+function handle(session)
42
+    --
43
+    -- Look for action handler in session dirs; do first one that exists
44
+    --
45
+    local valid = {
46
+        newmail = true,
47
+        rewind = true,
48
+        cleanup = true,
49
+        migrate = true
50
+    }
51
+    if not valid[session.action] then
52
+        error("invalid action: " .. session.action)
53
+        return nil
54
+    end
55
+    init_host(session)
56
+    for k,v in ipairs({session.dirs.host, session.dirs.default}) do
57
+        attempt = v .. "/handlers/" .. session.action .. ".lua"
58
+        if file_exists(attempt) then
59
+            dofile(attempt)
60
+            return
61
+        end
62
+    end
63
+end
64
+
65
+
66
+------------------------------------------------------------------------------
67
+-- mail getters                                                             --
68
+------------------------------------------------------------------------------
69
+
70
+function get_queue(acct, mbox)
71
+    --
72
+    -- Get queue from *mbox* from *acct* or nil (no messages)
73
+    --
74
+    -- If mbox is not specified, "FILTER_QUEUE" is used
75
+    --
76
+    mbox = mbox or "FILTER_QUEUE"
77
+    local exist, unread, unseen, uidnext = acct[mbox]:check_status()
78
+    if exist > 0 then
79
+        return acct[mbox]:select_all()
80
+    end
81
+    return nil
82
+end
83
+
84
+function _notifirc1(subj, from, body)
85
+    --
86
+    -- notify about message using (pre-configured) notifirc
87
+    --
88
+    local fd = assert(io.popen('notifirc -c mail -f - "message preview:"', "w"))
89
+    local fmt = "> %s\n> %s\n> BODY: %s"
90
+    fd:write(fmt:format(subj, from, body))
91
+    fd:close()
92
+end
93
+
94
+function notifirc_all(seq)
95
+    --
96
+    -- Send notifications about all messages in *seq*
97
+    --
98
+    for _, mesg in ipairs(seq) do
99
+        mbox, uid = table.unpack(mesg)
100
+        subj = mbox[uid]:fetch_field('Subject')
101
+        from = mbox[uid]:fetch_field('From')
102
+        body = mbox[uid]:fetch_body()
103
+        _notifirc1(subj, from, body)
104
+    end
105
+end
106
+
107
+function _partinf_compare(a, b)
108
+    if not type(a) == type(b) then
109
+        return false
110
+    end
111
+    if type(a) == 'number' then
112
+        return a == b
113
+    elseif type(a) == 'string' then
114
+        return a:lower() == b:lower()
115
+    end
116
+end
117
+
118
+function has_part_like(query, structure)
119
+    --
120
+    -- True if structure has MIME part matching *query*
121
+    --
122
+    if structure == nil then
123
+        return false
124
+    end
125
+    for partid, partinf in pairs(structure) do
126
+        local part_answer = true
127
+        -- check all query parts
128
+        for qkey, qvalue in pairs(query) do
129
+            value = partinf[qkey]
130
+            if not _partinf_compare(value, qvalue) then
131
+                part_answer = false
132
+                break
133
+            end
134
+        end
135
+        if part_answer then
136
+            return true
137
+        end
138
+    end
139
+    return false
140
+end
141
+
142
+function save_header(mesg, name)
143
+    --
144
+    -- Append header from *mesg* to file named *name*
145
+    --
146
+    -- File will be placed under directory specified by IMAPDOMO_HEADERS
147
+    -- environment variable.
148
+    --
149
+    local dest = os.getenv("IMAPDOMO_HEADERS") .. '/' .. name
150
+    local cmd = ('cat >>"%q"'):format(dest)
151
+    mbox, uid = table.unpack(mesg)
152
+    header = mbox[uid]:fetch_header()
153
+    if pipe_to(cmd, header) == 0 then
154
+        return true
155
+    else
156
+        return false
157
+    end
158
+end
159
+
160
+function filter_header_saved(seq, name)
161
+    --
162
+    -- Save headers from sequence
163
+    --
164
+    -- Append header of each message in sequence *seq* to file names
165
+    -- *name* and return new sequence with those messages where save was
166
+    -- successful.
167
+    --
168
+    result = Set {}
169
+    for _, mesg in ipairs(seq) do
170
+        if save_header(mesg, name) then
171
+            table.insert(result, mesg)
172
+        end
173
+    end
174
+    return result
175
+end
176
+
177
+function filter_part_like(query, seq)
178
+    --
179
+    -- Run MIME part query on *seq* sequence of messages
180
+    --
181
+    result = Set {}
182
+    for _, mesg in ipairs(seq) do
183
+        mbox, uid = table.unpack(mesg)
184
+        structure = mbox[uid]:fetch_structure()
185
+        if has_part_like(query, structure) then
186
+            table.insert(result, mesg)
187
+        end
188
+    end
189
+    return result
190
+end

+ 82
- 0
src/imapdomo.skel View File

@@ -0,0 +1,82 @@
1
+#!/bin/bash
2
+
3
+. "$(shellfu-get path)" || exit 3
4
+
5
+shellfu import pretty
6
+
7
+IMAPDOMO_CFGDIR="__IMAPDOMO_CONFIG_USER__"
8
+IMAPDOMO_USER_CACHE="__IMAPDOMO_USER_CACHE__"
9
+
10
+
11
+usage() {
12
+    mkusage "[options] ACTION" \
13
+        -o                                                                    \
14
+            "-c DIR      change to DIR before doing anything"                 \
15
+            "-d          turn on debugging mode"                              \
16
+       --                                                                     \
17
+       "imapdomo will try to read .imapdomo/host/HOSTNAME/init.lua and"       \
18
+       ".imapdomo/host/HOSTNAME/handlers/ACTION.lua, where HOSTNAME is your"  \
19
+       "short hostname (eg. 'foo' in 'foo.example.com')."                     \
20
+       ""                                                                     \
21
+       "four valid actions are understood; since you must write handler for"  \
22
+       "each action you want to use, the meanings below are purely a guide:"  \
23
+       ""                                                                     \
24
+       "    newmail - check for and file new mail"                            \
25
+       "    rewind  - re-add messages from FILTER_FAIL back to FILTER_QUEUE"  \
26
+       "    cleanup - delete or archive old messages"                         \
27
+       "    migrate - move mail between mailboxes"                            \
28
+       ""                                                                     \
29
+       "See imapfilter_config(5)) for guide and API reference.  Few functions"\
30
+       "are also available in .imapdomo/common.lua"
31
+}
32
+
33
+mkcmd() {
34
+    #
35
+    # Compose imapfilter command
36
+    #
37
+    echo -n "IMAPDOMO_ACTION=$Action"
38
+    echo -n "  IMAPDOMO_HEADERS=$HeaderDir"
39
+    echo -n "  IMAPFILTER_HOME=$CfgDir"
40
+    echo -n "  imapfilter"
41
+    if $Debug
42
+    then
43
+        mkdir -p "$LogDir"
44
+        echo -n "  -d $LogDir/debug.log"
45
+        echo -n "  -v"
46
+    fi
47
+    echo -n "  -c $CfgDir/main.lua"
48
+}
49
+
50
+main() {
51
+    local Action    # what to do
52
+    local Debug     # true if debugging
53
+    local cmd       # imapfilter command
54
+    local CfgDir    # config directory
55
+    local LogDir    # directory to store logs
56
+    local HeaderDir # directory to store headers by save_header()
57
+    local CdTo      # change dir to this before running imapfilter
58
+    CfgDir="$IMAPDOMO_CFGDIR"
59
+    LogDir="$IMAPDOMO_USER_CACHE/logs"
60
+    HeaderDir="$IMAPDOMO_USER_CACHE/headers"
61
+    Debug=false
62
+    #shellcheck disable=SC2034
63
+    while true; do case $1 in
64
+        -c) CdTo="$2"; shift 2 || usage ;;
65
+        -d) Debug=true; PRETTY_DEBUG=true; shift ;;
66
+        -*) usage ;;
67
+        *)  break ;;
68
+    esac done
69
+    Action="$1"; shift
70
+    grep -qw "$Action" <<< "newmail|rewind|cleanup|migrate" || usage
71
+    cmd=$(mkcmd)
72
+    debug -v cmd
73
+    bash -n <<<"$cmd" || die
74
+    if test -n "$CdTo";
75
+    then
76
+        cd "$CdTo" || die
77
+    fi
78
+    mkdir -p "$HeaderDir" || die
79
+    eval "$cmd"
80
+}
81
+
82
+main "$@"

+ 11
- 0
src/main.lua.skel View File

@@ -0,0 +1,11 @@
1
+#!/bin/lua
2
+
3
+dofile "__IMAPDOMO_SHARE__/common.lua"
4
+
5
+handle({
6
+    action = os.getenv("IMAPDOMO_ACTION"),
7
+    dirs = {
8
+        host = os.getenv("IMAPFILTER_HOME") .. "/host/" .. shortname(),
9
+        defhost = os.getenv("IMAPFILTER_HOME") .. "/default",
10
+    }
11
+})

+ 217
- 0
utils/mkit/include/build.sh View File

@@ -0,0 +1,217 @@
1
+#!/bin/bash
2
+
3
+. "$MKIT_DIR/include/ini.sh"   || die "cannot import ini.sh"
4
+. "$MKIT_DIR/include/facts.sh" || die "cannot import facts.sh"
5
+
6
+
7
+_build1() {
8
+    #
9
+    # Process one skeleton
10
+    #
11
+    local srcpath="$1"
12
+    local dstpath="$2"
13
+    local ftype="$3"
14
+    test -n "$dstpath"  || dstpath=${srcpath%.skel}
15
+    test -n "$ftype"    || ftype=$(_guess_ftype "$dstpath")
16
+    debug_var srcpath dstpath ftype
17
+    <"$srcpath" _build1_ftype "$ftype" >"$dstpath"
18
+    mkdir -p "$MKIT_LOCAL"
19
+    echo "$dstpath" >> "$MKIT_LOCAL/built.lst"
20
+}
21
+
22
+_build1_ftype() {
23
+    #
24
+    # Build a file of type $1
25
+    #
26
+    local ftype="$1"
27
+    case $ftype in
28
+        MKIT_COMMON)    _expand_tokens "tokens" ;;
29
+        markdown)       _expand_includes | _expand_tokens "tokens" ;;
30
+        rpmstuff)       _expand_tokens "tokens" "rpmstuff:tokens" ;;
31
+        *)              die "unknown file type: $ftype" ;;
32
+    esac
33
+}
34
+
35
+_expand_includes() {
36
+    #
37
+    # Expand include directives
38
+    #
39
+    # Expand e.g. `<!-- include4: foo.sh -->` to include code of foo.sh
40
+    #
41
+    perl -we '
42
+        use strict;
43
+        my $text;
44
+        while (<>) {
45
+            chomp;
46
+            if (m/<!-- include4: (\S+) -->/) {
47
+                open my $fh, $1 or warn "cannot find: $1";
48
+                my $text = do { local($/); <$fh> };
49
+                close $fh;
50
+                $text =~ s/^(.)/    $1/gm;
51
+                chomp $text;
52
+                print "$text\n";
53
+            } else {
54
+                print "$_\n";
55
+            }
56
+        }
57
+    '
58
+}
59
+
60
+_expand_tokens() {
61
+    #
62
+    # Expand tokens from sections $@
63
+    #
64
+    local script=$(mktemp --tmpdir mkit-tmp.XXXXXXXXXX)
65
+    local section varname varvalue
66
+    {
67
+        for section in "$@";
68
+        do
69
+            debug_var section
70
+            ini lskeys "$section" \
71
+              | while read varname;
72
+                do
73
+                    varvalue="$(ini 1value "$section:$varname" | _qfs )"
74
+                    echo "s|$varname|$varvalue|g;"
75
+                    debug_var varname varvalue
76
+                done
77
+        done
78
+        echo "s|__MKIT_PROJ_NAME__|$(ini 1value project:name | _qfs)|g;"
79
+        echo "s|__MKIT_PROJ_CODENAME__|$(ini 1value project:codename | _qfs)|g;"
80
+        echo "s|__MKIT_PROJ_PKGNAME__|$(ini 1value project:pkgname | _qfs)|g;"
81
+        echo "s|__MKIT_PROJ_TAGLINE__|$(ini 1value project:tagline | _qfs)|g;"
82
+        echo "s|__MKIT_PROJ_MAINTAINER__|$(ini 1value project:maintainer | _qfs)|g;"
83
+        echo "s|__MKIT_PROJ_VCS_BROWSER__|$(ini 1value project:vcs_browser | _qfs)|g;"
84
+        echo "s|__MKIT_PROJ_GIT_LASTHASH__|$(git_lasthash | _qfs)|g;"
85
+        echo "s|__MKIT_PROJ_VERSION__|$(semver | _qfs)|g;"
86
+        echo "s|__MKIT_SELF_VERSION__|$MKIT_VERSION|g;"
87
+    } >> "$script"
88
+    sed -f "$script" || die "_expand_tokens failed"
89
+    rm "$script"
90
+}
91
+
92
+_guess_ftype() {
93
+    #
94
+    # Guess file type from destination path $1
95
+    #
96
+    local dstpath="$1"
97
+    case $dstpath in
98
+        *.md) echo markdown    ;;
99
+        *)    echo MKIT_COMMON ;;
100
+    esac
101
+}
102
+
103
+_qfs() {
104
+    #
105
+    # Quote for our sed scipt's RHS
106
+    #
107
+    sed '
108
+        s:\\:\\\\:g
109
+        s:|:\\|:g
110
+    '
111
+}
112
+
113
+build() {
114
+    #
115
+    # Add meat to all skeletons
116
+    #
117
+    local srcpath
118
+    find -type f -name '*.skel' \
119
+     | while read srcpath;
120
+       do
121
+           _build1 "$srcpath"
122
+       done
123
+}
124
+
125
+build_manpages() {
126
+    local manfile mdfile
127
+    if command -v ronn >/dev/null;
128
+    then
129
+        ini lskeys "files:man" \
130
+          | while read manfile;
131
+            do
132
+                mdfile="$manfile.md"
133
+                ronn -r "$mdfile"
134
+                mkdir -p "$MKIT_LOCAL"
135
+                echo "$manfile" >> "$MKIT_LOCAL/built.lst"
136
+            done
137
+    else
138
+        echo "ronn is not installed"
139
+        return 1
140
+    fi
141
+}
142
+
143
+clean() {
144
+    #
145
+    # Clean up tree after building
146
+    #
147
+    test -f "$MKIT_LOCAL/built.lst" && {
148
+        <"$MKIT_LOCAL/built.lst" grep -v -e '\.\.' -e ^/ \
149
+          | xargs -r rm -rf
150
+        rm -f "$MKIT_LOCAL/built.lst"
151
+        rmdir --ignore-fail-on-non-empty "$MKIT_LOCAL"
152
+    }
153
+    true
154
+}
155
+
156
+debstuff() {
157
+    #
158
+    # Build Debian stuff (eamed tarball, debian dir)
159
+    #
160
+    local version="$(semver)"
161
+
162
+    # tarball - we should already have by means of 'dist'
163
+    #
164
+    mv "${MKIT_PROJ_PKGNAME}-$version.tar.gz" \
165
+       "${MKIT_PROJ_PKGNAME}_$version.orig.tar.gz" \
166
+     || die "could not rename tarball"
167
+    echo "${MKIT_PROJ_PKGNAME}_$version.orig.tar.gz" >> "$MKIT_LOCAL/built.lst"
168
+
169
+    # read content of each mandatory file from debian_skel
170
+    #
171
+    local debian_skel=$(ini 1value dist:debstuff)
172
+    test -n "$debian_skel" || die "dist:debstuff not specified"
173
+    test -d "$debian_skel" || die "debian directory template found: $debian_skel"
174
+    mkdir -p debian/source
175
+    local dfsrc dftgt
176
+    find "$debian_skel" -type f \
177
+      | while read dfsrc;
178
+        do
179
+            dftgt="debian/${dfsrc#$debian_skel}"
180
+            mkdir -p "$(dirname "$dftgt")"
181
+            _build1 "$dfsrc" "$dftgt"
182
+        done
183
+    echo debian >> "$MKIT_LOCAL/built.lst"
184
+}
185
+
186
+dist() {
187
+    #
188
+    # Create distributable tarball
189
+    #
190
+    #FIXME: lacking Makefile skills, we do this step twice fot
191
+    #       rpmstuff, hence -f hack for gzip
192
+    #
193
+    local version=$(semver)
194
+    local git_lasthash=$(git_lasthash)
195
+    local dirname=$MKIT_PROJ_PKGNAME-$version
196
+    mkdir -p "$dirname"
197
+    ini values "dist:tarball" | xargs -I DIST_ITEM cp -R DIST_ITEM "$dirname"
198
+    update_version "$version" "$dirname/mkit.ini"
199
+    mkdir -p "$dirname/.mkit"
200
+    echo -n "$git_lasthash" > "$dirname/.mkit/git_lasthash"
201
+    tar -cf "$dirname.tar" "$dirname"
202
+    gzip -f "$dirname.tar"      # see above FIXME
203
+    mkdir -p "$MKIT_LOCAL"
204
+    echo "$dirname.tar.gz" >> "$MKIT_LOCAL/built.lst"
205
+    rm -rf "$dirname"
206
+}
207
+
208
+rpmstuff() {
209
+    #
210
+    # Build specfile
211
+    #
212
+    local specname="$MKIT_PROJ_PKGNAME.spec"
213
+    local specsrc="$(ini 1value "dist:rpmstuff")"
214
+    test -n "$specsrc" || die "dist:rpmstuff not specified"
215
+    test -f "$specsrc" || die "specfile template not found: $specsrc"
216
+    _build1 "$specsrc" "$specname"
217
+}

+ 122
- 0
utils/mkit/include/deploy.sh View File

@@ -0,0 +1,122 @@
1
+#!/bin/bash
2
+
3
+_deploy_item() {
4
+    #
5
+    # Deploy item and make it look like wanted
6
+    #
7
+    # usage: _deploy_item src dst [mode]
8
+    #
9
+    # Both src and dst must be names of actual items[1],
10
+    # whereas dst must not exist.  On update, dst is
11
+    # usually to be replaced but that is uninstall's
12
+    # job!
13
+    #
14
+    #  [1] Ie. src=foo and dst=/foo/bar does *always*
15
+    #      mean that foo will become 'bar'.  This is
16
+    #      different than traditional `cp` behavior,
17
+    #      when this depends if 'bar' already exists
18
+    #      as a directory.
19
+    #
20
+    # If mode is omitted or empty, MKIT_DEFAULT_MODE is
21
+    # used instead.
22
+    #
23
+    # Directories are copied recursively, and mode is
24
+    # applied only to files.
25
+    #
26
+    local src="$1"
27
+    local dst="$2"
28
+    local mode="${3:-$MKIT_DEFAULT_MODE}"
29
+    if test -d "$src";
30
+    then
31
+        _maybe mkdir -vp "$(dirname "$dst")"
32
+        _maybe cp -Tvr "$src" "$dst"
33
+        find "$dst" -type f \
34
+          | while read chmod_item;
35
+            do
36
+                _maybe chmod "$mode" "$chmod_item"
37
+            done
38
+    else
39
+        _maybe install -DTvm "$mode" "$src" "$dst"
40
+    fi
41
+}
42
+
43
+_get_dst() {
44
+    #
45
+    # Find out target path for src file $2 of group $1
46
+    #
47
+    local grp=$1
48
+    local src=$2
49
+    local dst=$3
50
+    test -n "$dst" || dst=${src##*/}
51
+    echo "$(_get_root "$grp")/$dst"
52
+}
53
+
54
+_get_root() {
55
+    #
56
+    # Find out target root for group $1
57
+    #
58
+    local grp="$1"
59
+    local root=$(ini 1value "roots:$grp")
60
+    local destdir=$(ini 1value ENV:DESTDIR)
61
+    destdir=${destdir%/}
62
+    case $destdir:$root in
63
+        *:)     die "missing in config.ini: roots:$grp" ;;
64
+        :*)     echo "$root" ;;
65
+        *:*)    echo "$destdir/$root" ;;
66
+    esac
67
+}
68
+
69
+_maybe() {
70
+    #
71
+    # Call the deploy command $1 $@ unless in dry mode
72
+    #
73
+    debug "$@"
74
+    local cmd="$1"; shift
75
+    $MKIT_DRY && return
76
+    case "$cmd" in
77
+        cp|rm|rmdir|chmod|mkdir) $cmd "$@" ;;
78
+        install)                 command -p install "$@" ;;
79
+        *)                       die "bad command called";;
80
+    esac
81
+}
82
+
83
+install() {
84
+    #
85
+    # Install product
86
+    #
87
+    local dst group mode src
88
+    ini lskeys "files" \
89
+      | sort \
90
+      | uniq \
91
+      | while read group;
92
+        do
93
+            mode=$(ini 1value "modes:$group")
94
+            ini values "files:$group" \
95
+              | while read src dst;
96
+                do
97
+                    dst=$(_get_dst "$group" "$src" "$dst")
98
+                    _deploy_item "$src" "$dst" "$mode"
99
+                done
100
+        done
101
+    test -f "$MKIT_LOCAL/autoclean" && clean
102
+    true
103
+}
104
+
105
+uninstall() {
106
+    #
107
+    # Uninstall product
108
+    #
109
+    local dst group src
110
+    ini lskeys "files" \
111
+      | sort \
112
+      | uniq \
113
+      | while read group;
114
+        do
115
+            ini values "files:$group" \
116
+              | while read src dst;
117
+                do
118
+                    dst=$(_get_dst "$group" "$src" "$dst")
119
+                    _maybe rm -vrf "$dst"
120
+                done
121
+        done
122
+}

+ 175
- 0
utils/mkit/include/facts.sh View File

@@ -0,0 +1,175 @@
1
+#!/bin/bash
2
+
3
+. "$MKIT_DIR/include/ini.sh" || die "cannot import ini.sh"
4
+
5
+git_bool() {
6
+    #
7
+    # Get git bool (ie. exit status counts) $1
8
+    #
9
+    local bool_name="$1"
10
+    git_present || warn "can't give bool outside git repo: $bool_name"
11
+    case "$bool_name" in
12
+        dirty_files)
13
+            git diff-files | grep -qm 1 .
14
+            ;;
15
+        dirty_index)
16
+            git diff-index HEAD | grep -qm 1 .
17
+            ;;
18
+        dirty)
19
+            git_bool dirty_files || git_bool dirty_index
20
+            ;;
21
+        *)
22
+            warn "unknown git bool asked: $bool_name"
23
+            return 2
24
+            ;;
25
+    esac
26
+}
27
+
28
+git_fact() {
29
+    #
30
+    # Get git fact $1
31
+    #
32
+    local fact_name="$1"
33
+    git_present || warn "can't give fact outside git repo: $fact_name"
34
+    case "$fact_name" in
35
+        latest_tag)
36
+            git log --format="%d" \
37
+              | sed 's/,/\n/g' \
38
+              | sed 's/^[[:blank:]]*//' \
39
+              | grep -E '^\(?tag' \
40
+              | tr -cd '[[:digit:]].v\n' \
41
+              | grep . -m 1
42
+            ;;
43
+        latest_version)
44
+            git_fact latest_tag | git_tag2ver
45
+            ;;
46
+        current_branch)
47
+            git rev-parse --abbrev-ref HEAD
48
+            ;;
49
+        reldiff)
50
+            git log --oneline "$(git_fact latest_tag)..HEAD" --name-only
51
+            ;;
52
+        latest_sha)
53
+            git log -1 --pretty=format:%h HEAD
54
+            ;;
55
+        *)
56
+            warn "unknown git fact asked: $fact_name"
57
+            ;;
58
+    esac
59
+}
60
+
61
+git_present() {
62
+    #
63
+    # True if we're in a git repo
64
+    #
65
+    git rev-parse HEAD >&/dev/null
66
+}
67
+
68
+git_tag2ver() {
69
+    #
70
+    # Convert tag to version
71
+    #
72
+    sed s/^v//
73
+}
74
+
75
+git_ver2tag() {
76
+    #
77
+    # Convert version to tag
78
+    #
79
+    sed s/^/v/
80
+}
81
+
82
+git_lasthash() {
83
+    #
84
+    # Show last commit hash (with .dirty suffix if needed)
85
+    #
86
+    # If outside git repo, get it from .mkit/git_lasthash, which
87
+    # should have been put there by dist target.  (I.e., this won't
88
+    # work if you got outside the git repo in other way than dist
89
+    # target, but that's actually expected.)
90
+    #
91
+    if git_present;
92
+    then    # we are in git repo
93
+        local last_hash=$(git rev-parse HEAD)
94
+        echo -n "$last_hash"
95
+        git_bool dirty && echo -n ".dirty"
96
+    else    # we are outside (eg. distributor's build dir')
97
+        grep . .mkit/git_lasthash || {
98
+            echo UNKNOWN
99
+            warn "malformed source, could not determine git hash"
100
+        }
101
+    fi
102
+}
103
+
104
+semver() {
105
+    #
106
+    # Build semver version string with build metadata
107
+    #
108
+    # Build version string from available info using following
109
+    # logic:
110
+    #
111
+    #  1. use project.version (from mkit.ini)
112
+    #  2. if we are in git, override the version with last tag
113
+    #  3. if set, add project:prerl (from mkit.ini) as pre-release ID
114
+    #     (afer dash)
115
+    #  4. if we are at a later commit than the last tag, add branch
116
+    #     name and commit sha1 to build metadata (after plus sign)
117
+    #  5. if the tree is "dirty", i.e. has uncommited changes,
118
+    #     add "dirty" to build metadata
119
+    #
120
+    # The version is compatible with SemVer 2.0.0.
121
+    #
122
+    # Examples:
123
+    #
124
+    #     myprog v1.0.7                         # all clear
125
+    #     myprog v1.0.7-alpha                   # mkit.ini: project:prerl="alpha"
126
+    #     myprog v1.0.7-alpha+g1aef811.master   # ^^ + some commits after
127
+    #     myprog v1.0.7-alpha+gf14fc4f.api2     # ^^ + on a feature branch
128
+    #     myprog v1.0.7-alpha+gf14fc4f.api2.dirty  # ^^ + tree edited
129
+    #     myprog v1.0.7-alpha+dirty             # tag OK but tree edited
130
+    #     myprog v1.0.7+dirty                   # ^^ but no pre-release id
131
+    #
132
+    # Note that versions with "dirty" should be perceived as kind of
133
+    # dangerous outside developer's own machine.  Versions with sha1 are
134
+    # safer but must not be released.
135
+    #
136
+    # I have considered decorating the git commit refs to make them
137
+    # sort of sortable (e.g. "r1.g1aef811"), but on second thought,
138
+    # I don't think it's good idea to give *any* semantics to meta-data
139
+    # at all.  First, there is no rule that r1<r2<r3; a commit can be
140
+    # removing what other just added and one change can be split to
141
+    # multiple commits.  Also, the whole thing breaks anyway once you
142
+    # rebase your branch (no, it's not a sin).  The sole purpose of
143
+    # meta-data is to *identify* the code, and provide safe path back
144
+    # to tree; commit refs are already perfect for that.
145
+    #
146
+    # FIXME:  Using project:prerl for release IDs may not be compatible with
147
+    #         release strategy implemented in release.sh
148
+    #
149
+    local version=$(ini 1value project:version)
150
+    local prerl=$(ini 1value project:prerl)
151
+    grep ":" <<<"$prerl" && warn "colon in project:prerl may corrupt version data: $prerl"
152
+    if git_present;
153
+    then    # we are in git repo... so we can get smart
154
+        local latest_tag=$(git_fact latest_tag)
155
+        if ! git describe --tags --exact-match HEAD >&/dev/null;
156
+        then    # we are at a later commit than the last tag
157
+            local commit="$(git_fact current_branch).g$(git_fact latest_sha)"
158
+        fi
159
+        local dirty=""
160
+        local suffix=""
161
+        git_bool dirty; dirty=$?
162
+        test -n "$latest_tag" && version=${latest_tag:1}
163
+        case "$dirty:$commit" in
164
+            1:)  suffix=""               ;;
165
+            0:)  suffix="+dirty"         ;;
166
+            1:*) suffix="+$commit"       ;;
167
+            0:*) suffix="+$commit.dirty" ;;
168
+            *)   suffix=MKIT_BUG;
169
+                 warn "MKIT_BUG: bad dirt/commit detection" ;;
170
+        esac
171
+        test -n "$prerl" && suffix="-$prerl$suffix"
172
+        version="$version$suffix"
173
+    fi
174
+    echo "$version"
175
+}

+ 143
- 0
utils/mkit/include/ini.sh View File

@@ -0,0 +1,143 @@
1
+#!/bin/bash
2
+
3
+_ini_cat() {
4
+    #
5
+    # A no-op for text stream
6
+    #
7
+    while read line;
8
+    do
9
+        printf -- "%s\n" "$line"
10
+    done
11
+}
12
+
13
+_ini_expand() {
14
+    #
15
+    # Expand reference value (prefix only)
16
+    #
17
+    local line suffix ref value
18
+    while read line;                        # [foo:bar]/path
19
+    do
20
+        suffix="${line#\[*\]}"              # /path
21
+        ref="${line%$suffix}"               # [foo:bar]
22
+        ref="${ref%\]}"                     # [foo:bar
23
+        ref="${ref#\[}"                     # foo:bar
24
+        value="$(ini 1value "$ref")"        # foo_bar_value
25
+        printf -- "%s\n" "$value$suffix"    # foo_bar_value/path
26
+    done
27
+}
28
+
29
+_ini_grepkey() {
30
+    #
31
+    # Read key from a section
32
+    #
33
+    local wnt=$1
34
+    grep '.' \
35
+      | grep -v '\s*#' \
36
+      | sed -e 's/ *= */=/; s/ +$//; s/^//;' \
37
+      | grep -e "^$wnt=" \
38
+      | cut -d= -f2- \
39
+      | _ini_maybe_expand
40
+}
41
+
42
+_ini_greppath() {
43
+    #
44
+    # Read key from the right section
45
+    #
46
+    # E.g. `files:share:my/lib.sh` should read
47
+    #
48
+    #     [files:share]
49
+    #         my/lib.sh   = proj/my/lib.sh
50
+    #
51
+    local wnt="$1"
52
+    local wntkey="${wnt##*:}"
53
+    local wntsec="${wnt%:$wntkey}"
54
+    if test "$wntsec" = 'ENV';
55
+    then
56
+        local override="${!wntkey}"
57
+        if test -n "$override";
58
+        then
59
+            echo "$override"
60
+            return
61
+        fi
62
+    fi
63
+    _ini_grepsec "$wntsec" | _ini_grepkey "$wntkey"
64
+}
65
+
66
+_ini_grepsec() {
67
+    #
68
+    # Read one INI section
69
+    #
70
+    local wnt="$1"
71
+    local ok=false
72
+    grep '.' \
73
+      | grep -v '\s*#' \
74
+      | while read line;
75
+        do
76
+            case "$line" in
77
+                \[$wnt\]) ok=true;  continue ;;
78
+                \[*\])    ok=false; continue ;;
79
+            esac
80
+            $ok || continue
81
+            printf -- "%s\n" "$line"
82
+        done \
83
+      | sed -e 's/ *= */=/; s/ +$//; s/^//;'
84
+}
85
+
86
+_ini_lskeys() {
87
+    #
88
+    # List keys from a section
89
+    #
90
+    local sct="$1"
91
+    _ini_grepsec "$sct" | cut -d= -f1 | sort | uniq
92
+}
93
+
94
+_ini_maybe_expand() {
95
+    #
96
+    # Decide whether or not to expand
97
+    #
98
+    if test "$MKIT_INI_EXPAND" -gt 0;
99
+    then
100
+        MKIT_INI_EXPAND=$(( --MKIT_INI_EXPAND )) _ini_expand
101
+    else
102
+        _ini_cat
103
+    fi
104
+}
105
+
106
+ini() {
107
+    #
108
+    # do ini operation
109
+    #
110
+    local op=$1
111
+    local arg=$2
112
+    local fn
113
+    local limit=_ini_cat
114
+    case $op in
115
+        lskeys) fn=_ini_lskeys   ;;
116
+        sec)    fn=_ini_grepsec  ;;
117
+        values) fn=_ini_greppath ;;
118
+        1value) fn=_ini_greppath; limit="tail -1" ;;
119
+        *)      die "incorrect use of \`ini()\`"
120
+    esac
121
+    <"$MKIT_INI" $fn "$arg" | $limit
122
+}
123
+
124
+update_version() {
125
+    #
126
+    # Change project.version in mkit.ini at path $2 to version $1
127
+    #
128
+    local version="$1"
129
+    local inifile="$2"
130
+    local tmp=$(mktemp -t mkit.update_version.XXXXXXXX)
131
+    <"$inifile" perl -e '
132
+        my $hit = 0;
133
+        my $done = 0;
134
+        foreach (<STDIN>) {
135
+            if      ($done) { print; next; }
136
+            elsif   (m/\[project\]/) { $hit++; print; next; }
137
+            elsif   (m/\[/) { $hit = 0; print; next; }
138
+            elsif   ($hit) { s/\bversion\b( *)=( *).*/version$1=$2$ARGV[0]/ and $done++; print; }
139
+            else { print; next; }
140
+        }
141
+    ' "$version" >"$tmp" || die "failed to update version in mkit.ini"
142
+    mv "$tmp" "$inifile"
143
+}

+ 143
- 0
utils/mkit/include/mkit.sh View File

@@ -0,0 +1,143 @@
1
+#!/bin/bash
2
+
3
+. "$MKIT_DIR/include/build.sh"  || die "cannot import build.sh"
4
+. "$MKIT_DIR/include/deploy.sh" || die "cannot import deploy.sh"
5
+. "$MKIT_DIR/include/release.sh" || die "cannot import release.sh"
6
+. "$MKIT_DIR/include/ini.sh"    || die "cannot import ini.sh"
7
+
8
+_valid_targets() {
9
+    #
10
+    # List valid routes
11
+    #
12
+    echo build
13
+    echo build_manpages
14
+    echo clean
15
+    echo debstuff
16
+    echo dist
17
+    echo install
18
+    echo release_x
19
+    echo release_y
20
+    echo release_z
21
+    echo rpmstuff
22
+    echo uninstall
23
+    echo vbump_x
24
+    echo vbump_y
25
+    echo vbump_z
26
+}
27
+
28
+debug() {
29
+    #
30
+    # Print debug message
31
+    #
32
+    $MKIT_DEBUG || return 0
33
+    echo "MKIT_DEBUG: ${FUNCNAME[1]}()" "$@" >&2
34
+}
35
+
36
+debug_var() {
37
+    #
38
+    # Print debug message
39
+    #
40
+    $MKIT_DEBUG || return 0
41
+    local __mkit_debug_var_name__
42
+    for __mkit_debug_var_name__ in "$@";
43
+    do
44
+        {
45
+            echo -n "MKIT_DEBUG: ${FUNCNAME[1]}():"
46
+            echo -n " $__mkit_debug_var_name__"
47
+            echo -n "='${!__mkit_debug_var_name__}'"
48
+            echo
49
+        } >&2
50
+    done
51
+}
52
+
53
+die() {
54
+    #
55
+    # Exit with message and non-zero exit status
56
+    #
57
+    echo "fatal: $*" >&2
58
+    exit 4
59
+}
60
+
61
+_compver() {
62
+    #
63
+    # True if version $1 matches our version
64
+    #
65
+    # If our x is 0, check first two fragments, otherwise check just
66
+    # the x.  Fragments must equal.
67
+    #
68
+    local their_ver our_x our_y their_x their_y
69
+    their_ver="$1"
70
+    their_x=${their_ver%%.*}
71
+    their_y=${their_ver##$their_x.}
72
+    their_y=${their_y%%.*}
73
+    our_x=${MKIT_VERSION%%.*}
74
+    our_y=${MKIT_VERSION##$our_x.}
75
+    our_y=${our_y%%.*}
76
+    debug_var MKIT_VERSION our_x our_y their_ver their_x their_y
77
+    test "$their_x" -eq "$our_x" || return 1
78
+    test "$our_x" -eq 0 && {
79
+        test "$their_y" = "$our_y"
80
+        return $?
81
+    }
82
+    return 0
83
+}
84
+
85
+_chkiniversion() {
86
+    #
87
+    # Check if ini version is supported
88
+    #
89
+    # Look for "#mkit version=0.0.0" or similar in first or last
90
+    # 3 lines of the file; then check if the version is supported.
91
+    #
92
+    local ver_line
93
+    local their_ver
94
+    ver_line=$(
95
+        {
96
+            head -3 "$MKIT_INI"
97
+            tac "$MKIT_INI" | tail -3
98
+        } | grep -m 1 -E '^# *mkit +version *= *v?[0-9]+\.[0-9]+\.[0-9]+'
99
+    )
100
+    test -n "$ver_line" \
101
+     || die "version mark ('#mkit version=x.y.z') not found in: $MKIT_INI"
102
+    their_ver="$(tr -d '[:blank:]v' <<<"${ver_line##*=}")"
103
+    _compver "$their_ver" \
104
+     || die "bad mkit.ini version: $their_ver does not match $MKIT_VERSION"
105
+}
106
+
107
+mkit_init() {
108
+    #
109
+    # Do basic initialization
110
+    #
111
+    # Check for ini file and some variables
112
+    #
113
+    $MKIT_DRY && MKIT_DEBUG=true
114
+    MKIT_PROJ_PKGNAME=$(ini 1value "project:pkgname")
115
+    test -f "$MKIT_INI" || die "cannot find mkit.ini: $MKIT_INI"
116
+    _chkiniversion
117
+    test -n "$(tr -d '[:space:]' <<<"$MKIT_LOCAL")" \
118
+     || die "MKIT_LOCAL must be non-blank: '$MKIT_LOCAL'"
119
+}
120
+
121
+route() {
122
+    #
123
+    # Call correct function based on $1
124
+    #
125
+    if _valid_targets | grep -qwx "^$1";
126
+    then
127
+        "$1"
128
+    else
129
+        {
130
+            echo "usage: $(basename "$0") TARGET"
131
+            echo
132
+            echo "valid targets:"
133
+            _valid_targets | sed 's/^/    /'
134
+        } >&2
135
+    fi
136
+}
137
+
138
+warn() {
139
+    #
140
+    # Print warning message
141
+    #
142
+    echo "$@" >&2
143
+}

+ 163
- 0
utils/mkit/include/release.sh View File

@@ -0,0 +1,163 @@
1
+#!/bin/bash
2
+
3
+. "$MKIT_DIR/include/ini.sh"   || die "cannot import ini.sh"
4
+. "$MKIT_DIR/include/facts.sh" || die "cannot import facts.sh"
5
+
6
+_bump_version() {
7
+    #
8
+    # Bump version on stdin by level $1 (x, y or z)
9
+    #
10
+    local rlevel=$1
11
+    local old
12
+    read -r old
13
+    local oldx=${old%.*.*}
14
+    local oldz=${old#*.*.}
15
+    local tmpy=${old%.*}
16
+    local oldy=${tmpy#*.}
17
+    local new=""
18
+    case $rlevel in
19
+        x) new="$((oldx+MKIT_BUMPSIZE)).0.0"            ;;
20
+        y) new="$oldx.$((oldy+MKIT_BUMPSIZE)).0"        ;;
21
+        z) new="$oldx.$oldy.$((oldz+MKIT_BUMPSIZE))"    ;;
22
+        *) die "invalid release level: $1"  ;;
23
+    esac
24
+    echo "$new"
25
+}
26
+
27
+_relck() {
28
+    #
29
+    # Die if blocking condition $1 is detected
30
+    #
31
+    local condition="$1"
32
+    local x
33
+    case "$condition" in
34
+        git_present)
35
+            git rev-parse HEAD >&/dev/null\
36
+             || die "cannot do this outside git repo"
37
+            ;;
38
+        at_relsrc)
39
+            local relsrc=$(ini 1value project:relsrc)
40
+            git_fact current_branch \
41
+              | grep -qFx "$relsrc" \
42
+             || die "you are not on release source branch: $relsrc"
43
+            ;;
44
+        not_dirty)
45
+            git diff --shortstat 2>/dev/null \
46
+              | grep -q . \
47
+             && die "tree is dirty: $dirt"
48
+            ;;
49
+        tags_ok)
50
+            git_fact latest_tag \
51
+              | grep -q . \
52
+             || die "cannot find latest tag"
53
+            ;;
54
+        vbump_hot)
55
+            git diff-tree --no-commit-id --name-only -r HEAD \
56
+              | grep -qFx mkit.ini \
57
+             || die "last change must be version bump in mkit.ini"
58
+            ;;
59
+        no_wip)
60
+            git_fact reldiff \
61
+              | grep '^....... WIP ' \
62
+             && die "WIP commit since $(git_fact latest_tag)"
63
+            ;;
64
+        ini_version)
65
+            local oracle=$(git_fact latest_version | _bump_version "$rlevel")
66
+            ini 1value project:version  \
67
+              | grep -qFx "$oracle" \
68
+             || die "new version not in mkit.ini: $oracle"
69
+            ;;
70
+        *)
71
+            die "unknown release check: $condition"
72
+            ;;
73
+    esac
74
+}
75
+
76
+_release() {
77
+    #
78
+    # Prepare release
79
+    #
80
+    # Span release routines: make a signed tag, check branch
81
+    # and update release branch
82
+    #
83
+    # FIXME: Using project:prerl as build.sh:semver() does may not be
84
+    #        compatible with this release strategy
85
+    #
86
+    local rlevel=$1
87
+    local newtag
88
+    local reldst
89
+
90
+    _relck git_present
91
+    _relck at_relsrc
92
+    _relck not_dirty
93
+    _relck tags_ok
94
+    _relck vbump_hot
95
+    _relck no_wip
96
+    _relck ini_version
97
+
98
+    newtag=$(git_fact latest_version | _bump_version "$rlevel" | git_ver2tag )
99
+    set -e
100
+    debug_var newtag
101
+    $MKIT_DRY && return
102
+    git tag -m "$(ini 1value project:name) $newtag - $(ini 1value project:codename)" "$newtag"
103
+
104
+    reldst=$(ini 1value project:reldst)
105
+    debug_var reldst
106
+    if test -n "$reldst";
107
+    then
108
+        git branch -f "$reldst" "$newtag"
109
+    fi
110
+}
111
+
112
+_vbump() {
113
+    local rlevel="$1"
114
+    local nextver   # after the bump
115
+    _relck git_present
116
+    _relck at_relsrc
117
+    _relck not_dirty
118
+    nextver=$(ini 1value project:version | _bump_version "$rlevel")
119
+    debug_var nextver
120
+    $MKIT_DRY && return
121
+    update_version "$nextver" mkit.ini \
122
+      || die "failed to update version in mkit.ini"
123
+    git add mkit.ini \
124
+      || die "failed to add mkit.ini to the index"
125
+    git commit -e -m "$(_vbump_gitmsg)"
126
+}
127
+
128
+_vbump_gitmsg() {
129
+    echo "Bump version"
130
+    echo ""
131
+    echo "Overview of changes:"
132
+    echo ""
133
+    git_fact reldiff \
134
+      | sed '
135
+            s/^[a-f0-9]\{7\} / *  &/; t PATHS
136
+            s/^/        /
137
+            :PATHS
138
+        '
139
+}
140
+
141
+release_x() {
142
+    _release x
143
+}
144
+
145
+release_y() {
146
+    _release y
147
+}
148
+
149
+release_z() {
150
+    _release z
151
+}
152
+
153
+vbump_x() {
154
+    _vbump x
155
+}
156
+
157
+vbump_y() {
158
+    _vbump y
159
+}
160
+
161
+vbump_z() {
162
+    _vbump z
163
+}

+ 82
- 0
utils/mkit/make View File

@@ -0,0 +1,82 @@
1
+#!/bin/bash
2
+# mkit - simple install helper
3
+# See LICENSE file for copyright and license details.
4
+
5
+die() {
6
+    echo "$@" && exit 9
7
+}
8
+
9
+
10
+#
11
+# Bump size (for vbump_? and release_?)
12
+#
13
+MKIT_BUMPSIZE=${MKIT_BUMPSIZE:-1}
14
+
15
+#
16
+# Debug mode (true|false)
17
+#
18
+MKIT_DEBUG=${MKIT_DEBUG:-false}
19
+
20
+#
21
+# Default deploy mode for files
22
+#
23
+MKIT_DEFAULT_MODE="644"
24
+
25
+#
26
+# Path to MKit dir (where 'include' is)
27
+#
28
+MKIT_DIR=${MKIT_DIR:-$(dirname "$0")}
29
+
30
+#
31
+# Dry mode (true|false)
32
+#
33
+# Set to true to not install anything. Implies MKIT_DEBUG.
34
+#
35
+MKIT_DRY=${MKIT_DRY:-false}
36
+
37
+#
38
+# Path to mkit.ini
39
+#
40
+MKIT_INI=${MKIT_INI:-mkit.ini}
41
+
42
+#
43
+# Limit ini expansion depth
44
+#
45
+# To avoid endless loops, this value is subtracted each
46
+# time ini() expands a reference; when zero is reached,
47
+# no more expansions happen.
48
+#
49
+MKIT_INI_EXPAND=2
50
+
51
+#
52
+# Path to MKit local config and temp
53
+#
54
+# Typically hidden in project root, here MKit can
55
+# save its temporary lists.
56
+#
57
+MKIT_LOCAL=${MKIT_LOCAL:-.mkit}
58
+
59
+#
60
+# Package name
61
+#
62
+# Used as base for tarball and in some default tokens.
63
+#
64
+MKIT_PROJ_PKGNAME=""
65
+
66
+#
67
+# This MKit version
68
+#
69
+MKIT_VERSION=0.0.17
70
+
71
+
72
+. "$MKIT_DIR/include/mkit.sh" || die "failed to init; check if MKIT_DIR is set properly: $MKIT_DIR"
73
+
74
+case "$1" in
75
+    --version-semver) echo "$MKIT_VERSION"; exit 0 ;;
76
+    --version)        echo "Mkit (Simple Makefile target helper) $MKIT_VERSION"
77
+                      exit 0 ;;
78
+esac
79
+
80
+mkit_init
81
+
82
+route "$@"

+ 50
- 0
utils/mkit/mkit.mk View File

@@ -0,0 +1,50 @@
1
+# mkit - Simple Makefile target helper
2
+# See LICENSE file for copyright and license details.
3
+
4
+export MKIT_DIR
5
+
6
+all: build
7
+
8
+build:
9
+	@$(MKIT_DIR)/make build
10
+
11
+manpages: build
12
+	@$(MKIT_DIR)/make build_manpages
13
+
14
+clean:
15
+	@$(MKIT_DIR)/make clean
16
+
17
+debstuff: dist
18
+	@$(MKIT_DIR)/make debstuff
19
+
20
+dist: clean
21
+	@$(MKIT_DIR)/make dist
22
+
23
+rpmstuff: dist
24
+	@$(MKIT_DIR)/make rpmstuff
25
+
26
+install: all
27
+	@$(MKIT_DIR)/make install
28
+
29
+release_x:
30
+	@$(MKIT_DIR)/make release_x
31
+
32
+release_y:
33
+	@$(MKIT_DIR)/make release_y
34
+
35
+release_z:
36
+	@$(MKIT_DIR)/make release_z
37
+
38
+uninstall:
39
+	@$(MKIT_DIR)/make uninstall
40
+
41
+vbump_x:
42
+	@$(MKIT_DIR)/make vbump_x
43
+
44
+vbump_y:
45
+	@$(MKIT_DIR)/make vbump_y
46
+
47
+vbump_z:
48
+	@$(MKIT_DIR)/make vbump_z
49
+
50
+.PHONY: all clean dist rpmstuff install uninstall release_x release_y release_z vbump_x vbump_y vbump_z