common.sh 5.81 KB
Newer Older
1 2
#!/bin/sh

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

6 7 8 9 10 11
fatal()
{
    echo "FATAL: $*" >&2
    exit 1
}

12 13 14
# check if <arg> is a real command
is_command()
{
15
    epm tool which "$1" >/dev/null
16 17
}

18 19 20 21 22
eget()
{
    epm tool eget "$@"
}

23 24
[ -n "$BIGTMPDIR" ] || [ -d "/var/tmp" ] && BIGTMPDIR="/var/tmp" || BIGTMPDIR="/tmp"

25 26
cd_to_temp_dir()
{
27
    PKGDIR=$(mktemp -d --tmpdir=$BIGTMPDIR)
28 29
    trap "rm -fr $PKGDIR" EXIT
    cd $PKGDIR || fatal
30 31
}

32 33 34
is_supported_arch()
{
    local i
35 36

    # skip checking if there are no arches
37
    [ -n "$SUPPORTEDARCHES" ] || return 0
38 39
    [ -n "$1" ] || return 0

40 41 42 43 44 45
    for i in $SUPPORTEDARCHES ; do
        [ "$i" = "$1" ] && return 0
    done
    return 1
}

46

47 48
get_latest_version()
{
49
    local epmver="$(epm --short --version)"
50 51
    local URL="https://eepm.ru/releases/$epmver/app-versions"
    if ! eget -q -O- "$URL/$1" ; then
52
        URL="https://eepm.ru/app-versions"
53
        eget -q -O- "$URL/$1"
54
    fi
55 56
}

57 58 59 60 61 62 63
print_product_alt()
{
    [ -n "$1" ] || return
    shift
    echo "$*"
}

64 65 66 67 68
get_pkgvendor()
{
    epm print field Vendor for package $1
}

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
check_alternative_pkgname()
{
    [ -n "$BASEPKGNAME" ] || BASEPKGNAME="$PKGNAME"

    local i
    for i in $PRODUCTALT ; do
        if [ "$i" = "''" ] ; then
            PKGNAME=$BASEPKGNAME
            continue
        fi
        if [ "$VERSION" = "$i" ] ; then
            PKGNAME=$BASEPKGNAME-$i
            VERSION=""
            return
        fi
    done
    # when VERSION is not in PRODUCTALT
    for i in $PRODUCTALT ; do
        [ "$i" = "''" ] && continue
        if epm installed $BASEPKGNAME-$i ; then
            PKGNAME=$BASEPKGNAME-$i
            break
        fi
    done
}

# support direct run the script
if [ -x "../bin/epm" ] ; then
    export PATH="$(realpath ../bin):$PATH"
fi

if [ -z "$SUDO" ] && [ "$UID" != "0" ] ; then
    SUDO="sudo"
fi


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

108 109 110 111 112
case "$1" in
    "--remove")
        epm remove $PKGNAME
        exit
        ;;
113
    "--info")
114 115
        if [ -n "$PRODUCTALT" ] ; then
            echo "Help about additional parameters."
116
            echo "Use epm play $(basename $0 .sh) [= $(echo "$PRODUCTALT" | sed -e 's@ @|@g')]"
117 118
        fi
        [ -n "$TIPS" ] && echo "$TIPS"
119
        [ -n "$URL" ] && echo "Url: $URL"
120 121
        exit
        ;;
122
    "--package-name")
123
        [ -n "$DESCRIPTION" ] || exit 0
124 125 126
        echo "$PKGNAME"
        exit
        ;;
127 128 129 130
    "--product-alternatives")
        print_product_alt $PRODUCTALT
        exit
        ;;
131 132 133 134
    "--installed")
        epm installed $PKGNAME
        exit
        ;;
135 136 137 138
    "--installed-version")
        epm print version for package $PKGNAME
        exit
        ;;
139
    "--description")
140
        is_supported_arch "$2" || exit 0
141 142 143 144 145 146 147 148
        echo "$DESCRIPTION"
        exit
        ;;
    "--update")
        if ! epm installed $PKGNAME ; then
            echo "Skipping update of $PKGNAME (package is not installed)"
            exit
        fi
149

150 151 152 153
        if epm mark showhold | grep -q "^$PKGNAME$" ; then
            echo "Skipping update of $PKGNAME (package is on hold, see '# epm mark showhold')"
            exit
        fi
154

155
        pkgver="$(epm print version for package $PKGNAME)"
156 157 158 159 160 161 162
        latestpkgver="$(get_latest_version $PKGNAME)"
        # ignore update if have no latest package version or the latest package version no more than installed one
        if [ -n "$pkgver" ] ; then
            if [ -z "$latestpkgver" ] ; then
                echo "Can't get info about latest version of $PKGNAME, so skip updating installed version $pkgver."
                exit
            fi
163
            # latestpkgver <= $pkgver
164
            if [ -z "$force" ] && [ "$(epm print compare package version $latestpkgver $pkgver)" != "1" ] ; then
165
                echo "Latest available version of $PKGNAME: $latestpkgver. Installed version: $pkgver."
166 167
                exit
            fi
168

169
            if [ -n "$force" ] ; then
170 171 172 173 174
                echo "Updating $PKGNAME from $pkgver to latest available version ..."
            else
                echo "Updating $PKGNAME from $pkgver to $latestpkgver version ..."
                VERSION="$latestpkgver"
            fi
175
        fi
176
        # pass to run play code
177 178
        ;;
    "--run")
179
        # just pass to run play code
180 181
        ;;
    *)
182
        fatal "Unknown command '$1'. Use this script only via epm play."
183 184
        ;;
esac
185

186
# --update/--run
187

188
is_supported_arch "$(epm print info -a)" || fatal "Only '$SUPPORTEDARCHES' architectures is supported"
189 190 191 192 193

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

check_tty

194 195 196 197 198 199 200 201
__showcmd_shifted()
{
    local s="$1"
    shift
    shift $s
    showcmd "$*"
}

202 203 204 205
# add to all epm calls
EPM="$(epm tool which epm)" || fatal
epm()
{
206 207
    #if [ "$1" = "tool" ] ; then
    #    __showcmd_shifted 1 "$@"
208
    if [ "$1" != "print" ] && [ "$1" != "tool" ] && [ "$1" != "status" ] ; then
209 210 211 212
        showcmd "$(basename $EPM) $*"
    fi
    $EPM "$@"
}
213 214 215 216 217 218 219 220 221

is_repacked_package()
{
    local pkg="$1"
    [ -n "$pkg" ] || pkg="$PKGNAME"
    [ -n "$pkg" ] || fatal "is_repacked_package() is called without package name"

    epm status --installed $pkg || return 0

222
    [ -n "$force" ] && return 0
223 224 225 226 227 228

    if epm status --original $pkg ; then
       echo "Package $pkg is already installed from ALT repository."
       return 1
    fi

229
    if epm status --thirdparty $pkg ; then
230 231 232 233 234 235 236 237 238 239 240 241
       echo "Package $pkg is already installed, packaged by vendor $(epm print field Distribution 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
}

242

243 244
# skip install if there is package installed not via epm play
is_repacked_package $REPOPKGNAME || exit 0
245

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