#*/
#* Copyright (c) 2011 Gitanjali Yadav (gy@nipgr.res.in)
#* All rights reserved.
#*
#* Redistribution and use in source and binary forms, with or without
#* modification, are permitted without a fee provided that the following
#* conditions are met:
#*
#* 1. This software is only used for private, research, or academic 
#*    purposes.
#*    
#* 2. Redistributions of source code must retain the above copyright
#*    notice, this list of conditions and the following disclaimer.
#*
#* 3. Redistributions in binary form must reproduce the above copyright
#*    notice, this list of conditions and the following disclaimer in the
#*    documentation and/or other materials provided with the distribution.
#*
#* 4. Any changes made to this package must be submitted to the author.
#*    The legal status of the submitted changes must allow their inclusion
#*    into this package under this license.
#*
#* 5. Publications in the field of pseudorandom number generation, which
#*    made use of this package must include a reference to this package.
#*      
#* Any use of this software in a commercial environment requires a
#* written licence from the author. Contact Gitanjali Yadav
#* (gy@nipgr.res.in) to negotiate the terms.
#*
#* THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR
#* IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
#* WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
#* 
#* IN NO EVENT SHALL GITANJALI YADAV BE LIABLE FOR ANY SPECIAL, INCIDENTAL,
#* INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, OR ANY DAMAGES 
#* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER OR
#* NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF 
#* LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE 
#* OF THIS SOFTWARE.
#*
#*/
#*
#*/
#*  get_nodes_perturbation 	For perturbation of multiple nodes in the network 
#*
#*  Author:	Gitanjali Yadav (gy@nipgr.res.in)
#*
#*  Last Modification: Wed Sep 18 12:50:35 CEST 2011
#*
#*  Called from within 'run_group_perturbation'
#*/

#*/ Modification History:

#   11/02/02: First Version.
#   11/22/04: Insert warnings for possible errors
#   11/18/09: replaced /usr/local/bin/R by Rpath
#*/

if test `cat igp_mode` = "USR"
then
echo = FOR `cat icut` USER SELECTED NODES=
if test -s 'group.input'
then
cp group.input  group.cascade 
sed 's/^/sed -n /' group.cascade | sed 's/$/p DATA.nodes | sed '"'"'s\/ \/\/g'"'"'/' >itmpy
chmod a+x itmpy
./itmpy >ilist
else
echo === CANNOT FIND INPUT FILE ==
echo PLEASE RETRY OR CHECK README FOR INSTRUCTIONS FOR SIMULATING GROUP PERTURBATION
fi
else
if test `cat igp_mode` = "MAX"
then
echo = FOR `cat icut` MOST CONNECTED NODES=
tail -`cat icut` DATA.degree | tac > gplist 
cat gplist | awk '{print $1}' > ilist
sed 's/^/grep -wn /' ilist | sed 's/$/ DATA.nodes | cut -d: -f1 /' >itmpy
chmod a+x itmpy
./itmpy >group.cascade
else
if test `cat igp_mode` = "MIN"
then
echo ===FOR `cat icut` LEAST CONNECTED NODES==
head -`cat icut` DATA.degree | tac > gplist 
cat gplist | awk '{print $1}' > ilist
sed 's/^/grep -wn /' ilist | sed 's/$/ DATA.nodes | cut -d: -f1 /' >itmpy
chmod a+x itmpy
./itmpy >group.cascade
else
echo " INPUT NOT CLEAR"
fi
fi
fi
if test -s group.cascade
then 
paste -d, -s group.cascade | sed 's/^/c(/' | sed 's/$/)/' >inlist
sed 's/INODE/'"`cat gp_id`"'/' run_nodes_perturb.R >itmpx
sed 's/NLIST/'"`cat inlist`"'/' itmpx >runnodes_tmp.R
echo nodes_`cat gp_id`_rem.ncol >gpname
`cat Rpath` --no-restore --no-save --no-readline --slave < runnodes_tmp.R >>R.out
cp ilist nodes_`cat gp_id`.names 
sed 's/"/\n/g' nodes_`cat gp_id`.isolates | grep '[A-Z|a-z|0-9]' | sed '1d'  | grep -v '\[' >nodes_`cat gp_id`.secex
sed 's/^/grep -w /' ilist | sed s'/$/ DATA.ncol/' >itmp_getnodes
chmod a+x itmp_getnodes 
./itmp_getnodes | sort -u >nodes_`cat gp_id`.ncol
if test -s `cat gpname` 
then
echo ====SIMULATION COMPLETE! ====
else
echo ""
echo "====   TOTAL COLLAPSE    ===="
echo = THE NETWORK COMPLETELY DISINTEGRATES UPON PERTURBATION =
echo "====   TOTAL COLLAPSE    ===="
echo ""
fi
fi
