esu 1.91 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
#!/usr/bin/env bash
#
# Copyright (C) 2023  Etersoft
# Copyright (C) 2023  Vitaly Lipatov <lav@etersoft.ru>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#

PROGDIR=$(dirname "$0")
PROGNAME=$(basename "$0")
[ -n "$EPMCURDIR" ] || export EPMCURDIR="$(pwd)"
23 24
CMDENV="/usr/bin/env"
[ -x "$CMDENV" ] && CMDSHELL="/usr/bin/env bash" || CMDSHELL="$SHELL"
25 26 27 28 29 30 31 32 33 34 35 36
# TODO: pwd for ./epm and which for epm
[ "$PROGDIR" = "." ] && PROGDIR="$EPMCURDIR"
if [ "$0" = "/dev/stdin" ] || [ "$0" = "sh" ] ; then
    PROGDIR=""
    PROGNAME=""
fi

# will replaced with /usr/share/eepm during install
SHAREDIR=$PROGDIR

load_helper()
{
37 38 39 40
    local shieldname="loaded$(echo "$1" | sed -e 's|-||g')"
    # already loaded
    eval "test -n \"\$$shieldname\"" && debug "Already loaded $1" && return

41 42 43
    local CMD="$SHAREDIR/$1"
    # do not use fatal() here, it can be initial state
    [ -r "$CMD" ] || { echo "FATAL: Have no $CMD helper file" ; exit 1; }
44
    eval "$shieldname=1"
45 46 47 48
    # shellcheck disable=SC1090
    . $CMD
}

49 50
load_helper epm-sh-functions

51 52
esu_print_help()
{
53
    cat <<EOF
54 55
esu - get root shell or run a command in root shell
Usage: esu [-c] [<command>]
56
EOF
57 58 59 60 61 62 63 64 65 66 67 68 69 70
}



if [ "$1" = "-h" ] || [ "$1" = "--help" ] ; then
    esu_print_help
    exit
fi

# just ignore
if [ "$1" = "-c" ] ; then
    shift
fi

71 72 73
# just ignore
if [ "$1" = "-" ] ; then
    shift
74 75
fi

76
esu "$@"