#!/bin/sh 

#####################################################################
# scenario #3
#     Explores relationship between project and qualified member
#
#  Drd, a geniPI, created a project 'proj1' and 'proj2'
#  Drd grants memberQ/info to Lisa on proj1, memberQ/stop to James on proj1
#  Drd grants memberQ_/info and memberQ_/stop to Tim
#  Drd grants delegate_memberQ_/info to John and 
#                                 delegate_memberQ_/instantiate to Tim
#  PA grants member/info to Tim
#  PA grants memberQ/info and memberQ/stop to Dan
#  PA grants memberQ_/info and memberQ_/stop to Frank and
#           memberQ_/instantiate to Frank
#  PA grants delegate_memberQ_/info to James and delegate_memberQ_/stop to Lisa
#  PA grants delegate_memberQ_/info (proj2) to John
#  John grants memberQ_/info (proj2) to James
#  John grants memberQ/info (proj2) to Joe
#
unset ABAC_CN
keyloc=`pwd`
sidx=s3

makeWho() {
    WHO=$1
    rm -rf ${WHO}_*.pem
    creddy --generate --cn ${WHO}
}
makeWho GR
makeWho IDP
makeWho EXO
makeWho Drd
makeWho Frank
makeWho Dan
makeWho Lisa
makeWho John
makeWho Joe
makeWho Tim
makeWho James

g=`creddy --keyid --cert $keyloc/G_ID.pem`
gr=`creddy --keyid --cert $keyloc/GR_ID.pem`
pa=`creddy --keyid --cert $keyloc/PA_ID.pem`
sa=`creddy --keyid --cert $keyloc/SA_ID.pem`
idp=`creddy --keyid --cert $keyloc/IDP_ID.pem`
exo=`creddy --keyid --cert $keyloc/EXO_ID.pem`
drd=`creddy --keyid --cert $keyloc/Drd_ID.pem`
frank=`creddy --keyid --cert $keyloc/Frank_ID.pem`
dan=`creddy --keyid --cert $keyloc/Dan_ID.pem`
lisa=`creddy --keyid --cert $keyloc/Lisa_ID.pem`
john=`creddy --keyid --cert $keyloc/John_ID.pem`
joe=`creddy --keyid --cert $keyloc/Joe_ID.pem`
tim=`creddy --keyid --cert $keyloc/Tim_ID.pem`
james=`creddy --keyid --cert $keyloc/James_ID.pem`

pname=${sidx}_yap_clauses
namespace=access1
sfile=sfile_${sidx}

idp_gUser()
{
eval "namename=[keyid:\$$1]"
attr="[keyid:$idp].role:geniUser <- $namename"
name=${sidx}_IDP_gUser__$1_attr.der
creddy --attrnow --string "$attr" --out $name --out_prolog $pname --namespace $namespace
}

## drd requesting a proj1 from pa
## makeProject $drd proj1 Drd
makeProject() {
   what="'$2'"
# Drd.project <- 'proj1'
   attr="[keyid:$1].oset:project <- [string:$what]"
   name=${sidx}_$3_project__$2_attr.der
   creddy --attrnow --string "$attr" --out $name --out_prolog $pname --namespace $namespace
# PA.standard <- 'proj1'
   attr="[keyid:$pa].oset:standard <- [string:$what]"
   name=${sidx}_PA_standard__$2_attr.der
   creddy --attrnow --string "$attr" --out $name --out_prolog $pname --namespace $namespace
# PA.owner('proj1') <- Drd
   attr="[keyid:$pa].role:owner([string:$what]) <- [keyid:$1]"
   name=${sidx}_PA_owner_$2__$3_attr.der
   creddy --attrnow --string "$attr" --out $name --out_prolog $pname --namespace $namespace
# PA.root('proj1') <-Drd
   attr="[keyid:$pa].role:root([string:$what]) <- [keyid:$1]"
   name=${sidx}_PA_root_$2__$3_attr.der
   creddy --attrnow --string "$attr" --out $name --out_prolog $pname --namespace $namespace
}

###############################################################################
echo "s/$g/G/g "  > $sfile 
echo "s/$gr/GR/g "  >> $sfile 
echo "s/$pa/PA/g "  >> $sfile
echo "s/$sa/SA/g "  >> $sfile
echo "s/$idp/IDP/g "  >> $sfile
echo "s/$exo/EXO/g "  >> $sfile
echo "s/$drd/Drd/g "  >> $sfile
echo "s/$frank/Frank/g "  >> $sfile
echo "s/$dan/Dan/g "  >> $sfile
echo "s/$lisa/Lisa/g "  >> $sfile
echo "s/$john/John/g "  >> $sfile 
echo "s/$joe/Joe/g "  >> $sfile
echo "s/$tim/Tim/g "  >> $sfile
echo "s/$james/James/g "  >> $sfile

