#!/bin/ksh
# @Object: Checkout library version of a source file
# @Author: S.Chamberland
# @Date:   March 2014
. .rdebase.inc.dot

DESC='Checkout library version of a source file'
USAGE="USAGE: ${MYSELF} [-h] [-v] [-f] FILES"

usage_long() {
	 toto=$(echo -e $USAGE)
	 more <<EOF
$DESC

$toto

Options:
    -h, --help         : print this help
    -v, --verbose      : verbose mode
    -q, --quiet        : silence every comment and errors
    -t, --touch        : touch file even if already checked out
    -f, --force        : force replace existing file
        --ext          : No error if file.ext not in ext list
    FILES              : space separated list of src files or dir

EOF
}

mystatus=0
verbose=1
myquiet=''
myforce=0
mytouch=0
myextlist=""
while [[ $# -gt 0 ]] ; do
   case $1 in
      (-h|--help) usage_long; exit 0;;
      (-v|--verbose) ((verbose=verbose+1));;
      (-q|--quiet) ((quiet=1));;
      (-f|--force) myforce=1;;
      (-t|--touch) mytouch=1;;
      (--ext=*) myextlist="$myextlist ${1#*=}";;
      (--ext) ;;
      (--) shift ; break;;
      (-*) myerror "Option Not recognized: $1";;
      *)
         case $previous in
            (--ext) myextlist="$myextlist $1";;
            *) PATHLIST="$PATHLIST $1";;
         esac ;;
    esac
    previous=$1
    shift
done
PATHLIST="$PATHLIST $@"
SRCPATH="$(mysrcpath)"
if [[ $quiet == 1 ]] ; then
   verbose=0
   myquiet='-q'
fi

#rde_exit_if_not_rdetopdir

# find_src MYFILE
find_src() {
   _myfile=$1
   for _mydir in $SRCPATH ; do
      #TODO: look into sub-dir tree
      if [[ -r ${_mydir}/${_myfile} ]] ; then
         echo ${_mydir}/${_myfile}
         return 0
      fi
   done
   return 1
}

for myfile in $PATHLIST ; do
   if [[ x"$myextlist" != x"" ]] ; then
      ext=${myfile##*.} 
      if [[ x$ext == x$myfile ]] ; then ext="" ; fi
      isSrc="$(echo $myextlist | grep -F .${ext:-_______})"
      if [[ x"$isSrc" == x ]] ; then
         exit 0
      fi
   fi
   mypathlist="$(find_src $myfile -L 2>/dev/null)"
   if [[ x${mypathlist} == x ]] ; then
      mysimfile="$(.rdesimilnames $myquiet $myfile)"
      mynbsim="$(echo $(echo $mysimfile | wc -w))"
      if [[ x$mynbsim == x1 && ${mysimfile%.*} ==  $myfile ]] ; then
         mypathlist="$(find_src $mysimfile -L 2>/dev/null)"
      fi
   fi
   if [[ x${mypathlist} == x ]] ; then
      mystderr 1 "ERROR: File $myfile is not found in SRCPATH ($SRC_PATH_FILE)"
      mystderr 1 "Maybe you meant one of: $mysimfile"
      mystatus=1
   else
      for mypath in ${mypathlist} ; do
         topath=${mypath##*/}
         myecho 1 "Retrieving $mypath"
         isdir=0
         replaced=""
         if [[ -e ${ROOT}/$topath ]] ; then
            if [[ $myforce == 0 ]] ; then
               mypath=""
               mystderr 1 "WARNING: File $topath already exists, overwrite with:"
               mystderr 1 "${MYSELF} -f $myfile"
               mystatus=1
            else
               rm -f ${ROOT}/$topath
               replaced="(overwritting existing file)"
            fi
         fi

         if [[ x$mypath != x ]] ; then
            cp $mypath ${ROOT}/$topath
            chmod -R u+w ${ROOT}/$topath
            if [[ x${replaced} != x ]] ; then
               touch ${ROOT}/$topath
            fi
            myecho 1 "Checking out ${topath} ${replaced}"
         fi
         if [[ -e ${ROOT}/$topath && $mytouch == 1 ]] ; then
            touch ${ROOT}/$topath
            myecho 1 "Touch ${topath}"
         fi
      done
   fi
done

exit $mystatus
