You need to sign in or sign up before continuing.
common.sh 12.4 KB
Newer Older
1 2
#!/bin/sh

3 4
# kind of hack: inheritance --force from main epm
echo "$EPM_OPTIONS" | grep -q -- "--force" && force="--force"
5
echo "$EPM_OPTIONS" | grep -q -- "--auto" && auto="--auto"
6 7
echo "$EPM_OPTIONS" | grep -q -- "--latest" && latest="true"

8

9 10 11 12 13 14
fatal()
{
    echo "FATAL: $*" >&2
    exit 1
}

15 16 17 18 19 20 21 22 23
[ -n "$BIGTMPDIR" ] || [ -d "/var/tmp" ] && BIGTMPDIR="/var/tmp" || BIGTMPDIR="/tmp"

cd_to_temp_dir()
{
    PKGDIR=$(mktemp -d --tmpdir=$BIGTMPDIR)
    trap "rm -fr $PKGDIR" EXIT
    cd $PKGDIR || fatal
}

24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
# print a path to the command if exists in $PATH
if a= type -a type 2>/dev/null >/dev/null ; then
print_command_path()
{
    a= type -fpP -- "$1" 2>/dev/null
}
elif a= which which 2>/dev/null >/dev/null ; then
    # the best case if we have which command (other ways needs checking)
    # TODO: don't use which at all, it is a binary, not builtin shell command
print_command_path()
{
    a= which -- "$1" 2>/dev/null
}
else
print_command_path()
{
    a= type "$1" 2>/dev/null | sed -e 's|.* /|/|'
}
fi

44 45 46
# check if <arg> is a real command
is_command()
{
47
    print_command_path "$1" >/dev/null
48 49
}

50 51 52 53 54
is_url()
{
    echo "$1" | grep -q "^[filehtps]*:/"
}

55 56 57 58 59 60 61 62 63

#__showcmd_shifted()
#{
#    local s="$1"
#    shift
#    shift $s
#    showcmd "$*"
#}

64 65 66 67
# support for direct run a play script
if [ -x "../bin/epm" ] ; then
    export PATH="$(realpath ../bin):$PATH"
fi
68 69

# add to all epm calls
70 71
#EPM="$(epm tool which epm)" || fatal
EPM="$(print_command_path epm)" || fatal
72
epm()
73
{
74 75 76 77 78 79
    #if [ "$1" = "tool" ] ; then
    #    __showcmd_shifted 1 "$@"
    if [ "$1" != "print" ] && [ "$1" != "tool" ] && [ "$1" != "status" ] ; then
        showcmd "$(basename $EPM) $*"
    fi
    $EPM "$@"
80 81
}

82

83
eget()
84
{
85
    epm tool eget "$@"
86 87
}

88

89 90 91
is_supported_arch()
{
    local i
92 93

    # skip checking if there are no arches
94
    [ -n "$SUPPORTEDARCHES" ] || return 0
95 96
    [ -n "$1" ] || return 0

97 98 99 100 101 102
    for i in $SUPPORTEDARCHES ; do
        [ "$i" = "$1" ] && return 0
    done
    return 1
}

103 104 105 106
warn_version_is_not_supported()
{
    [ "$VERSION" = "*" ] || echo -e "\nWarning: Specifying the version is not supported by vendor. Downloading latest version ...\n"
}
107

108 109 110 111 112 113
override_pkgname()
{
    PKGNAME="$1"
    export EEPM_INTERNAL_PKGNAME="$PKGNAME"
}

114 115 116 117 118 119 120 121 122 123 124 125
# epm install $PKGURL
install_pkgurl()
{
    local repack=''
    local pkgtype="$(epm print info -p)"

    # we have workaround for their postinstall script, so always repack rpm package
    # TODO: repack for deb systems too
    [ "$pkgtype" = "rpm" ] && repack='--repack'

    [ -n "$PKGURL" ] || fatal "Can't get package URL"

126
    epm install $repack $PKGURL "$@" || exit
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
}

# epm pack --install $PKGNAME "$PKGURL"
install_pack_pkgurl()
{
    local repack=''
    local pkgtype="$(epm print info -p)"

    # we have workaround for their postinstall script, so always repack rpm package
    # TODO: repack for deb systems too
    [ "$pkgtype" = "rpm" ] && repack='--repack'

    [ -n "$PKGURL" ] || fatal "Can't get package URL"

    epm pack $repack --install $PKGNAME "$PKGURL" "$@"
}