# G.geniRoot <- GR 
attr="[keyid:$g].role:geniRoot <- [keyid:$gr]"
name=${sidx}_G_geniRoot__gr_attr.der
creddy --attrnow --string "$attr" --out $name --out_prolog $pname --namespace $namespace

# GR.sliceAuthorithority <- SA
attr="[keyid:$gr].role:sliceAuthority <- [keyid:$sa]"
name=${sidx}_GR_sliceAuthority__sa_attr.der
creddy --attrnow --string "$attr" --out $name --out_prolog $pname --namespace $namespace

# GR.identityProvider <- IDP
attr="[keyid:$gr].role:identityProvider <- [keyid:$idp]"
name=${sidx}_GR_identityProvider__idp_attr.der
creddy --attrnow --string "$attr" --out $name --out_prolog $pname --namespace $namespace

# GR.projectAuthority <- PA
attr="[keyid:$gr].role:projectAuthority <- [keyid:$pa]"
name=${sidx}_GR_projAuthority__pa_attr.der
creddy --attrnow --string "$attr" --out $name --out_prolog $pname --namespace $namespace

# GR.aggregate <- EXO
attr="[keyid:$gr].role:aggregate <- [keyid:$exo]"
name=${sidx}_GR_aggregate__exo_attr.der
creddy --attrnow --string "$attr" --out $name --out_prolog $pname --namespace $namespace

# IDP.geniUser <- Drd
idp_gUser drd
# IDP.geniUser <- Frank
idp_gUser frank
# IDP.geniUser <- Dan
idp_gUser dan
# IDP.geniUser <- Lisa
idp_gUser lisa
# IDP.geniUser <- John
idp_gUser john
# IDP.geniUser <- Joe
idp_gUser joe
# IDP.geniUser <- Tim
idp_gUser tim 
# IDP.geniUser <- James
idp_gUser james

# IDP.geniPI <- Drd
attr="[keyid:$idp].role:geniPI <- [keyid:$drd]"
name=${sidx}_IDP_geniPI__drd_attr.der
creddy --attrnow --string "$attr" --out $name --out_prolog $pname --namespace $namespace

## drd requesting a proj2 from pa
makeProject $drd proj2 Drd
## drd requesting a proj1 from pa
makeProject $drd proj1 Drd

## PA gives member permission on proj1 to Tim
attr="[keyid:$pa].role:member([string:'proj1']) <- [keyid:$tim]"
name=${sidx}_PA_member_proj1__tim_attr.der
creddy --attrnow --string "$attr" --out $name --out_prolog $pname --namespace $namespace

## PA gives memberQ/info permission on proj1 to Dan
attr="[keyid:$pa].role:memberQ([string:'proj1'],[string:'info']) <- [keyid:$dan]"
name=${sidx}_PA_memberQ_proj1_info__dan_attr.der
creddy --attrnow --string "$attr" --out $name --out_prolog $pname --namespace $namespace

## PA gives memberQ/stop permission on proj1 to Dan
attr="[keyid:$pa].role:memberQ([string:'proj1'],[string:'stop']) <- [keyid:$dan]"
name=${sidx}_PA_memberQ_proj1_stop__dan_attr.der
creddy --attrnow --string "$attr" --out $name --out_prolog $pname --namespace $namespace

## PA gives memberQ/instantiate permission on proj1 to Frank
attr="[keyid:$pa].role:memberQ([string:'proj1'],[string:'instantiate']) <- [keyid:$frank]"
name=${sidx}_PA_memberQ_proj1_instantiate__frank_attr.der
creddy --attrnow --string "$attr" --out $name --out_prolog $pname --namespace $namespace

## PA gives memberQ_/info permission on proj1 to Frank
attr="[keyid:$pa].role:memberQ_([string:'proj1'],[string:'info']) <- [keyid:$frank]"
name=${sidx}_PA_memberQDel_proj1__info_frank_attr.der
creddy --attrnow --string "$attr" --out $name --out_prolog $pname --namespace $namespace

## PA gives memberQ_/stop permission on proj1 to Frank
attr="[keyid:$pa].role:memberQ_([string:'proj1'],[string:'stop']) <- [keyid:$frank]"
name=${sidx}_PA_memberQDel_proj1__stop_frank_attr.der
creddy --attrnow --string "$attr" --out $name --out_prolog $pname --namespace $namespace

