#!/bin/sh
##
##  etc.usrgrp.sh -- user/group name/id determination
##  Copyright (c) 2000-2012 OpenPKG GmbH <http://openpkg.com/>
##
##  This software is property of the OpenPKG GmbH, DE MUC HRB 160208.
##  All rights reserved. Licenses which grant limited permission to use,
##  copy, modify and distribute this software are available from the
##  OpenPKG GmbH.
##
##  THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
##  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
##  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
##  IN NO EVENT SHALL THE AUTHORS AND COPYRIGHT HOLDERS AND THEIR
##  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
##  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
##  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
##  USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
##  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
##  OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
##  OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
##  SUCH DAMAGE.
##

#   command line parameters (defaults)
help=0
usr='';  grp=''
susr=''; sgrp=''
musr=''; mgrp=''
rusr=''; rgrp=''
nusr=''; ngrp=''
suid=''; sgid=''
muid=''; mgid=''
ruid=''; rgid=''
nuid=''; ngid=''

#   parse command line options
for opt
do
    case $opt in
        -*=*) arg=`echo "$opt" | sed 's/^[-_a-zA-Z0-9]*=//'` ;;
           *) arg='' ;;
    esac
    case $opt in
        -h | --help          ) help=1      ;;
        --usr=* | --user=*   ) usr=$arg    ;;
        --grp=* | --group=*  ) grp=$arg    ;;
        --susr=*             ) susr=$arg   ;;
        --sgrp=*             ) sgrp=$arg   ;;
        --musr=*             ) musr=$arg   ;;
        --mgrp=*             ) mgrp=$arg   ;;
        --rusr=*             ) rusr=$arg   ;;
        --rgrp=*             ) rgrp=$arg   ;;
        --nusr=*             ) nusr=$arg   ;;
        --ngrp=*             ) ngrp=$arg   ;;
        --suid=*             ) suid=$arg   ;;
        --sgid=*             ) sgid=$arg   ;;
        --muid=*             ) muid=$arg   ;;
        --mgid=*             ) mgid=$arg   ;;
        --ruid=*             ) ruid=$arg   ;;
        --rgid=*             ) rgid=$arg   ;;
        --nuid=*             ) nuid=$arg   ;;
        --ngid=*             ) ngid=$arg   ;;
        *                    ) help=1      ;;
    esac
done
if [ ".$help" = .1 ]; then
    echo "Usage: sh $0 [-h|--help]" 2>&1
    echo "             [--[smrn]?usr=<usr>] [--[smrn]?grp=<usr>]" 2>&1
    echo "             [--[smrn]uid=<uid>] [--[smrn]gid=<gid>]" 2>&1
    exit 1
fi

#   determine cusr/cgrp
cusr=`(id -un) 2>/dev/null ||\
      (id | sed -e 's;^[^(]*(\([^)]*\)).*;\1;') 2>/dev/null ||\
      (whoami) 2>/dev/null ||\
      (who am i | cut "-d " -f1) 2>/dev/null ||\
      echo $LOGNAME`
cgid=`(id -g $cusr) 2>/dev/null ||\
      ((getent passwd "${cusr}"; grep "^${cusr}:" /etc/passwd; ypmatch "${cusr}" passwd; nismatch "${cusr}" passwd; nidump passwd . | grep "^${cusr}:") 2>/dev/null |\
       sed -n -e '1p' | awk -F: '{ print $4; }')`
cgrp=`(id -gn $cusr) 2>/dev/null ||\
      ((getent group; cat /etc/group; ypcat group; niscat group; nidump group .) 2>/dev/null | grep "^[^:]*:[^:]*:${cgid}:" |\
       sed -n -e '1p' | awk -F: '{ print $1; }')`
[ ".$cgrp" = . ] && cgrp="$cusr"

#   determine OpenPKG susr/sgrp
if [ ".$susr" = . ]; then
    if [ ".$usr" = . ]; then
        susr="$cusr"
    else
        susr="root"
    fi
fi
if [ ".$sgrp" = . ]; then
    sgrp=`(id -gn $susr) 2>/dev/null`
    if [ ".$sgrp" = . ]; then
        tgid=`(getent passwd "${susr}"; grep "^${susr}:" /etc/passwd; ypmatch "${susr}" passwd; nismatch "${susr}" passwd; nidump passwd . | grep "^${susr}:") 2>/dev/null |\
              sed -n -e '1p' | awk -F: '{ print $4; }'`
        if [ ".$tgid" != . ]; then
            sgid="${tgid}"
            sgrp=`(getent group; cat /etc/group; ypcat group; niscat group; nidump group .) 2>/dev/null |\
                  grep "^[^:]*:[^:]*:${sgid}:" | sed -n -e '1p' | awk -F: '{ print $1; }'`
        fi
        if [ ".$sgrp" = . ]; then
            sgrp="wheel"
        fi
    fi