144 145
snap_get_pkgurl()
{
146
    local SNAPNAME="$1"
147 148 149 150 151 152 153 154
    case "$(epm print info -a)" in
        x86_64)
            local ARCH="amd64" ;;
        aarch64)
            local ARCH="arm64" ;;
        *)
            local ARCH="$(epm print info -a)" ;;
    esac
155
    is_url "$SNAPNAME" && SNAPNAME="$(basename "$SNAPNAME")"
156
    eget -O- -H Snap-Device-Series:16 https://api.snapcraft.io/v2/snaps/info/$SNAPNAME | epm --inscript tool json -b | grep -A 8 "$ARCH" | grep '\["channel-map",[0-9],"download","url"\]' | head -n1 | sed 's/.*"\(https:\/\/.*\)".*/\1/'
157
}
158

159
# return version only for the first package
160 161
get_latest_version()
{
162
    local ver
163
    local epmver="$(epm --short --version)"
164
    local URL
165
    epmver=$(echo "$epmver" | sed -e 's|\.[0-9]*$||')
166
    for URL in "https://eepm.ru/releases/$epmver/app-versions" "https://eepm.ru/app-versions" ; do
167 168
        ver="$(eget -q -O- "$URL/$1")" || continue
        ver="$(echo "$ver" | head -n1 | cut -d" " -f1)"
169 170
        [ -n "$ver" ] && echo "$ver" && return
    done
171 172
}

173 174 175 176 177 178 179 180
get_github_version()
{
    local url="$1"
    local user_and_repo=${url#https://github.com/}
    local user_and_repo=${user_and_repo%/}
    local asset_name="$2"

    if [ "$3" == "prerelease" ] ; then
181
        eget -O- "https://api.github.com/repos/${user_and_repo}/releases" | grep 'browser_download_url' | grep -iEo 'https.*download.*' | grep "$2" | head -n1
182
    else
183
        eget -O- "https://api.github.com/repos/${user_and_repo}/releases" \
184 185 186 187 188 189 190 191 192 193 194
        | awk '{
    if ($0 ~ /"prerelease": false/) {
        prerelease = 0;
    } else if ($0 ~ /"prerelease": true/) {
        prerelease = 1;
    }
    if (!prerelease && $0 ~ /"browser_download_url":/) {
        match($0, /"browser_download_url": "(https[^"]*)"/, arr);
        print arr[1];
    }
}' | grep "$2" | head -1
195 196 197 198
    fi

}

199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
get_github_tag()
{
    local url="$1"
    local user_and_repo=${url#https://github.com/}
    local user_and_repo=${user_and_repo%/}

    if [ "$2" == "prerelease" ] ; then
        eget -O- "https://api.github.com/repos/${user_and_repo}/releases" | grep -oP '"tag_name": *"[^"]*' | grep -oP '[0-9]+(\.[0-9]+)*(-[0-9]+)?' | head -n1
    else
        eget -O- "https://api.github.com/repos/${user_and_repo}/releases" \
        | awk '{
    if ($0 ~ /"prerelease": false/) {
        prerelease = 0;
    } else if ($0 ~ /"prerelease": true/) {
        prerelease = 1;
    }
    if (!prerelease && $0 ~ /"tag_name":/) {
        match($0, /"tag_name": "([^"]*)"/, arr);
        print arr[1];
    }
}' | grep -oP '[0-9]+(\.[0-9]+)*(-[0-9]+)?' | head -n1
    fi

}

224 225 226 227 228 229 230
print_product_alt()
{
    [ -n "$1" ] || return
    shift
    echo "$*"
}

231 232
get_pkgvendor()
{
233
    epm print field Vendor for package "$1"
234 235
}

236 237 238

is_pkg_enough()
{
239 240
    local PKG="$1"
    local needed="$2"
241 242
    # epm print enough package version "$PKG" "$needed"
    epm status --installed "$PKG" "$needed"
243 244
}

245 246 247 248 249 250
# arg: minimal require of libstdc++ version
# return true is we have such version
is_stdcpp_enough()
{
    local needed="$1"
    local STDCPKG="libstdc++"
251
    epm status --installed $STDCPKG || STDCPKG="libstdc++6"
252

253 254 255 256 257 258 259
    is_pkg_enough $STDCPKG $needed
}

