.key DEVICE/A,LIST/S,ICONDIR/K
.BRA (
.KET )
;# 
;# SFSOptimize 0.8 by P-O Yliniemi <peoyli@algonet.se>
;#
;# $VER: SFSOptimize 0.8 (21.12.100) © 2000-2001 by P-O Yliniemi
;#
;# A tool for SFS file systems to check the file system, remove
;# deleted files (clean .recycled directory), generate a file list,
;# rewrite the icons (after archiving them, removing them, and
;# defragmenting the file system) and then defragment again.
;# 
;# Uses a special version of SFSOpenDefragmentGUI, which opens
;# a much smaller, inactive window, and exits when it is done.
;#
;# If available, it uses the GNU / GG 'find' command to generate
;# file lists (because of its compatibility with symbolic links).
;#
;# Otherwise the AmigaOS 'list' command is used to generate a list
;# that almost looks like the 'find' output, but it will fail when
;# it reaches a symbolic link.
;#
;# Parameters:
;#   DEVICE      - Device to perform the actions on (required)
;#
;#   LIST        - Generate a list of all the files and store as :.filelist
;#   ICONDIR     - Temporary location for icons, will otherwise be the work drive
;#

failat 200

set error 0
set canoptimize 0

which sh >nil:
if not warn
  which tar >nil:
  if not warn
    which find >nil:
    if not warn
      set canoptimize 1
    endif
  endif
endif

echo noline "Checking [(DEVICE)]..."
c:SFS/SFSCheck (DEVICE) >t:sfscheck.out
if fail
  echo "Error!"
  type t:sfscheck.out >"CON:0/0/640/200/SFSCheck Result - error/AUTO/CLOSE/WAIT"
  set error 1
else
  echo "OK"
  set error 0
endif

if $error eq 0
  if $canoptimize eq 1
    if (ICONDIR)
      set icondir (ICONDIR)
    else
      set icondir (DEVICE)
    endif

;   ; -- Always clean .recycled if optimizing
    echo noline "Removing stuff from [(DEVICE).recycled]..."
    delete >nil: (DEVICE).recycled/#? quiet
    echo "done"

;   ; -- list is created after removing contents of .recycled and before removing icons
    if (LIST)
      echo noline "Generating file list for [(DEVICE)]..."
      which find >nil:
      if warn
        list all (DEVICE) lformat "%f%n" >(DEVICE).filelist files
      else
        find (DEVICE) -type f -o -type l >(DEVICE).filelist
      endif
      echo "done"
    endif

;   ; -- Archive icons and store them in the specified directory or on the device
    echo noline "Archiving icons..."
    set oldcd `cd`
    cd (DEVICE)
    delete t:icons.tar >nil:
    sh -c "find . -iname \"*\.info\" -type f -exec tar rf t:icons.tar {} \;" >nil:
    copy t:icons.tar $icondir
    delete t:icons.tar >nil:
    echo "done"

;   ; -- Remove all icons
    echo noline "Removing icons from disk. If something goes wrong, your icons is archived in $icondir as icons.tar ..."
    sh -c "find . -iname \"*\.info\" -type f -exec delete {} \;" >nil:
    echo "done"
    cd $oldcd

;   ; -- Clean up .recycled again (after deletion of icons)
    echo noline "Removing stuff from [(DEVICE).recycled]..."
    delete >nil: (DEVICE).recycled/#? quiet
    echo "done"

;   ; -- Defragment file system
    echo noline "Defragging [(DEVICE)]..."
    c:SFS/SFSDefragmentGUI (DEVICE)
    echo "done"

;   ; -- Put back the icons and delete the stored icon archive
    cd $icondir
    copy icons.tar t:
    cd (DEVICE)
    echo noline "Putting back icons in place..."
    tar xf t:icons.tar
    echo "done"
    delete icons.tar

;   ; -- Clean up .recycled again (after deletion of icons archive)
    echo noline "Removing stuff from [(DEVICE).recycled]..."
    delete >nil: (DEVICE).recycled/#? quiet
    echo "done"

;   ; -- Defragment file system again after the icons are in place
    echo noline "Defragging [(DEVICE)]..."
    c:SFS/SFSDefragmentGUI (DEVICE)
    echo "done"
    cd $oldcd
  else
    echo "Can't optimize because either 'find', 'tar' or 'sh' is missing"
  endif
else
  echo "Can't optimize because SFSCheck returned an error!"
endif

unset error
