#!/bin/sh
# @(#)sfmvfs	1.2 09/30/92 CERN-SW/DC Fabrizio Cane
#
# Copyright (C) 1990,1991 by CERN/CN/SW/DC
# All rights reserved 
#
USAGE="usage: sfmvfs [-yvmuodlpr] pool host:/path"
#
#  Initialize 
#
LOGERR="sfmvfs.error"
LOGMOVE="sfmvfs.moved"
POSTFIX=".sfmoving"
PREFIX="sfmoved"
SHIFTBIN="/usr/local/bin"
ULTRABIN="/usr/local/bin/ultra"
#
#  -y   use yellow page 	     ( by default /etc/passwd and /etc/group are used )
#  -v   verbose mode
#  -m   keep mounted the file system 		( by default it's remounted read only )
#  -u   use the ultra rcp 				      ( by default cp is used )
#  -o   the file system is spread over a different pool ( by default the file system
#	is supposed to be spread over the pool it belongs )
#  -d   the server and pool declaration is avoided
#
#  -l   the original files are left on disk 				   ( see sfrn )
#  -p   the original files are renamed using the $PREFIX 		   ( see sfrn )
#  -r   the original files are removed 					   ( see sfrn )
#
ypage=0
verbose=0
mounted=0
ultracp=0
inpool=1
declaration=1
leave=0
prefix=0
remove=0
#
cwd=`pwd`
#
echo "\n\nsfmvfs $*" | tee -a $LOGMOVE >> $LOGERR
echo "AT" `date` "\n" | tee -a $LOGMOVE >> $LOGERR
#
#  Check the number of arguments in the command line
#
if [ $# -lt 3 -o $# -gt 9 ]
then
     echo $USAGE
     exit 1
fi
#
#  Get the options [-yvmuodlpr]
#
set -- `getopt yvmuodlpr $*`
if [ $? != 0 ]
then
     echo $USAGE
     exit 1
fi
for k in $*
do
     case $k in
       -y)  ypage=1; shift;;
       -v)  verbose=1; shift;;
       -m)  mounted=1; shift;;
       -u)  ultracp=1; shift;;
       -o)  inpool=0; shift;;
       -d)  declaration=0; shift;;
       -l)  leave=1; 
	    if [ $remove = 1 -o $prefix = 1 ] 
	    then 
		echo $USAGE
	        exit 1
	    fi; 
	    RENAME='-l'; 
	    shift;;
       -p)  prefix=1;
	    if [ $leave = 1 -o $remove = 1 ]
	    then
		echo $USAGE
		exit 1
	    fi;
	    RENAME='-p'+" "+$PREFIX;
	    shift;;
       -r)  remove=1; 
	    if [ $leave = 1 -o prefix = 1 ] 
	    then 
		echo $USAGE
	        exit 1
	    fi;
	    RENAME='-r';
	    shift;;
       --)  shift; break;;
     esac
done
#
#  Check the number of arguments in the command line that are not options
#
if [ $# != 2 ]
then
     echo $USAGE
     exit 1
fi
#
#  Get from the command line the pool name
#
pool=$1
#
if [ $declaration = 1 ]
then
#
#  Get from the command line the file system path to move
#
OLDIFS=$IFS
IFS=:
num=0
for k in $2
do
     if [ $num = 0 ]
     then
         server=$k
     elif [ $num = 1 ]
     then
         fsys=$k
     fi
     num=`expr $num + 1`
done
IFS=$OLDIFS
if [ $num != 2 ]
then
     echo "Syntax error in the file system name <$2>"
     exit 1
fi
else
fsys=$2
fi
#
if [ $declaration = 1 ]
then
#
#  Look for the file system declaration in the 'shift configuration file'
#
found=0
foundp=0
check=0
checkp=0
belong=0
belongp=0
set -- `sfmake -read -pools` > /dev/null
for k in $*
do
     case $k in
       SERVER)  if [ $2 = $server ] 
		then
		     mountpath=$3
		     found=1
		     check=1
		elif [ $check = 1 ]
		then
		     check=0
		fi;;
       POOL)    if [ $check = 1 ]
		then
		     check=0
		fi
		if [ $2 = $pool ]
		then
		     foundp=1
		     checkp=1 
		elif [ $checkp = 1 ]
		then
		     checkp=0
		fi;;
     esac
     if [ $check = 1 -a $k = $fsys ]
     then
	  belong=1
     fi
     if [ $checkp = 1 -a $k = $server ]
     then
	if [ $inpool = 1 -a $2 = $fsys ]
	then
	     belongp=1
	fi
     fi
     shift
done
#
if [ $found = 0 ]
then
     echo "SERVER <$server> not declared in <$SHIFTPOOLS>"
     exit 1
fi
if [ $belong = 0 ]
then
     echo "File system <$fsys> doesn't belong to SERVER <$server>"
     exit 1
fi
#
if [ $foundp = 0 ]
then
     echo "POOL <$pool> not declared in <$SHIFTPOOLS>"
     exit 1
fi
if [ $inpool = 1 -a $belongp = 0 ]
then
     echo "File system <$server:$fsys> doesn't belong to POOL <$pool>"
     exit 1
fi
fsyspath=$mountpath$fsys
else
fsyspath=$fsys
fi
#
echo "Moving file system <$fsyspath> into the pool <$pool>" | tee -a $LOGERR | tee -a $LOGMOVE
#
if [ $declaration = 1 ]
then
    sfmake -wlock $server:$fsys