fi

#   determine OpenPKG musr/mgrp
if [ ".$musr" = . ]; then
    musr="$usr"
fi
if [ ".$musr" = . ]; then
    musr="$cusr"
fi
if [ ".$mgrp" = . ]; then
    mgrp=`(id -gn $musr) 2>/dev/null`
    if [ ".$mgrp" = . ]; then
        tgid=`(getent passwd "${musr}"; grep "^${musr}:" /etc/passwd; ypmatch "${musr}" passwd; nismatch "${musr}" passwd; nidump passwd . | grep "^${musr}:") 2>/dev/null |\
              sed -n -e '1p' | awk -F: '{ print $4; }'`
        if [ ".$tgid" != . ]; then
            mgid="${tgid}"
            mgrp=`(getent group; cat /etc/group; ypcat group; niscat group; nidump group .) 2>/dev/null |\
                  grep "^[^:]*:[^:]*:${mgid}:" | sed -n -e '1p' | awk -F: '{ print $1; }'`
        fi
        if [ ".$mgrp" = . ]; then
            mgrp="$grp"
        fi
        if [ ".$mgrp" = . ]; then
            mgrp="$cgrp"
        fi
    fi
fi

#   determine OpenPKG rusr/rgrp
if [ ".$rusr" = . ]; then
    rusr="${usr}-r"
fi
if [ ".$rusr" = ".-r" ]; then
    rusr="$cusr"
fi
if [ ".$rgrp" = . ]; then
    rgrp=`(id -gn $rusr) 2>/dev/null`
    if [ ".$rgrp" = . ]; then
        tgid=`(getent passwd "${rusr}"; grep "^${rusr}:" /etc/passwd; ypmatch "${rusr}" passwd; nismatch "${rusr}" passwd; nidump passwd . | grep "^${rusr}:") 2>/dev/null |\
              sed -n -e '1p' | awk -F: '{ print $4; }'`
        if [ ".$tgid" != . ]; then
            rgid="${tgid}"
            rgrp=`(getent group; cat /etc/group; ypcat group; nismatch group; nidump group .) 2>/dev/null |\
                  grep "^[^:]*:[^:]*:${rgid}:" | sed -n -e '1p' | awk -F: '{ print $1; }'`
        fi
        if [ ".$rgrp" = . ]; then
            rgrp="${grp}-r"
        fi
        if [ ".$rgrp" = ".-r" ]; then
            rgrp="$cgrp"
        fi
    fi
fi

#   determine OpenPKG nusr/ngrp
if [ ".$nusr" = . ]; then
    nusr="${usr}-n"
fi
if [ ".$nusr" = ".-n" ]; then
    nusr="$cusr"
fi
if [ ".$ngrp" = . ]; then
    ngrp=`(id -gn $nusr) 2>/dev/null`
    if [ ".$ngrp" = . ]; then
        tgid=`(getent passwd "${nusr}"; grep "^${nusr}:" /etc/passwd; ypmatch "${nusr}" passwd; nismatch "${nusr}" passwd; nidump passwd . | grep "^${nusr}:") 2>/dev/null |\
              sed -n -e '1p' | awk -F: '{ print $4; }'`
        if [ ".$tgid" != . ]; then
            ngid="${tgid}"
            ngrp=`(getent group; cat /etc/group; ypcat group; niscat group; nidump group .) 2>/dev/null |\
                  grep "^[^:]*:[^:]*:${ngid}:" | sed -n -e '1p' | awk -F: '{ print $1; }'`
        fi
        if [ ".$ngrp" = . ]; then
            ngrp="${grp}-n"
        fi
        if [ ".$ngrp" = ".-n" ]; then
            ngrp="$cgrp"
        fi
    fi
fi

#   determine OpenPKG suid/sgid
#   (currently not necessary)

#   determine OpenPKG muid/mgid
#   (currently not necessary)

#   determine OpenPKG ruid/rgid
#   (currently not necessary)

#   determine OpenPKG nuid/ngid
#   (currently not necessary)

#   print results
output=""
for var in \
    susr sgrp \
    musr mgrp \
    rusr rgrp \
    nusr ngrp \
    suid sgid \
    muid mgid \
    ruid rgid \
    nuid ngid; do
    eval "val=\"\$$var\""
    if [ ".$output" = . ]; then
        output="$var=\"$val\""
    else
        output="$output; $var=\"$val\""
    fi
done
echo $output