## PA gives memberQ_/instantiate permission on proj1 to John
attr="[keyid:$pa].role:memberQ_([string:'proj1'],[string:'instantiate']) <- [keyid:$john]"
name=${sidx}_PA_memberQDel_proj1_instantiate__john_attr.der
creddy --attrnow --string "$attr" --out $name --out_prolog $pname --namespace $namespace

## PA gives delegate_memberQ_/info permission on proj1 to James
attr="[keyid:$pa].role:delegate_memberQ_([string:'proj1'],[string:'info']) <- [keyid:$james]"
name=${sidx}_PA_delmemberQDel_proj1_info__james_attr.der
creddy --attrnow --string "$attr" --out $name --out_prolog $pname --namespace $namespace

## PA gives delegate_memberQ_/stop permission on proj1 to Lisa
attr="[keyid:$pa].role:delegate_memberQ_([string:'proj1'],[string:'stop']) <- [keyid:$lisa]"
name=${sidx}_PA_delmemberQDel_proj1_stop__lisa_attr.der
creddy --attrnow --string "$attr" --out $name --out_prolog $pname --namespace $namespace

## Drd gives memberQ/info permission on proj1 to Lisa
attr="[keyid:$drd].role:memberQ([string:'proj1'],[string:'info']) <- [keyid:$lisa]"
name=${sidx}_Drd_memberQ_proj1_info__lisa_attr.der
creddy --attrnow --string "$attr" --out $name --out_prolog $pname --namespace $namespace

## Drd gives memberQ/stop permission on proj1 to James
attr="[keyid:$drd].role:memberQ([string:'proj1'],[string:'stop']) <- [keyid:$james]"
name=${sidx}_Drd_memberQ_proj1_stop__james_attr.der
creddy --attrnow --string "$attr" --out $name --out_prolog $pname --namespace $namespace

## Drd gives memberQ_/info permission on proj1 to Tim
attr="[keyid:$drd].role:memberQ_([string:'proj1'],[string:'info']) <- [keyid:$tim]"
name=${sidx}_Drd_memberQDel_proj1_info__tim_attr.der
creddy --attrnow --string "$attr" --out $name --out_prolog $pname --namespace $namespace

## Drd gives memberQ_/stop permission on proj1 to Tim
attr="[keyid:$drd].role:memberQ_([string:'proj1'],[string:'stop']) <- [keyid:$tim]"
name=${sidx}_Drd_memberQDel_proj1_stop__tim_attr.der
creddy --attrnow --string "$attr" --out $name --out_prolog $pname --namespace $namespace

## Drd gives delegate_memberQ_/info permission on proj1 to John
attr="[keyid:$drd].role:delegate_memberQ_([string:'proj1'],[string:'info']) <- [keyid:$john]"
name=${sidx}_Drd_delmemberQDel_proj1_info__john_attr.der
creddy --attrnow --string "$attr" --out $name --out_prolog $pname --namespace $namespace

## Drd gives delegate_memberQ_/instantiate permission on proj1 to Tim
attr="[keyid:$drd].role:delegate_memberQ_([string:'proj1'],[string:'instantiate']) <- [keyid:$tim]"
name=${sidx}_Drd_delmemberQDel_proj1_instantiate__tim_attr.der
creddy --attrnow --string "$attr" --out $name --out_prolog $pname --namespace $namespace


## PA gives info delegate_memberQ_/info permission on proj2 to John 
attr="[keyid:$pa].role:delegate_memberQ_([string:'proj2'],[string:'info']) <- [keyid:$john]"
name=${sidx}_Drd_delmemberQDel_proj2_info__john_attr.der
creddy --attrnow --string "$attr" --out $name --out_prolog $pname --namespace $namespace

## John gives memberQ_/info permission on proj2 to James 
attr="[keyid:$john].role:memberQ_([string:'proj2'],[string:'info']) <- [keyid:$james]"
name=${sidx}_PA_memberQDel_proj2_info__james_attr.der
creddy --attrnow --string "$attr" --out $name --out_prolog $pname --namespace $namespace

## John gives memberQ/info permission on proj2 to Joe 
attr="[keyid:$john].role:memberQ([string:'proj2'],[string:'info']) <- [keyid:$joe]"
name=${sidx}_PA_memberQ_proj2_info__joe_attr.der
creddy --attrnow --string "$attr" --out $name --out_prolog $pname --namespace $namespace


