#! /bin/bash

# (c) 1997 Winfried Trmper
# Provided "as is", use at your own risk
#
# WARNING: this script is by no means secure, try
#          echo -e "\necho Hallo Welt\n" >> ~/.bashrc

META_ENV="/etc/meta/environment"
META_LIMITS="/etc/meta/limits"
META_UMASK="/etc/meta/umask"


  # The variables are set before 'set -a' to prevent automatic export.
ORGARGS="$@"
MYNAME=$0
old_IFS="$IFS"

function limit_resources () {
    local META_ULIMIT_OPTIONS
    if [ "$1" != "-" -a "$1" != "" ]
	then META_ULIMIT_OPTIONS="-t $1"
    fi
    shift
    if [ "$1" != "-" -a "$1" != "" ]
	then META_ULIMIT_OPTIONS="$META_ULIMIT_OPTIONS -d $1"
    fi
    shift
    if [ "$1" != "-" -a "$1" != "" ]
	then META_ULIMIT_OPTIONS="$META_ULIMIT_OPTIONS -u $1"
    fi
    shift
    if [ "$1" != "-" -a "$1" != "" ]
	then META_ULIMIT_OPTIONS="$META_ULIMIT_OPTIONS -n $1"
    fi

    if [ "$META_ULIMIT_OPTIONS" != "" ]
	then ulimit  $META_ULIMIT_OPTIONS
    fi
}


if [ -f "$META_LIMITS" ]
then
    while read META_TYPE META_USER META_CPU META_RAM META_PROCESSES META_FILES
    do
	case "$META_TYPE" in
	    "#*" | "") continue ;;
	    "user")
		if [ "$META_USER" = "*" -o "$META_USER" = "$LOGNAME" ]
		then
		    limit_resources $META_CPU $META_RAM $META_PROCESSES $META_FILES
		    break
		else
		    continue
		fi
		;;
	    "uid")
		IFS="-"
		set -- $META_USER
		IFS="$old_IFS"
		if [ "$UID" -le "$2" -a "$UID" -ge "$1" ]
		then
		    limit_resources $META_CPU $META_RAM $META_PROCESSES $META_FILES
		    break
		else
		    continue
		fi
		;;
	    *)
		# moan
		;;
	esac
    done < "$META_LIMITS"
fi

    # oct_perm() computes the ocatal value of a simple symbolic file permission
    # sad umask (and chmod) doesn't supports verbatim symbolic permissions
function oct_perm () {
    local permissions n m pre post patt oct_val
    permissions="$1"

      # in case somebody used cut&paste (argh!)
    permissions=${permissions#c}
    permissions=${permissions#b}

      # theory of operation: we shift the dash in -???????? from left to right
      # and match against the symbloc permissions. If - doesn't match we assume
      # r, w or x respectivly. Of course this is all too easy.
    pre=""
    post="????????"
    oct_val="777"
    for m in 100 10 1
    do
        for n in 4 2 1
        do
            patt="$pre-$post"
            case "$permissions" in
                $patt)
                    oct_val=$[ $oct_val-$n*$m ]
                    ;;
            esac
            pre="?$pre"
            post=${post#?*}
        done
    done
    echo $oct_val
}


function set_umask () {
    local U_opt U_perm

    if [ "$1" != "-" -a "$1" != "" ]
    then
	U_perm=$1
	case $U_perm in
	    [r-]*)
		U_perm=`oct_perm $U_perm`
		;;
	esac

	umask $U_opt "$U_perm"
    fi
}


if [ -f "$META_UMASK" ]
then
    while read META_TYPE META_USER META_umask
    do
	case "$META_TYPE" in
	    "#*" | "") continue ;;
	    "user")
		if [ "$META_USER" = "*" -o "$META_USER" = "$LOGNAME" ]
		then
		    set_umask "$META_umask"
		    break
		else
		    continue
		fi
		;;
	    "uid")
		IFS="-"
		set -- $META_USER
		IFS="$old_IFS"
		if [ "$UID" -le "$2" -a "$UID" -ge "$1" ]
		then
		    set_umask "$META_umask"
		    break
		else
		    continue
		fi
		;;
	    *)
		# moan
		;;
	esac
    done < "$META_UMASK"
fi


set -a
if [ -f "$META_ENV" ]
then 
	source "$META_ENV"
fi
set +a


SHELL_BIN=""
SHELL_NAME="${MYNAME#/bin/meta}"
unset MYNAME

while read SHELL
do
    case "$SHELL" in
	"#*" | "") continue ;;
	*$SHELL_NAME) 
	    SHELL_BIN=$SHELL
	    break
	    ;;
    esac
done < /etc/shells

if [ -x "$SHELL_BIN" ]
then
    exec - $SHELL_BIN $ORGARGS
fi
exit 1