#!/bin/sh
##
##  usrgrp.sh -- user/group name/id determination
##  Copyright (c) 2002 Cable & Wireless Deutschland GmbH
##  Copyright (c) 2002 The OpenPKG Project <http://www.openpkg.org/>
##  Copyright (c) 2002 Ralf S. Engelschall <rse@engelschall.com>
##
##  Permission to use, copy, modify, and distribute this software for
##  any purpose with or without fee is hereby granted, provided that
##  the above copyright notice and this permission notice appear in all
##  copies.
##
##  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=''

#   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   ;;
        *                    ) 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=`(cat /etc/passwd; ypcat passwd) 2>/dev/null |\
      grep "^${cusr}:" | sed -n -e '1p' | awk -F: '{ print $4; }'`
cgrp=`(cat /etc/group; ypcat group) 2>/dev/null |\
      grep ":${cgid}:" | sed -n -e '1p' | awk -F: '{ print $1; }'`
[ ".$cgrp" = . ] && cgrp="$cusr"

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

#   determine OpenPKG musr/mgrp
if [ ".$musr" = . ]; then 
    musr="$usr"
fi
if [ ".$musr" = . ]; then
    musr="$cusr"
fi
if [ ".$mgrp" = . ]; then
    mgid=`(cat /etc/passwd; ypcat passwd) 2>/dev/null |\
          grep "^${musr}:" | sed -n -e '1p' | awk -F: '{ print $4; }'`
    if [ ".$mgid" != . ]; then
        mgrp=`(cat /etc/group; ypcat 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

#   determine OpenPKG rusr/rgrp
if [ ".$rusr" = . ]; then 
    rusr="${usr}-r"
fi
if [ ".$rusr" = ".-r" ]; then
    rusr="$cusr"
fi
if [ ".$rgrp" = . ]; then
    rgid=`(cat /etc/passwd; ypcat passwd) 2>/dev/null |\
          grep "^${rusr}:" | sed -n -e '1p' | awk -F: '{ print $4; }'`
    if [ ".$rgid" != . ]; then
        rgrp=`(cat /etc/group; ypcat 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

#   determine OpenPKG nusr/ngrp
if [ ".$nusr" = . ]; then 
    nusr="${usr}-n"
fi
if [ ".$nusr" = ".-n" ]; then
    nusr="$cusr"
fi
if [ ".$ngrp" = . ]; then
    ngid=`(cat /etc/passwd; ypcat passwd) 2>/dev/null |\
          grep "^${nusr}:" | sed -n -e '1p' | awk -F: '{ print $4; }'`
    if [ ".$ngid" != . ]; then
        ngrp=`(cat /etc/group; ypcat 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

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

