#!/bin/bash
#
# 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.
#
# This script changes the state of the channels using a given SE to "Stopped"
# It receives an input parameter which should be a valid name for a SE
# The channels using the specified SE as source or destination will change its state to Stopped
#
# Authors:
#	Rosa M.Garcia Rioja <Rosa.Garcia@cern.ch>
#	Akos Frohner <Akos.Frohner@cern.ch>


USAGE="Usage: $(basename $0) [-h | --help][-s <SURL>] [-S <State (Active|Inactive|Drain|Stopped)]  SE"
VERBOSE='yes'
#DRYRUN=echo
TEMP=$(getopt -o hs:S: --long help -- "$@")

eval set -- "$TEMP"

while true; do
    case "$1" in
        -h|--help)
           echo $USAGE
           exit
           ;;
        -s)
           shift
           ENDPOINT="-s $1"
           shift
           ;;
        -S)
           shift
           STATE="-S $1"
           shift
           ;;
        --)
          shift
          break
          ;;  
        *)
          exit 
          ;;
    esac
done


if [ $# -ne 1 ]
then 
  echo $USAGE
  exit
fi
SE="$1"

for channel in `$DRYRUN glite-transfer-channel-list $ENDPOINT`;
do
 $DRYRUN glite-transfer-channel-list $ENDPOINT $channel | grep -q $SE
 if [ $? = 0 ];
 then
    echo "Channel using the SE specified" $1
    $DRYRUN glite-transfer-channel-set $STATE $channel
    echo "The state of the channel has been modified"
 fi
done;
