#!/bin/bash
#
# @package      hubzero-mw2-exec-service
# @file         setup_accounts
# @author       Pascal Meunier  <pmeunier@purdue.edu>
# @copyright    Copyright (c) 2018 HUBzero Foundation, LLC.
# @license      http://opensource.org/licenses/MIT MIT
#
# Copyright (c) 2018 HUBzero Foundation, LLC.
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#
# HUBzero is a registered trademark of HUBzero Foundation, LLC.

# Entry Point for a Docker image, to
# setup account and group information from environment variables
# then sleep to keep container running independently of tool
# this is done to be able to collect container stats after tool has run
# as the stats disappear when the container stops.

echo $etcpasswd >> /etc/passwd
echo $etcshadow >> /etc/shadow
[ -z "$etcsudoers" ] || echo $etcsudoers >> /etc/sudoers

[ -z "$etcpasswdapps" ] || echo $etcpasswdapps >> /etc/passwd
[ -z "$etcshadowapps" ] || echo $etcshadowapps >> /etc/shadow
echo ${!group_*}
for envgroup in ${!group_*}; do
  echo ${!envgroup} >> /etc/group
done

if [ -z "$USER" ]; then
  echo USER not set
  exit 1
fi

# copy Xauthority information to user's home .Xauthority file
i=0
until [ $i -ge 5 ]; do
  /bin/su -s /bin/sh -c 'xauth -v source /var/run/Xvnc/authlist*' $USER
  [ $? -eq 0 ] && break
  i=$[$i+1]
  sleep 1
done
if [ $i -ge 5 ]; then
  echo Unable to merge X information with user "$USER" .Xauthority file
  exit 1
fi

# copy Xauthority information to ionhelper's home .Xauthority file
if [ -d /var/ion/ ]; then
  i=0
  until [ $i -ge 5 ]; do
    /bin/su -s /bin/sh -c 'xauth -v source /var/run/Xvnc/authlist*' ionhelper
    [ $? -eq 0 ] && break
    i=$[$i+1]
    sleep 1
  done
  if [ $i -ge 5 ]; then
    echo Unable to merge X information with ionhelper .Xauthority file
    exit 1
  fi
  mkdir -p $sessiondir_ionhelper
  echo sessionid $session_id > $rpath_ionhelper
  echo results_directory /var/ion/runs >> $rpath_ionhelper
  su $USER -s /bin/sh -c "cat $rpath_user" >> $rpath_ionhelper
  mkdir /var/ion/drivers
  chmod 0770 /var/ion/drivers
  chown -R 199:199 %s/var/ion
fi

# sleep forever to keep container running so we can get cpu stats
while :; do
  sleep 99999
done
