#!/usr/local/bin/bash

sudo cp gen_sql /proj/emulab-ops/nodecheck/nodecheck/gen_sql
sudo cp checkutils.sh /proj/emulab-ops/nodecheck/nodecheck/checkutils.sh
sudo scp rc.nodecheck checkutils.sh diskcheck apt155:/etc/testbed
exit 0


#
# Copyright (c) 2013-2020 University of Utah and the Flux Group.
# 
# {{{EMULAB-LICENSE
# 
# This file is part of the Emulab network testbed software.
# 
# This file is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or (at
# your option) any later version.
# 
# This file is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public
# License for more details.
# 
# You should have received a copy of the GNU Affero General Public License
# along with this file.  If not, see <http://www.gnu.org/licenses/>.
# 
# }}}
#

arg=$1

#list="pc4 pc7 pc133 pc137 pc207 pc208 pc406 pc446 pc511 pc606"

if [ "${arg}" == "static" ] ; then
    here=$(pwd)
    if [ -d "bin_FreeBSD_static" ] ; then
	cd bin_FreeBSD_static
	stuff=$(ls)
	if [ -n "${stuff}" ] ; then
	    sudo rsync -a -P * /proj/emulab-ops/nodecheck/bin_freebsd
	fi
	cd $here
    fi
    if [ -d "bin_Linux_static" ] ; then
	cd bin_Linux_static
	stuff=$(ls)
	if [ -n "${stuff}" ] ; then
	    sudo rsync -a -P * /proj/emulab-ops/nodecheck/bin_linux
	fi
	cd $here
    fi
    exit 0
fi

if [ "$@" ] ; then
    list="$@"
else
    #list=$(mysql -B -N -e "select node_id from reserved where pid='emulab-ops' and eid='nodechk';" tbdb)
    blist=$(mysql -B -N -e "select node_id from reserved where pid='emulab-ops' and eid='nodecheck';" tbdb)
    elist=$(mysql -B -N -e "select node_id from reserved where pid='emulab-ops' and eid='64nodecheck';" tbdb)
    alist=$(mysql -B -N -e "select node_id from reserved where pid='emulab-ops' and eid='nore32';" tbdb)
    clist=$(mysql -B -N -e "select node_id from reserved where pid='testbed' and eid='wce';" tbdb)
    dlist=$(mysql -B -N -e "select node_id from reserved where pid='emulab-ops' and eid='hw';" tbdb)
    flist=$(mysql -B -N -e "select node_id from reserved where pid='emulab-ops' and eid='nodetest';" tbdb)
    list="$alist $blist $clist $dlist $elist $flist"
fi
echo -n "LIST="
for i in $list ; do
echo -n "$i "
done
echo ""

machines=""

set -u

linux=""
bsd=""

#echo "||$machines||"
#echo "bsd||$bsd||"
#echo "linux||$linux||"


utils="diskcheck memcheck cpucheck checkutils.sh hbis.sh timecheck niccheck checknode"

arg=${arg=""} # use var if set else use ""

case $arg in
    disk | mem | cpu | time | nic | Ngetlogs | clearlogs | help | -help | -h | ? ) ;;
    pc* | dbox* | pg* )
	list=$arg ;;
esac

case $arg in
    # don't need OS if not 
    disk | mem | cpu | time | nic | Ngetlogs | clearlogs | help | -help | -h | ? ) machines=$list ;;
    * )
	for i in $list
	do
	    os=$(sudo ssh $i uname -s)
	    if [ "$os" == "FreeBSD" ] ; then
		bsd="$bsd $i" ; machines="$machines $i"
	    elif [ "$os" == "Linux" ] ; then
		linux="$linux $i" ; machines="$machines $i"
	    else
		echo "Do not know the $os of $i, skipping"
	    fi
	done
	;;
esac

case $arg in
    drive | disk | mem | cpu | time | nic)
	[[ "$arg" == "drive" ]] && arg=disk
	for i in $machines ; do
	    sudo scp -q ${arg}check $i:/usr/local/etc/emulab
	    echo -n "$i "
	    sudo ssh $i "cd /usr/local/etc/emulab; bash /usr/local/etc/emulab/${arg}check /var/emulab/logs/nodecheck.log"
	    echo ""
	done
	;;
    boot | run )
	[[ "$arg" == "boot" ]] && action="boot" || action=''
	    sudo scp -q rc.nodecheck $i:/usr/local/etc/emulab/rc
	for i in $machines ; do
	    [[ ${linux/${i}//} == ${linux} ]] && echo -n "FreeBSD" || echo -n "Linux"
	    echo "----------------- $i --------------------"
	    sudo ssh $i "cd /usr/local/etc/emulab; /usr/local/etc/emulab/rc/rc.nodecheck ${action}"
	done
	;;
    getlogs )
	for i in $machines ; do
	    [[ ${linux/${i}//} == ${linux} ]] && echo -n "FreeBSD" || echo -n "Linux"
	    echo "----------------- $i --------------------"
	    sudo ssh $i "cat /var/emulab/logs/nodecheck.log"
	done
	;;
    clearlogs )
	for i in $machines ; do
	    sudo ssh $i "rm -f /var/emulab/logs/nodecheck.log"
	done
	;;
    help | -help | -h | ? )
	cat <<EOF
        Usage:
	Nodes to be operated on $machines
	[disk|mem|cpu|diskspeed|time|nic] Update node test and run
	[run] Update bootscript and run all the nodetests
	[boot] Update bootscript run the nodetests as if run at boot time
	[getlog] get and display nodecheck.log files
	[clearlog] clear the nodecheck.log files
        No argument will update all node with checknode scripts
EOF
	;;
    * )
    
for i in $linux
do
    echo -n "$i:rc.nodecheck-linux "
    sudo scp -p rc.nodecheck ${i}:/usr/local/etc/emulab/rc
#    sudo ssh ${i} chown root /usr/local/etc/emulab/rc/rc.nodecheck
#    sudo ssh ${i} chgrp root /usr/local/etc/emulab/rc/rc.nodecheck
done

for i in $bsd
do
    echo -n "$i:rc.nodecheck-bsd "
    sudo scp -p rc.nodecheck ${i}:/usr/local/etc/emulab/rc
#    sudo ssh ${i} chown root /usr/local/etc/emulab/rc/rc.nodecheck
#    sudo ssh ${i} chgrp wheel /usr/local/etc/emulab/rc/rc.nodecheck
done


for i in $machines
do
    echo "${i}:utils "
#    sudo rsync -a ${utils} ${i}:/usr/local/etc/emulab
    for u in ${utils} ; do
	scp -p $u ${i}:/usr/local/etc/emulab
    done
 #   sudo ssh ${i} "cd /usr/local/etc/emulab ; chown root ${utils} "
 #   sudo ssh ${i} "cd /usr/local/etc/emulab ; chgrp 0 ${utils} "

done

;;
esac
