#!/bin/tcsh -f
echo "Join files ${1}* into ${2}"
##################################################
## joinFiles v1.0 by JC
## ----------
## the file "$2" (called 'myFile') was splitted (in 10Ko files) with:
## $> split -b10000 myFile myFile.splitted
## and created myFile.splittedaa, myFile.splittedab, myFile.splittedac... of 10Ko each
##
## now, let's join them into 1 file; with:
## $> joinFiles myFile.splitted myFile
##################################################


###################################################
## test if destination file already exists

if (`filetest -e ${2}`) then
  echo "### WARNING: file <$2> exists!"
  echo -n "    Erase old file? [Yes/No](N)"
  set CHOICE=$<
  if( "$CHOICE" == "Y" || "$CHOICE" == "y") then
    echo "removing old file: <$2>"
    rm -f $2
  else
    exit 0
  endif
endif

set FILES=`ls ${1}*`
echo $FILES

###################################################
## check that source files exist

if ("$FILES" == "") then
  echo "### ERROR: No file such as <${1}*> was found..."
  exit 0
endif


echo "Joining files..."
echo "------ Go!"

###################################################
# let's do some stuff

foreach NAME ($FILES)
  echo -n "${NAME}:"
  cat $NAME >> $2
  echo " OK!"
end
echo "------ Done!"

exit 1