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