#! /bin/tcsh

# A handful of cleanup items that get run overnight.

# ...Purge downloaded files older than one week
   foreach dir (~/Downloads)
      if (-d $dir) then
	cd $dir
	 find    . -fstype local -type f -ctime +6 -mtime +6 -exec rm -f -- {} \;
	 find    . -fstype local -type l -ctime +6           -exec unlink {}   \;
	 find -d . -fstype local -type d -empty ! -name .    -exec rmdir -- {} \;
	   # The find statements above don't descend into .pkg files.  Kludge below.
      endif
   end

   # .pkg files have creation/modification dates set to *original*
   # creation date (not download date).  Nonetheless, the lines
   # above don't want to touch them.  Last Access date no help
   # either, as it is touched every time it is sampled.  Hence,
   # we'll create some meta-data.

   @ today 	= `date +%s`
   @ NextWeek	= $today + 630000

   cd ~/Downloads
   foreach filetype (.pkg .mpkg)
	find *$filetype >& /dev/null
	if ($status == 0) then
	    foreach file (*$filetype)
		set ItemDate = `xattr -p PurgeDate $file |& grep -v "No such"`
		if ( "$ItemDate" == "") then
		    xattr -w PurgeDate $NextWeek $file
		else if ($today > "$ItemDate") then
		    mv $file ~/.Trash
		endif
	    end
	endif
   end


# Turn system volume down to something reasonable
    # OS 10.8:
    #	osascript -e "set volume output volume 35"
    # OS 10.11:
    #	 line above won't work since no tty attached.  Kludge:
	 sudo -E -u `ls -l /dev/console | awk '{print $3}'` \
	     osascript -e "set volume output volume 45"



# Return screen to reasonable brightness
#	Jeff Drake, <http://emptysodacan.blogspot.com/2012/08/applescript-set-display-brightness.html>
#	        nb: values range 0-1, = 16 clicks of the brightness-button.
osascript << EOT
    tell application "System Preferences"
	activate
	set current pane to pane "com.apple.preference.displays"
	tell application "System Events"
	    set value of slider 1 of group 1 of tab group 1 of window 1 of process "System Preferences" to 0.6
	end tell
	quit
    end tell
EOT



# check that backup disk is mounted...
df | grep -q "/Volumes/Backup"
if ($status != 0 ) then 
    printf "\nBackup Disk not Mounted.  Remounting ...\n\t"
    diskutil mount `diskutil list | grep Backup | awk '{print $6}'`
    if ($status != 0 ) printf "PROBLEMS ENCOUNTERED\n\n"
endif

# ... and eject disks which often get left mounted...
#		(neither diskutil nor disktool works w/ staffshares)
foreach disk (staffshares "Organic Lab")
    if -e "/Volumes/$disk" umount "/Volumes/$disk"
end




# Build a locate database for user account
#    requires a modified version of /etc/locate.rc to be stored
#	in ~/.locate.rc
#    to access the database:
#       alias myfiles 'gzcat ~/.locate.db.gz | locate -d - '
#       then use "myfiles" similarly to "locate"
#-----
# From the user account:
#    env LOCATE_CONFIG=$HOME/.locate.rc /usr/libexec/locate.updatedb
#    gzip -f $HOME/.locate.db
#-----
# From a periodic script:
#
#  OS 10.5
#   chmod 600 /Users/xyz/.locate.db || rc=3
#   echo "LOCATE_CONFIG=/Users/xyz/.locate.rc /usr/libexec/locate.updatedb" \
#      | su -fl xyz || rc=3
#   chmod 400 /Users/xyz/.locate.db || rc=3
#
#  OS 10.11
    chmod 600 /Users/xyz/.locate.db || rc=3
    #  assuming xyz runs csh, not bash
    sudo -E -u xyz \
       env LOCATE_CONFIG=/Users/xyz/.locate.rc /usr/libexec/locate.updatedb \
       || rc=3
    chmod 400 /Users/xyz/.locate.db || rc=3




# Create  database of hard-link database
  # nb/ this requires root permission, so it goes in /etc/daily.local

  echo "Updating hard-links database:"
  cat /dev/null > /var/db/HardLinks.db
  # !! Do not (accidentally) include Time-Machine Backup Drive !!
  #    /dev causes some grumbling, so omit it, too.
  #    Likewise, watch out for printer-drivers -- tons of links.
  for dir in \
    /BAK /Library /System /Users /bin /private/etc /opt /sbin /usr
  do
    find $dir ! -links 1 -type f -print0 | xargs -0 ls -1i >> /var/db/HardLinks.db
  done
  /opt/local/bin/xz -f /var/db/HardLinks.db
  chmod 644 /var/db/HardLinks.db.xz

  # The database may be accessed through the following csh alias:
  #   alias links	'set file = ("`ls -i \!* | cut -f1 -d\ `" /dev/null) ; \
  #	     xzcat /var/db/HardLInks.db.xz | grep $file[1] ; unset $file'
  # usage:
  #     to return links to ./foo/myfile, enter
  #     	% links foo/myfile  
