#!/bin/sh


#--- Help function -------------
BACKENDS="xrd"
printhelp()
{
   echo "   "
   echo "  pq2-redistribute"
   echo "   "
   echo "  Purpose: execute the file movements as determined by pq2-ana-dist"
   echo "   "
   echo "  Syntax:"
   echo "           pq2-redistribute [-h|--help] --dryrun"
   echo "                            -b backend (--backend=backend)"
   echo "                            -f filemv (--filemv=filemv)"
   echo "                            -t tmpdir (--tmp=tmpdir)"
   echo "                            -r redirurl (--redir=redirurl)"
   echo "   "
   echo "   -h | --help   Print this help"
   echo "   --dryrun      Only show the action that should be done (don't execute them)"
   echo "   backend       Backend to be used; currently available: $BACKENDS"
   echo "   filemv        File with the file movement directives produced by pq2-ana-dist"
   echo "   redirurl:     URL of the redirector from where the files should be copied and deleted."
   echo "   tmpdir        Directory for temporary files [/tmp/<user>]."
   echo "   "
}

# Basic file movement function based on xrootd
movefilexrd()
{
    # This function will try to move file [$1] from redirector $2 to redirector/dataserver $3
    # using directory [$4] as intermediate location (default /tmp).
    # The result of the action is stored in mvfile_status ('ok' or 'ko').

    mvfile_status="ko"
    # Source and destination files
    FNAME=
    if test "x$1" = "x" ; then
       echo "movefile: file name undefined!"
       return 1;
    else
       FNAME="$1"
    fi
    SRC=
    REDIR=
    if test "x$2" = "x" ; then
       echo "movefile: source URL undefined!"
       return 1;
    else
       REDIR="$2"
       SRC="$REDIR/$FNAME"
    fi
    DST=
    DSURL=
    if test "x$3" = "x" ; then
       echo "movefile: destination URL undefined!"
       return 1;
    else
       DSURL="$3"
       DST="$DSURL/$FNAME"
    fi
    TMP=
    if test "x$4" = "x" ; then
       TMP="/tmp/"`basename $FNAME`
    else
       if test ! -d $4 ; then
          mkdir -p $4
          if test ! -d $4 ; then
             echo "movefile: unable to create missing directory '$4'"
          fi
       fi
       TMP="$4/"`basename $fNAME`
       if test -f $TMP ; then
          rm -f $TMP;
          if test -f $TMP ; then
             echo "movefile: unable to remove existing '$TMP'"
          fi
       fi
    fi
    echo "movefilexrd: moving $SRC to $DST via $TMP"

    # Copy SRC to TMP
    xrdcp $SRC $TMP
    if test ! "x$?" = "x0" ; then
       echo "movefile: xrdcp $SRC $TMP failed!"
       return 1;
    fi

    # Remove the file
    case "$REDIR" in
      root://*) hport=`echo "$REDIR" | sed 's/root:\/\///'` ;;
      *) hport="$REDIR" ;;
    esac ;
    xrd $hport rm  $FNAME
    if test ! "x$?" = "x0" ; then
       echo "movefile: xrd $hport rm  $FNAME failed!"
       return 1;
    fi

    # Copy the file to destination
    xrdcp $TMP $DST
    if test ! "x$?" = "x0" ; then
       echo "movefile: xrdcp $TMP $DST failed!"
       return 1;
    fi

    # Rebuild the xrootd dynamic cache
    xrd $hport existfile  $FNAME
    if test ! "x$?" = "x0" ; then
       echo "movefile: xrd $hport existfile  $FNAME failed!"
       return 1;
    fi

    mvfile_status="ok"
    return 0
}

REDIRURL=""
FILEIO=""
TMPDIR=""
BACKEND="xrd"
#
# Parse long options first
short_opts=
for i in $@ ; do
   opt=""
   case $i in
      --*) opt=`echo "$i" | sed 's/--//'` ;;
      *) short_opts="$short_opts $i" ;;
   esac
   if test ! "x$opt" = "x" ; then
      case "$opt" in
         *=*) oarg=`echo "$opt" | sed 's/[-_a-zA-Z0-9]*=//'` ;;
         *) oarg= ;;
      esac ;
      case $opt in
         backend=*)  BACKEND="$oarg" ;;
         dryrun)     DRYRUN="yes" ;;
         fileio=*)   FILEIO="$oarg" ;;
         help)       printhelp ; exit ;;
         redirurl=*) REDIRURL="$oarg" ;;
         tmp=*)      TMPDIR="$oarg" ;;
      esac
   fi
done

if test ! "x$short_opts" = "x" ; then
   while getopts b:f:r:t:h i $short_opts ; do
      case $i in
      b) BACKEND="$OPTARG" ;;
      f) FILEIO="$OPTARG" ;;
      h) printhelp ; exit ;;
      r) REDIRURL="$OPTARG" ;;
      t) TMPDIR="$OPTARG" ;;
      \?) printhelp; exit 1 ;;
   esac
   done
fi

# Check backend availability
HAVEBACKEND="no"
for b in $BACKENDS ; do
   if test "x$BACKEND" = "x$b" ; then
      HAVEBACKEND="yes"
   fi
done
if test "x$HAVEBACKEND" = "xno" ; then
   echo "Backend '$BACKEND' not supported: sorry!"
   printhelp
   exit
fi

# Check consistency
if test "x$BACKEND" = "xxrd" && test "x$REDIRURL" = "x"; then
   echo "Specifying a redirector URL is mandatory for a $BACKEND backend"
   printhelp
   exit
fi

# Check input file
if test "x$FILEIO" = "x" ; then
   echo "Specifying the file with input information is mandatory"
   printhelp
   exit
fi
if test ! -f $FILEIO; then
   echo "Specifyed file $FILEIO does not exist"
   printhelp
   exit
fi

while read -r ff src dst; do
   if test "x$BACKEND" = "xxrd" ; then
      if test "x$DRYRUN" = "xyes" ; then
         echo ">> $BACKEND: would move $REDIRURL/$ff to $dst/$ff via $TMPDIR"
      else
         movefilexrd $ff $REDIRURL $dst $TMPDIR
      fi
   else
      echo "Backend $BACKEND not supported"
   fi
done < $FILEIO
