#! /bin/csh


# v1.2:  standalone export tool, as well as import tool.
# v1.3:  incorporates hfspax, so that finder info and rsrc forks are preserved.
# v1.4	 compression of hfspax archive
#		This version no longer works on OS-10.5 :
#		cpio complains about the header-size.  No newer version of hfspax.
# v2.0	27 Aug 08
# 	     Zip files utilized rather than hfspax/gzip, now that Apple has
# 	     zips that support resource forks
# v2.1  12 Mar 12
#	    OsX "cp" now preserves mac linefeeds, rsrc forks, etc, so removing CpMac
# v2.2	21 Jun 15
#	     xz backup copy rather than gzip
# v2.3  07 Jul 15
#	     files not in fsynch_list generate a meaningful error message


set IE = $1

TOP:
switch ($IE)

case -h* :
    cat << END_OF_HELP_TXT

${0:t}:

  a script to aid in synchronizing file between work and home by moving 
  updated files to their correct location, as specified in ~/.fsynch_list .
 
  use:	fsynch -e		to export synch-files
 	fsynch -i		to import synch-files
 
  associated files:
        ~/.fsynch_list		a list of files to be synched, with full paths
        ~/.fsynch_timestamp	date of last import/export

END_OF_HELP_TXT
    breaksw


# Import: detect "synch.zip" archive, expand it in ~/inq...

case -i:
    cd ~/inq
    if ( -f ~/in/synch.zip ) then
	    ls | grep Synch
	    if ($status == 0) then
	    	echo "fsynch: old synch file exists.  Rerun to install new files."
		breaksw
		endif
	    mv ~/in/synch.zip .
	    unzip -q synch.zip  &&  rm synch.zip
	else if ( -f synch.zip) then
	    unzip -q synch.zip  &&  rm synch.zip
	else if ( ! -d Synch ) then 
	    printf "\nsynch:  no Synch archive detected.   Aborting.\n"
	    breaksw
	endif

#      Change permissions of qd_list to accord with mail client
#          (sendmail=644 vs postfix=600)
#    if (-e Synch/qd_list) chmod 600 Synch/qd_list


    printf "\nfiles synchronized:\n"

#       ...then file its contents based on ~/.fsynch_list .
    foreach newfile ("`ls -A Synch`")
	set oldfile = "`grep -h /${newfile}'\>' ~/.fsynch_list | sed 's,~,$HOME,'`"
	if ( "$oldfile" == "" ) then
	    echo "   $newfile not in fsynch_list -- skipping"
	    continue
	    endif
	if (! -f "$oldfile") then
	    echo "   no old file -- skipping $newfile"
	    continue
	    endif
	set archfile = `dirname "$oldfile"`/."$newfile.bak.xz"
	xz -c "$oldfile" >! "$archfile"
	touch -r "$oldfile" "$archfile"
	cp Synch/${newfile} "$oldfile"
	if ($status == 0) then 
	    /bin/rm Synch/${newfile}
	    echo "	$oldfile"
	  else
	    echo "  -->	$oldfile"
	    endif
	end

    rmdir Synch
    touch -t `date +%Y%m%d%H%M.59` ~/.fsynch_timestamp
    breaksw



#  Export: Create Synch directory.  Add new files, and make (or append
#          to) zip archive.

case -e:

    if (! -f ~/.fsynch_timestamp) then
    	printf "\nfsynch:  ~/.fsynch_timestamp not found.   Aborting\n"
	breaksw 
	endif

    if (! -d ~/outq/Synch) mkdir -p ~/outq/Synch
    cd ~/outq

    foreach file ("`cat ~/.fsynch_list | grep -v '^#' |  sed s,~/,$HOME/,`" )
	find "$file" -type f -newer ~/.fsynch_timestamp -exec cp "{}" Synch/`basename "${file}"` \;
	end

    zip -rTq synch.zip Synch  &&  /bin/rm -r Synch
    touch ~/.fsynch_timestamp

    breaksw


#	  Try again if import/export not specified on command line
default:
    echo -n "	i)mport or e)xport files?    "
    set IE = -$<
    echo ""
    goto TOP

endsw

echo ""
#									vi: tw=0
