##############################################################################
# Copyright (c) Members of the EGEE Collaboration. 2004.
# See http://www.eu-egee.org/partners/ for details on the copyright
# holders.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at #
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS
# OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
##############################################################################
#
# NAME :        config-vobox
#
# DESCRIPTION : This file configures the VOBOX proxy renewal service.
#
# AUTHORS :     Preslav.Konstantinov@cern.ch
#               Elena.Puncheva@cern.ch
#               Maarten.Litmaath@cern.ch
#
# NOTES :
#
# YAIM MODULE:  glite-yaim-clients
#
##############################################################################

config_vobox_check () {
  requires $1 USERS_CONF VOS SITE_NAME
  return $?
}

config_vobox_setenv () {
  yaimgridenv_set SITE_NAME ${SITE_NAME} # general
  if [ "$SITE_BDII_HOST" ]; then
    yaimgridenv_set SITE_GIIS_URL ${SITE_BDII_HOST}
  fi
}

config_vobox (){

VOBOX_PROXY_ROOT=${VOBOX_PROXY_ROOT:-/var/lib/vobox}

# loop around every VO

for i in $VOS ; do
    ACCOUNTS=`users_getspecialusers $i sgm`
    ACCOUNT=$(echo $ACCOUNTS | cut -d' ' -f1)
    if [ -z "$ACCOUNTS" ]; then continue; fi

#    for ACCOUNT in $ACCOUNTS; do

    # add the SGM to the cron.allow
    [ -e /etc/cron.allow ] && grep -w $ACCOUNT /etc/cron.allow > /dev/null ||
	echo $ACCOUNT >> /etc/cron.allow

    vodir=${VOBOX_PROXY_ROOT}/${i}
    mkdir -p $vodir
    chown ${ACCOUNT}. $vodir

    for dir in start stop agents info-provider log proxy_repository; do
      d=$vodir/${dir}
      mkdir -p $d
      chmod 700 $d
      chown ${ACCOUNT}. $d
    done

    # install the renewal service

    svc=/etc/rc.d/init.d/${i}-box-proxyrenewal

    sed -e "s/THEVO/${i}/" -e "s/THEADMIN/${ACCOUNT}/" \
	/etc/vobox/templates/voname-box-proxyrenewal > $svc
    chmod +x $svc

    svc=${svc##*/}

    /sbin/chkconfig --add $svc
    /sbin/chkconfig $svc on
    /sbin/service   $svc stop
    /sbin/service   $svc start

    cron_job $svc root "20 2,8,14,20 * * * /sbin/service $svc proxy"

    cat <<EOF > /etc/logrotate.d/$svc
$vodir/log/events.log {
    compress
    monthly
    rotate 12
    missingok
}
EOF

done

return 0

}
