#! /bin/sh ####### To Do ####### # Add support for multiple files # Add -a option to automatically copy to all users # Add user checks to verify that user folder exists ####### Define constants and Variables ####### ARGS=2 # Number of parameters required besides # options. Set equal to nothing for no requirement ME=`basename $0` # Capture this script's name VERS="1.0" # Current version number E_NOARGS=65 # Exit code for wrong number of arguments E_NOLENGTH=66 # Exit code for zero length arguments E_NOTROOT=67 # Exit code if not run as root WINSWITCH="/Library/Application Support/WinSwitch/" IN="Switch-In Items" OUT="Switch-Out Items" # Option defaults # Mirror any changes here as needed in usage() and getopts OPT_i= # -i option default OPT_o= # -o option default ####### UI Information ####### usage () { echo echo " Usage: $ME options file user1 [user2...]" echo " Copies one or more files to a given user's" echo " Library/Application Support/WinSwitch folders." echo " Must be run with admin priveledges." echo " Options:" # Change the following as needed # and mirror in the following section echo " -i Copy to Switch-In folder" echo " -o Copy to Switch-Out folder" echo " -h For this help list" echo " -v For current version" echo } version () { echo echo " $ME version $VERS" echo } ####### Parse command line ####### # Get options from $@ (parameters/arguments) while getopts ihov option do case $option in i) OPT_i=1;; o) OPT_o=1;; h) usage exit 0;; v) version exit 0;; *) exit 1;; esac done # This strips off options leaving remaining parameters shift $(($OPTIND - 1)) # Must be run as root if [ $USER != "root" ]; then echo echo "$ME error: Must be run as root" >&2 usage >&2 exit $E_NOTROOT fi # Check for required number of parameters if $ARGS is set if [ $# -lt "${ARGS:-$#}" ]; then echo echo "$ME error: Requires $ARGS or more argument(s)" >&2 usage >&2 exit $E_NOARGS fi # Check for zero length arguments # Uncomment to activate for arg in "$@" do if [ -z "$arg" ]; then echo echo "$ME error: No empty string arguments allowed" >&2 usage >&2 exit $E_NOLENGTH fi done ############################################################## # Main script area # # If we got this far, all command line parameters # # must be good # ############################################################## # Convenient exit function to clean up after yourself # Call this function instead of exit X allDone () { # Insert cleanup code here exit $1 } file_path=$1 this_file=`basename "${file_path}"` shift echo for this_user in "$@" do # better copy option? /Developer/Tools/CpMac -r if [ ! -z "$OPT_i" ]; then echo "Copying ${this_file} to ${this_user}'s ${IN} folder" sudo -u ${this_user} /bin/cp -R "${file_path}" "/Users/${this_user}${WINSWITCH}${IN}/" /usr/sbin/chown ${this_user}:staff "/Users/${this_user}${WINSWITCH}${IN}/${this_file}" fi if [ ! -z "$OPT_o" ]; then echo "Copying ${this_file} to ${this_user}'s ${OUT} folder" sudo -u ${this_user} /bin/cp -R "${file_path}" "/Users/${this_user}${WINSWITCH}${OUT}/" /usr/sbin/chown ${this_user}:staff "/Users/${this_user}${WINSWITCH}${OUT}/${this_file}" fi done echo ######## Exit with a successful code ######## allDone 0