common.sh 2.25 KB
Newer Older
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1 2 3 4 5 6 7 8
#!/bin/sh

fatal()
{
    echo "FATAL: $*" >&2
    exit 1
}

9 10 11 12 13 14 15


# compatibility layer

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

Vitaly Lipatov's avatar
Vitaly Lipatov committed
19
# compatibility layer
20

Vitaly Lipatov's avatar
Vitaly Lipatov committed
21
# add realpath if missed
22
if ! is_command realpath ; then
Vitaly Lipatov's avatar
Vitaly Lipatov committed
23 24 25 26 27 28 29 30
realpath()
{
    [ -n "$*" ] || return
    readlink -f "$@"
}
fi

# add subst if missed
31
if ! is_command subst ; then
Vitaly Lipatov's avatar
Vitaly Lipatov committed
32 33 34 35 36 37
subst()
{
    sed -i -e "$@"
}
fi

38 39 40 41
eget()
{
    epm tool eget "$@"
}
42

43 44 45 46 47
erc()
{
    epm tool erc "$@"
}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
48 49 50 51 52
is_abs_path()
{
    echo "$1" | grep -q "^/"
}

53 54 55 56
is_url()
{
    echo "$1" | grep -q "^[filehtps]*:/"
}
Vitaly Lipatov's avatar
Vitaly Lipatov committed
57

Vitaly Lipatov's avatar
Vitaly Lipatov committed
58 59 60 61 62
is_dir_empty()
{
    [ -z "$(ls -A "$1")" ]
}

63 64 65 66 67 68 69 70
# copied from strings
# CHECKME: the same like estrlist has ?
# Note: used grep -E! write '[0-9]+(first|two)', not '[0-9]\+...'
rhas()
{
    echo "$1" | grep -E -q -- "$2"
}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
71 72 73 74 75 76 77 78 79 80
has_space()
{
    [ "${1/ /}" != "$1" ]
}

has_wildcard()
{
    [ "${1/\*/}" != "$1" ]
}

81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
__handle_tarname()
{
    # TODO: we don't know PKGNAME here
    PKGNAME=

    if [ -n "$EEPM_INTERNAL_PKGNAME" ] ; then
        # it is ok
        [ "$EEPM_INTERNAL_PKGNAME" = "$PKGNAME" ] && continue
        # PKGNAME was changed in play.d script after common.sh include
        echo "Packing as $PKGNAME (not $EEPM_INTERNAL_PKGNAME as it said before) ..."
    else
        # it is possible direct call, not from epm play
        echo "Packing as $PKGNAME package ..."
    fi

    export EEPM_INTERNAL_PKGNAME="$PKGNAME"
}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
99 100 101

return_tar()
{
102
    local i
Vitaly Lipatov's avatar
Vitaly Lipatov committed
103
    [ -n "$RETURNTARNAME" ] || fatal "RETURNTARNAME is empty"
104 105
    rm -f $RETURNTARNAME
    for i in $* ; do
106
        #__handle_tarname $i
107 108
        realpath $i >>$RETURNTARNAME || fatal "Can't save tar name $i to file $RETURNTARNAME"
    done
109
    exit 0
Vitaly Lipatov's avatar
Vitaly Lipatov committed
110 111
}

112
# really like install -D src dst
113 114 115 116 117
install_file()
{
    local src="$1"
    local dest="$2"

Vitaly Lipatov's avatar
Vitaly Lipatov committed
118 119 120
    if is_abs_path "$dest" ; then
        dest=".$dest"
    fi
121
    mkdir -p "$(dirname "$dest")" || return
122 123

    if is_url "$src" ; then
124
        eget -O "$dest" "$src" || fatal "Can't download $src to install to $dest"
125 126 127
    else
        cp "$src" "$dest" || return
    fi
Vitaly Lipatov's avatar
Vitaly Lipatov committed
128
    chmod 0644 "$dest"
129 130
}

131 132 133 134 135 136 137 138 139
# Create target file from file
# Usage: echo "text" | create_file file
create_file()
{
    local t="$1"
    install_file /dev/stdin $t
}


140
# set PRODUCT by pack.d script name
141
[ -n "$PRODUCT" ] || PRODUCT="$(basename $0 .sh)"