fi
#
if [ $mounted = 0 ]
then
# 
reply=1
while [ $reply != 0 ]
do
     set -- `umount $fsyspath`
     status=$?
     if [ $status != 0 ]
     then
          sleep 1
     elif [ $# != 0 ]
     then
	  exit
     else
	  reply=0
     fi
done
echo $fsyspath unmounted
#
set -- `mount -r $fsyspath`
if [ $? != 0 -o $# != 0 ]
then
     exit 1
fi
echo $fsyspath mounted READ ONLY
#
fi
#
echo "\nList of files directories and soft links not moved\n" >> $LOGERR
echo "\nList of shift files moved\n" >> $LOGMOVE
#
#  Perform an 'ls' in the file system directory '$fsyspath' 
#  and get all valid group subdirectories
#
gdirs=`ls $fsyspath`
if [ $? != 0 ]
  then
    echo "Bad file system <$fsyspath>"
    exit 1
fi
for g in $gdirs
do
  gdir=$fsyspath"/"$g
  if [ -d $gdir ] 
  then
    if [ $ypage = 1 ]
    then
	ypmatch $g group > /dev/null 2>&1
    else
        grep $g /etc/group > /dev/null 2>&1
    fi
    if [ $? = 0 ]
    then
#
#  This is a valid group subdirectory
#  Perform an 'ls' on it and get all valid user subdirectories
#
      udirs=`ls $gdir`
      for u in $udirs
      do
	udir=$fsyspath"/"$g"/"$u
	if [ -d $udir ]
	then
	  if [ $ypage = 1 ]
	  then
	      ypmatch $u passwd > /dev/null 2>&1
	  else
	      grep $u /etc/passwd > /dev/null 2>&1
	  fi
	  if [ $? = 0 ]
	  then
#
#  This is a valid user subdirectory
#  (i.e. '$udir' has the format /filesystem-path/group-name/user-name )
#  Perform a recursive 'find' to get all files
#
	    if [ $verbose != 0 ] 
	    then
		echo "Scanning $udir"
	    fi
	    cd $udir
	    userfiles=`find . -type f -print`
	    cd $cwd
#
#  For each file do a 'shift move'
#
	    for dotf in $userfiles
	    do
	      OLDIFS=$IFS
	      IFS="/"
	      set -- `echo $dotf`
	      IFS=$OLDIFS
	      f="."
	      for k in $*
	      do
	        if [ $f = "." ]
		then
		     f=$k
		else
		     f=$f"/"$k
		fi
	      done
	      echo "Moving  $udir/$f"
#
	      set `ls -ls $udir"/"$f`
	      owner=$4
	      size=`expr $6 / 1048576 + 1`
	      fnew=$f$POSTFIX
	      first=1
	      sfmoving=`su $owner -c "$SHIFTBIN/sfget -p $pool -u $u -l $f"`
	      reply=$?
	      if [ $reply = 0 ]
	      then
		echo "File $udir/$f already exists in pool $pool -> $sfmoving" | tee -a $LOGMOVE
		continue
	      fi
	      while [ $reply -gt 1 -o $first != 0 ]
	      do
		if [ $first != 1 ]
		then
			sleep 20
		else
			first=0
		fi
#		echo su $owner -c umask 0 ";" $SHIFTBIN/sfget -p $pool -u $u -s $size $fnew
		sfmoving=`su $owner -c "umask 0 ; \
			$SHIFTBIN/sfget -p $pool -u $u -s $size $fnew"`
		reply=$?
#		echo sfget -p $pool -u $u -s $size $fnew reply is $reply
		if [ $reply = 1 ] 
		then
		    break
		fi
	      done
#
#		echo $sfmoving
#		ls -ls "$sfmoving"
#		echo $udir"/"$f 
#		ls -ls $udir"/"$f 
#
	      if [ $ultracp = 1 ]
		then
	  	  OLDIFS=$IFS
	          IFS="/"
	      	  set -- `echo $sfmoving`
	      	  IFS=$OLDIFS
		  $ULTRABIN/rcp $udir"/"$f $2:$sfmoving
	        else
	          cp $udir"/"$f $sfmoving
	      fi
	      $SHIFTBIN/sfrn $RENAME $udir"/"$f $sfmoving
	      if [ $? != 0 ]
	      then
		   echo "$SHIFTBIN/sfrn $RENAME $udir"/"$f $sfmoving failed" | tee -a $LOGERR
		   rm $sfmoving
              else
	           echo "$udir"/"$f\t\t$sfmoving" >> $LOGMOVE
#		   su $owner -c "$SHIFTBIN/sfrm -p $pool -u $u $sfmoving"
	      fi
#
	    done
#
	    echo "List of soft links NOT moved (link move not implemented yet)"
	    find $udir -type l -print | tee -a $LOGERR
	    if [ $verbose != 0 ] 
	    then
	    	echo ""
	    fi
	  else
	      rmdir $udir > /dev/null 2>&1
	      if [ $? != 0 ]
	      then
	  	echo "Directory <$udir> NOT moved (not user directory)" \
								| tee -a $LOGERR
	      fi
	  fi
  	else
	      echo "File <$udir> NOT moved (not shift file)" | tee -a $LOGERR
	fi
      done
    else
	rmdir $gdir > /dev/null 2>&1
	if [ $? != 0 ]
	then
	  echo "Directory <$gdir> NOT moved (not group directory)" | tee -a $LOGERR
	fi
    fi
  else
	echo "File <$gdir> NOT moved (not shift file)" | tee -a $LOGERR
  fi
done
#
#
#
exit 0
#
#
