#! /bin/sh
#set -xv
#=========================================================================
# Copyright (C) VMware, Inc. 1986-2011.  All Rights Reserved..
#
# Name - makeusers.sh
#
# Purpose - Script to build Topaz "input" script
#
# $Id: makeusers.sh,v 1.8 2008-01-09 22:50:52 stever Exp $
#
#=========================================================================

if [ "a$GEMSTONE" = "a" ]; then
  echo "ERROR: GemStone scripts require a GEMSTONE environment variable."
  echo "       Please set it to the directory where GemStone resides."
  exit 1
fi

# make sure of minimum path
PATH=:/bin:/usr/bin:/usr/ucb:/usr/etc:$PATH; export PATH

comid="makeusers"               #   this script's name
tmpName=/tmp/makeusers-$$

. $GEMSTONE/bin/misc.sh

defaultErrorControl

usageStr="makeusers [-h]\n\
\n\
Create a topaz script which will create GemStone users in a new repository.\n"

giveHelp "$1" "$usageStr"

if [ ! -t ]; then
  # must be a terminal
  # say makeusers.noterm
  echo "makeusers[Error]:    This script must be run from a terminal"
  exit 1
fi

# Get name of the script
scriptName="$HOME/newusers.topaz"
# Explain ourselves, ask for name of result file
# say makeusers.howdy
cat <<EOF

This script will create a file that should subsequently be input using topaz.
What is the name of the file you wish us to create?

EOF

while [ 1 ]; do
  # say makeusers.scriptprompt $scriptName
  echo "[ $scriptName ]:"
  read prompt
  if [ "x$prompt" != "x" ]; then
    scriptName=$prompt
  fi
  if [ -f $scriptName ]; then
    # Already exists - delete it?
    # say makeusers.exists $scriptName
    echo "$scriptName  already exists."
    echo "Do you want to replace it? [n]:"
    read prompt
    if [ "x$prompt" = xy -o "x$prompt" = xY ]; then
      rm $scriptName
      if [ $? -ne 0 ]; then
        # say makeusers.cantremove $scriptName
        echo "Can't remove  $scriptName , Please suggest another"
	continue
      fi
    else
      continue
    fi
  fi
  touch $scriptName
  status=$?
  if [ $status -eq 0 ]; then
    break
  fi
  # trouble creating file
  # say makeusers.create
  echo "Cannot create file $scriptName , Please suggest another"
done

# Get StoneName
echo ""
stoneName=gs64stone
# say makeusers.stone
echo "What is the name of your GemStone monitor process [$stoneName]:"

read prompt
if [ "x$prompt" != "x" ]; then
  stoneName=$prompt
fi

chmod 600 $scriptName
cat <<EOF > $scriptName
set gemstone $stoneName
set username DataCurator
set password swordfish
status
login
level 0
iferror exit
EOF

# say makeusers.intro $scriptName
cat <<EOF

The default SystemUser, DataCurator, and GcUser passwords should be changed
for security purposes (They are the same by default in every new
repository.) Once they have been changed, it is important that you remember
them; there is no trivial "back door" to allow maintenance access to your
repository without these passwords!

This script will write your choice of passwords into the file:

 $scriptName 

which has its protections set so that only you can read it. After you have
successfully committed your users to the GemStone repository, the script will
be automatically deleted. In case of failure, the script will *not* be deleted.
In this case, you should
  rm $scriptName     for security.

EOF


# get new SystemUser password
# say makeusers.npass SystemUser
echo "Enter new SystemUser password:"

# can't stty if logged as root and su'ed as user on DECstation
#   and so stderr to null
stty -echo 2>/dev/null
read password
stty echo 2>/dev/null
echo ""
cat <<END >>$scriptName
run
(AllUsers userWithId: 'SystemUser') password: '$password'.
'succeeded'
%
commit
END

# get datacurator password
# say makeusers.npass DataCurator
echo "Enter new DataCurator password:"

# can't stty if logged as root and su'ed as user on DECstation
#   and so stderr to null
stty -echo 2>/dev/null
read password
stty echo 2>/dev/null
echo ""
cat <<END >>$scriptName
run
(AllUsers userWithId: 'DataCurator') password: '$password'.
'succeeded'
%
commit
END

# get GcUser password
echo "Enter new GcUser password:"
# can't stty if logged as root and su'ed as user on DECstation
#   and so stderr to null
stty -echo 2>/dev/null
read password
stty echo 2>/dev/null
echo ""
cat <<END >>$scriptName
run
(AllUsers userWithId: 'GcUser') password: '$password'.
'succeeded'
%
commit
END


# Get list of users to create
defaultPassword="gemstone"
# say makeusers.ulist "$defaultPassword"
cat <<EOF

Now you need to create GemStone users within your repository. For each user,
GemStone will assign a reasonable set of privileges, and a default password of
  $defaultPassword 

Please enter an empty line when done.
EOF


while [ 1 ]; do
  # say makeusers.uprompt
  echo "User name:"
  read userName
  if [ "x$userName" = "x" ]; then
    break
  fi
  cat <<END >>$scriptName
run
(AllUsers addNewUserWithId: '$userName' asSymbol
          password: '$defaultPassword')
    privileges: #( 'SessionAccess' 'UserPassword'
                   'DefaultSegment' 'SegmentCreation').
'done'
%
commit
END
done

cat <<END >>$scriptName
run
  System performOnServer: 'rm $scriptName'.
  ^ 'Congratulations!'
%
logout
END

# say makeusers.bye $scriptName
cat <<EOF

Now you need to run "topaz -l", and at the prompt 'topaz >'   type:

   input  $scriptName 

If the update is successful, the script will be deleted, and the current session
will logout.
If there are errors, topaz will exit and the script will not be deleted.
EOF

exit 0