is_glibc_enough()
{
    local needed="$1"
    local PKG="glibc-core"
260
    epm status --installed $PKG || PKG="glibc"
261 262 263 264 265 266 267 268 269 270

    is_pkg_enough $PKG $needed
}

is_openssl_enough()
{
    local needed="$1"
    local PKG="openssl"

    is_pkg_enough $PKG $needed
271 272 273
}


274 275 276 277 278
get_first()
{
    echo "$1"
}

279 280 281
check_alternative_pkgname()
{
    [ -n "$BASEPKGNAME" ] || BASEPKGNAME="$PKGNAME"
282
    [ -n "$BASEPKGNAME" ] || return
283

284
    # default: with first entry in $PEODUCTALT
285 286
    BRANCH=$(get_first $PRODUCTALT)
    PKGNAME="$BASEPKGNAME-$BRANCH"
287 288

    # override with VERSION
289 290 291 292 293 294 295 296
    local i
    for i in $PRODUCTALT ; do
        if [ "$i" = "''" ] ; then
            PKGNAME=$BASEPKGNAME
            continue
        fi
        if [ "$VERSION" = "$i" ] ; then
            PKGNAME=$BASEPKGNAME-$i
297
            BRANCH="$i"
298 299 300 301
            VERSION=""
            return
        fi
    done
302 303

    # when VERSION is not in PRODUCTALT, check installed package
304
    for i in $PRODUCTALT ; do
305 306 307
        if [ "$i" = "''" ] ; then
            continue
        fi
308
        if epm status --installed $BASEPKGNAME-$i ; then
309
            PKGNAME=$BASEPKGNAME-$i
310
            BRANCH="$i"
311 312 313 314 315
            break
        fi
    done
}

316 317 318 319 320 321
is_repacked_package()
{
    local pkg="$1"
    [ -n "$pkg" ] || pkg="$PKGNAME"
    [ -n "$pkg" ] || return 0 #fatal "is_repacked_package() is called without package name"

322 323
    epm status --installed $pkg || return 0

324
    epm status --supported || return 1
325 326 327 328

    [ -n "$force" ] && return 0

    if epm status --original $pkg ; then
329
       echo "Package $pkg is already installed from repository (use --force to override it)."
330 331 332 333 334 335 336 337 338
       return 1
    fi

    if epm status --certified $pkg ; then
       # allow install/update if we agreed with their package
       return 0
    fi

    if epm status --thirdparty $pkg ; then
339
       [ -n "$SKIPREPACK" ] && return 0
340 341 342 343 344 345 346 347 348 349 350 351
       echo "Package $pkg is already installed, packaged by vendor $(epm print field Vendor for $pkg)."
       return 1
    fi

    if ! epm status --repacked $pkg ; then
       echo "Package $pkg is already installed (possible, manually packed)."
       return 1
    fi

    return 0
}

352 353 354 355 356 357 358 359 360 361 362
is_repacked_packages()
{
    local pkg
    local pkgs="$1"
    [ -n "$pkgs" ] || pkgs="$PKGNAME"
    [ -n "$pkgs" ] || return 0 #fatal "is_repacked_package() is called without package name"
    for pkg in $pkgs ; do
        is_repacked_package $pkg || return
    done
}

363 364 365 366 367 368 369 370 371 372 373 374 375 376
check_for_product_update()
{
    # HACK: check version only by first package (we assume all packages have the same version)
    pkgver="$(epm print version for package $PKGNAME | head -n1)"
    latestpkgver="$(get_latest_version $PKGNAME)"

    # ignore update if have no latest package version or the latest package version no more than installed one
    [ -n "$pkgver" ] || return

    if [ -z "$latestpkgver" ] ; then
        echo "Can't get info about latest version of $PKGNAME, so skip updating installed version $pkgver."
        exit
    fi
    # latestpkgver <= $pkgver
377 378 379 380 381 382 383 384
    if [ -z "$force" ] || [ -z "$latest" ] ; then
        if [ "$(epm print compare package version $latestpkgver $pkgver)" != "1" ] ; then
            if [ "$latestpkgver" = "$pkgver" ] ; then
                echo "Latest available version of $PKGNAME $latestpkgver is already installed."
            else
                echo "Latest available version of $PKGNAME: $latestpkgver, but you have a newer version installed: $pkgver."
            fi
            exit
385 386 387 388
        fi
    fi

    #echo "Updating $PKGNAME from $pkgver to the latest available version (equal to $latestpkgver or newer) ..."
389
    if [ -n "$force" ] || [ -z "$latest" ] ; then
390 391 392 393 394 395 396
        echo "Updating $PKGNAME from $pkgver to latest available version ..."
    else
        echo "Updating $PKGNAME from $pkgver to $latestpkgver version ..."
        VERSION="$latestpkgver"
    fi
}

