#!/bin/bash
#
# 
#
#  The contents of this file are subject to the terms of the
#  Common Development and Distribution License, Version 1.0 only
#  (the "License").  You may not use this file except in compliance
#  with the License.
# 
#  You can obtain a copy of the license at 
#  http://www.opensource.org/licenses/cddl1.php
# 
#  When distributing Covered Code, include this CDDL HEADER in each
#  file and include the original License file at src/license.txt.
#  If applicable, add the following below this CDDL HEADER, with the
#  fields enclosed by brackets "[ ]" replaced with your own identifying
#  information: Portions Copyright [yyyy] [name of copyright owner]
# 

#  Copyright (c) 2005 by Centeris, Inc.  All rights reserved.
#  Use is subject to license terms.
#  Initial Developer updates, news, and information may be found at 
#  http://sourceforge.net/projects/likewiseopenagt
#

#
# Script to shut down a system from Samba. Note that this may be called w/o the user being root.  
#   Exit codes provided by this script match the error codes
#   expected by Windows -- e.g. 0 == success, 5 == access denied
#
#   Call from within the smb.conf file with the following
#     shutdown script = /etc/samba/shutdown %t %g %x %m %r 
#     %r reboot_flag %f force_flag %t time_val %g message %x reason_val
#  
#echo `date` >>/var/log/samba/log.shutdown 
time=$1
time=$(( (time+59)/60))
#echo $1 $2 $3 $4 $5 >>/var/log/samba/log.shutdown
#
# if $1 doesn't specify a reboot flag, then we halt the machine
# - no dangling roots...
#
if [ -z "$5" ]; then
   reboot='-h'
else
   reboot="$5"
fi
/bin/logger "shutdown $reboot requested in $1 seconds from machine $4 $2 reason=$3"
# if we're delaying shutdown the background the process
if [ $1 != 0 ]; then
	/sbin/shutdown $reboot +$time \"$2 reason=$3\" &
else
	/sbin/shutdown $reboot +$time \"$2 reason=$3\"
	exit $?
fi

#
# give time for the shutdown to fail if it's going to...
sleep 2
#
pstate=`/bin/ps -p $! -o state=`
if [ -n "$pstate"  ]; then
   /bin/logger "shutdown started [$pstate]"
   exit 0
fi
# access denied
exit 5

