fritzkink
2022-09-26 1138c15043733495d4da2ca36fe10aaec21e037b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#! /usr/bin/ksh
#
#
# This file and its contents are supplied under the terms of the
# Common Development and Distribution License ("CDDL"), version 1.0.
# You may only use this file in accordance with the terms of version
# 1.0 of the CDDL.
#
# A full copy of the text of the CDDL should have accompanied this
# source.  A copy of the CDDL is also available via the Internet at
# http://www.illumos.org/license/CDDL.
#
 
#
# Copyright 2022 Marcel Telka
#
 
 
function usage
{
    [[ -n "$1" ]] && printf "ERROR: %s\n\n" "$1"
    printf "Usage: perl-version-convert DISTRIBUTION VERSION\n" >&2
    [[ -n "$1" ]] && exit 1
    exit 0
}
 
 
(($# == 0)) && usage
(($# == 1)) && usage "Missing VERSION to convert"
(($# > 2)) && usage "Too many arguments"
 
DISTRIBUTION="$1"
VERSION="$2"
 
 
function unsupported_version
{
    typeset -n C=$1
    typeset VERSION=$2
 
    printf "ERROR: Unsupported version format: %s\n" "$VERSION" >&2
    C="UNSUPPORTED_VERSION_FORMAT_$VERSION"
}
 
function convert_version
{
    typeset -n C=$1
    typeset DIST=$2
    typeset VERSION=$3
 
    typeset -a VER
 
    #
    # Handle special cases
    #
    # Notes:
    #
    # (*) The version format for Authen-PAM is 0.XX, but it was integrated
    # as XX, so we need to follow that.
    #
    # (*) The actual version format for Crypt-PBKDF2 is 0.YYDDDR (YY -
    # year, DDD - day in year, R - release in the day) so it had to be
    # converted to 0.YY.DDD.R to be more accurate, but it was already
    # integrated as 0.YYDDDR, so we need to follow it for now, because
    # 0.YY.DDD.R would be considered older than 0.YYDDDR.  Possible
    # conversion of 0.YYDDDR to YY.DDD.R would cause problem in a case the
    # Crypt-PBKDF2 bump their major version.
    #
    # (*) Module-Build uses apparently version format 0.XXYY so it had to
    # be converted to 0.XX.YY, but it was already integrated as 0.XXYY, so
    # we need to follow it for now.
    #
    [[ "$DIST" == "Authen-PAM" && "${VERSION:0:2}" == "0." ]] && ((${#VERSION} == 4)) && VERSION=$((1${VERSION:2:2} - 100))
    [[ "$DIST" == "Crypt-PBKDF2" && "${VERSION:0:2}" == "0." ]] && ((${#VERSION} == 8)) && VER[1]=$((1${VERSION:2:6} - 1000000)) && VERSION=${VERSION:0:1}
    [[ "$DIST" == "Email-Sender" && "${VERSION:0:2}" == "1." ]] && ((${#VERSION} == 8)) && VER[1]=${VERSION:2:1} && VER[2]=$((1${VERSION:3:5} - 100000)) && VERSION=${VERSION:0:1}
    [[ "$DIST" == "Email-Sender" && "${VERSION:0:2}" != "0." && "${VERSION:3:2}" == "00" ]] && ((${#VERSION} == 5)) && VER[1]=${VERSION:2:1} && VERSION=${VERSION:0:1}
    [[ "$DIST" == "Error" && "${VERSION:0:2}" == "0." ]] && ((${#VERSION} == 7)) && VER[2]=$((1${VERSION:4:3} - 1000)) && VERSION=${VERSION:0:4}
    [[ "$DIST" == "Mail-AuthenticationResults" && "${VERSION:0:2}" == "1." ]] && ((${#VERSION} == 10)) && VER[1]=$((1${VERSION:2:4} - 10000)) && VER[2]=$((1${VERSION:6:2} - 100)) && VER[3]=$((1${VERSION:8:2} - 100)) && VERSION=${VERSION:0:1}
    [[ "$DIST" == "Mail-AuthenticationResults" && "${VERSION:0:2}" == "1." ]] && ((${#VERSION} == 12)) && VER[1]=$((1${VERSION:2:4} - 10000)) && VER[2]=$((1${VERSION:6:2} - 100)) && VER[3]=$((1${VERSION:8:2} - 100)) && VER[4]=${VERSION:11:1} && VERSION=${VERSION:0:1}
    [[ "$DIST" == "Mail-AuthenticationResults" && "${VERSION:0:2}" == "2." ]] && ((${#VERSION} == 10)) && VER[1]=$((1${VERSION:2:4} - 10000)) && VER[2]=$((1${VERSION:6:2} - 100)) && VER[3]=$((1${VERSION:8:2} - 100)) && VERSION=${VERSION:0:1}
    [[ "$DIST" == "Mail-AuthenticationResults" && "${VERSION:0:2}" == "2." ]] && ((${#VERSION} == 12)) && VER[1]=$((1${VERSION:2:4} - 10000)) && VER[2]=$((1${VERSION:6:2} - 100)) && VER[3]=$((1${VERSION:8:2} - 100)) && VER[4]=${VERSION:11:1} && VERSION=${VERSION:0:1}
    [[ "$DIST" == "Mail-DKIM" && "${VERSION:0:2}" == "1." ]] && ((${#VERSION} == 10)) && VER[1]=$((1${VERSION:2:4} - 10000)) && VER[2]=$((1${VERSION:6:2} - 100)) && VER[3]=$((1${VERSION:8:2} - 100)) && VERSION=${VERSION:0:1}
    [[ "$DIST" == "Mail-DKIM" && "${VERSION:0:2}" == "1." ]] && ((${#VERSION} == 12)) && VER[1]=$((1${VERSION:2:4} - 10000)) && VER[2]=$((1${VERSION:6:2} - 100)) && VER[3]=$((1${VERSION:8:2} - 100)) && VER[4]=${VERSION:11:1} && VERSION=${VERSION:0:1}
    [[ "$DIST" == "Module-Build" ]] && ((${#VERSION} == 6)) && VER[1]=$((1${VERSION:2:4} - 10000)) && VERSION=${VERSION:0:1}
    [[ "$DIST" == "Mozilla-CA" ]] && ((${#VERSION} == 8)) && VER[1]=$((1${VERSION:4:2} - 100)) && VER[2]=$((1${VERSION:6:2} - 100)) && VERSION=${VERSION:0:4}
    [[ "$DIST" == "Net-DNS-Resolver-Mock" && "${VERSION:0:2}" == "1." ]] && ((${#VERSION} == 10)) && VER[1]=$((1${VERSION:2:4} - 10000)) && VER[2]=$((1${VERSION:6:2} - 100)) && VER[3]=$((1${VERSION:8:2} - 100)) && VERSION=${VERSION:0:1}
    [[ "$DIST" == "PkgConfig" && "${VERSION:4:3}" == "026" ]] && ((${#VERSION} == 7)) && VER[1]=$((1${VERSION:2:2} - 100)) && VERSION=${VERSION:0:1}
 
    C=
    VER[0]=${VERSION%%.*}
 
    if [[ "${VER[0]}" != "$VERSION" ]] ; then
        V=${VERSION#*.}
 
        # Currently we do not support underscore in version
        V=${V//*_*}
 
        case "$V" in
        ?)    VER[1]=$V ;;
        ??)    VER[1]=$((1$V - 100)) ;;
        ?.?)    VER[1]=${V:0:1} && VER[2]=${V:2:1} ;;
        ???)    VER[1]=$((1$V - 1000)) ;;
        ?.??)    VER[1]=${V:0:1} && VER[2]=$((1${V:2:2} - 100)) ;;
        ??.?)    VER[1]=$((1${V:0:2} - 100)) && VER[2]=${V:3:1} ;;
        ????)    VER[1]=$((1${V:0:2} - 100)) && VER[2]=$((1${V:2:2} - 100)) ;;
        ?.????)    unsupported_version C $VERSION ; return ;;
        ??.???)    unsupported_version C $VERSION ; return ;;
        ???.??)    unsupported_version C $VERSION ; return ;;
        ????.?)    unsupported_version C $VERSION ; return ;;
        ??????)    VER[1]=$((1${V:0:3} - 1000)) ; VER[2]=$((1${V:3:3} - 1000)) ;;
        *)    unsupported_version C $VERSION ; return ;;
        esac
    fi
 
    i=0
    while ((i < ${#VER[@]})) ; do
        ((i > 0)) && C="$C."
        C="$C${VER[$i]}"
        i=$((i + 1))
    done
}
 
 
# For convenience we replace '::' by '-' in a case somebody calls this script
# with module name instead of distribution name.
convert_version VER "${DISTRIBUTION//::/-}" "$VERSION"
printf "%s\n" "$VER"