#!/bin/csh
#----------------------------
# strip metadata from a file
# usage: mdstrip pathname
#----------------------------
# MacOS X maintains a metadata tree within its file system.
# Here the Finder and other programmes can deposit and retrieve information
# about files that cannot be accommodated within the file system itself (e.g.
# the creation date of a file, or the URL whence it was downloaded).
# When a file is moved or copied within the Finder, these metadata stay
# entangled with the file. Sometimes, however, it is desirable to get rid
# of this information. That is what this script attempts to do.
#
# written drz 20080714
# mod.	  drz 20080909	reset directory date
#
if($#argv > 0) then
  set the_path = "$argv[1]"
  if ( -f "$the_path" ) then
    if ( -w "$the_path" ) then
      set the_dir  = "$the_path:h"
      set the_file = "$the_path:t"
      set the_extn = "$the_file:e"
# save creation/modification dates
# (this requires the Developer Tools to be installed)
      set the_cdat = `GetFileInfo -d "$the_path"`
      set the_mdat = `GetFileInfo -m "$the_path"`
      set the_ddat = `GetFileInfo -m "$the_dir"`
# does the file name have an extension ?
      if ( ${%the_extn} > 0 ) then
	set scratch = scratch.$the_extn
      else
	set scratch = scratch
      endif

# copy file contents 
      cat "$the_path" > $scratch
      if ( $status == 0 ) then
# transfer modification dates to copied file
	touch -r "$the_path" $scratch
# destroy all evidence
	rm "$the_path"
# gimme back my old name
	mv $scratch "$the_path"
# and my birth certificate (requires Developer Tools, too)
	SetFile -d "$the_cdat" "$the_path"
	SetFile -m "$the_mdat" "$the_path"
# finally, fix the directory's modification date
	SetFile -m "$the_ddat" "$the_dir"
#	touch -r "$the_path" "$the_dir"
      else
	echo cannot write $scratch - giving up
	exit 3
      endif
    else
      echo "$the_path" is not writable
      exit 2
    endif
  else
    echo "$the_path" is not a plain file
    exit 1
  endif
else
  echo $0:t needs a pathname as an argument
endif
exit 0