397

398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414
case "$1" in
    "--description")
        is_supported_arch "$2" || exit 0
        echo "$DESCRIPTION"
        exit
        ;;
    "--product-alternatives")
        print_product_alt $PRODUCTALT
        exit
        ;;
esac


. $(dirname $0)/common-outformat.sh

check_tty

415 416 417
pkgtext="package"
pkgistext="package is"
epm tool estrlist has_space "$PKGNAME" 2>/dev/null && pkgtext="packages" && pkgistext="packages are"
418 419 420 421

# set PKGNAME to $BASEPKGNAME-$VERSION if $VERSION is found in PRODUCTALT
[ -n "$PRODUCTALT" ] && check_alternative_pkgname

422 423
case "$1" in
    "--remove")
424
        #is_repacked_packages || exit 0
425 426 427
        epm remove $PKGNAME
        exit
        ;;
428
    "--info")
429 430
        if [ -n "$PRODUCTALT" ] ; then
            echo "Help about additional parameters."
431
            echo "Use epm play $(basename $0 .sh) [= $(echo "$PRODUCTALT" | sed -e 's@ @|@g')]"
432 433
        fi
        [ -n "$TIPS" ] && echo "$TIPS"
434
        [ -n "$URL" ] && echo "Url: $URL"
435 436
        exit
        ;;
437
    "--package-name")
438
        #[ -n "$DESCRIPTION" ] || exit 0
439 440 441 442
        echo "$PKGNAME"
        exit
        ;;
    "--installed")
443
        #epm installed $PKGNAME
444
        is_repacked_packages $PKGNAME
445 446
        exit
        ;;
447
    "--installed-version")
448
        epm print version for package $PKGNAME | head -n1
449 450
        exit
        ;;
451 452
    "--update")
        if ! epm installed $PKGNAME ; then
453
            echo "Skipping update of $PKGNAME ($pkgistext not installed)"
454 455
            exit
        fi
456

457 458
        if epm mark checkhold $PKGNAME ; then
            echo "Skipping update of $PKGNAME ($pkgistext on hold, see '# epm mark showhold')"
459 460
            exit
        fi
461

462
        check_for_product_update
463
        # pass to run play code
464 465
        ;;
    "--run")
466
        # just pass to run play code
467 468
        ;;
    *)
469
        fatal "Unknown command '$1'. Use this script only via epm play."
470 471
        ;;
esac
472

473
# --update/--run
474

475
is_supported_arch "$(epm print info -a)" || fatal "Only '$SUPPORTEDARCHES' architectures is supported"
476

477
#epm tool estrlist has_space "$PKGNAME" && fatal "play.d/common does not support a new packages in PKGNAME at all!"
478

479
# skip install if there is package installed not via epm play
480
is_repacked_packages $REPOPKGNAME || exit 0
481

482
if [ -z "$VERSION" ] && [ -z "$force" ] && [ -n "$EGET_IPFS_DB" ] && [ -z "$latest" ] ; then
483 484 485 486
    # IPFS is using, use known version
    VERSION="$(get_latest_version $PKGNAME)"
fi

487
if [ -z "$VERSION" ] && [ -z "$force" ]  && [ -z "$latest" ] ; then
488
    # by default use latest known version to install
489 490 491
    VERSION="$(get_latest_version $PKGNAME)"
fi

492 493
# default version value (can be overrided with arg $2 or by update)
[ -n "$VERSION" ] || VERSION="*"
494 495

echo
496
echo "Installing $DESCRIPTION as $PKGNAME $pkgtext ..."
497 498

export EEPM_INTERNAL_PKGNAME="$PKGNAME"