#native_company# #native_desc#
#native_cta#

Archiver 1.0

By Kevin Kinsey
on May 28, 2005

Version: 1.0.1

Type: Full Script

Category: File Management

License: BSD License

Description: What happens when a PHP hacker tires of junk filling up his $HOMEDIR, or wants some disk space back, but hasn’t the heart to call “rm -f”.

A simple command-line utility, it appears pretty useless, but I’ve found it helpful. Plenty of room for expansion/improvement, but does a fine job of what it’s designed for — it’s just a simple idea, that’s all 🙂

#! /usr/local/bin/php
<?

/***********************************************************

   "Archive", by dalecosp.  Use it to clean up your $HOMEDIR, 
or your webroot, whatever.  Easy script, this moves files 
specified by the script arguments to a configurable $ARCHIVEDIR, 
optionally compressing the file(s) in the process.  If your 
shell supports wildcards/globbing (and whose doesn't, to some 
extent? Shame on them!) or if you just like typing lots of 
filenames, you can give it as many files as you wish at once.

   To use this script, put it in your $PATH, and call it
from the CLI, thusly:

# archive somefile

   See the section after the COPYRIGHT to modify this to
work on your system/environment.

   You are welcome, if you hack PHP, to modify this and
improve it.  There's lots that could be done, but I fear
to add too many features 'cause it's likely that I am just
reinventing the wheel anyway, and I've got other work to do.
Certainly, to operate in UNIX tradition, we'd need a "-v"
and a "-q" option (verbose and quiet), perhaps a "-c" option
or a "--version".  I'm sure someone would like to have support
for zip/PKZIP, too, but I'm not to "up on that, and like I
said, I've other projects.

   Note that I've not tested this for *lots* of files,
so I make no claims about its performance (heck, I make no
claims about it whatsoever.  But I do use it ;-).  Also, read
the paragraphs below:


COPYRIGHT/LICENSE/DISCLAIMER:

Copyright (C) 2005 by "dalecosp".

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
   notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
   notice, this list of conditions and the following disclaimer in the
   documentation and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
SUCH DAMAGE.

***********************************************************/

############## USER CONFIGURATION ##########################

# Path to move command; if you don't know where mv(1) is, try 
# `whereis mv` at your shell prompt.
   $move="/bin/mv";

# Paths to various compression programs.
   $compress="/usr/bin/compress";
   $gzip="/usr/bin/gzip";
   $bzip2="/usr/bin/bzip2";

# We'll need this later, I guess ;-)
   $dir=getcwd();

# Where do you want your files?  If you leave it empty, we'll
# put your files in an "archive" subdirectory of your current
# location (the location you call us from) - which is my preference.
   $ARCHIVEDIR='';

########### END OF CONFIG OPTIONS  #########################

########### DON'T EDIT BELOW HERE  #########################
####### (unless you know what you're doing!) ###############

function usage() {
   echo "Usage: archive [-Z -gz -bz2] filename1 ... filename[n]
   This will move the file(s) to ./archive/ 
   (and optionally compress the file(s)). n";
   exit;
}

# An array of possible compress options

$zips=array("-Z", "-gz", "-bz2");

# At present we insist that we only call one compression option

if (in_array($argv[1], $zips)&&in_array($argv[2], $zips)) {
   echo "You can't call more than one compression option! n";
   usage();
}

# You can't call us without a file to operate on!

if ($argv[1]=='' || (in_array($argv[1], $zips) && $argv[2]=='')) {
   echo "You must call a filename! n";
   usage();
}

if (in_array($argv[1],$zips)) {
   $zipping=1;
   switch($argv[1]) {
      case "-Z":
         $zipper=$compress;
         break;
      case "-gz":
         $zipper=$gzip;
         break;
      case "-bz2":
         $zipper=$bzip2;
         break;
   }
}

# Here's the main() thing!

foreach ($argv as $argument) {
   if ($ARCHIVEDIR=='') {
      $ARCHIVEDIR="$dir/archive";
   }
   if (!is_dir($ARCHIVEDIR)) {
      mkdir($ARCHIVEDIR);
   }
   if ($argument != $argv[0] && (!in_array($argument,$zips))) {     
      echo "Archiving ./".$argument."n";
      system("$move "$dir/$argument" "$ARCHIVEDIR/$argument"",$return);
      if ($return==0) {
         echo "Move successful.n";
      } else {
      echo "Move failed!n";
      }
      if ($zipping) {
         system("$zipper "$ARCHIVEDIR/$argument"",$return2);
      if ($return2==0) {
         echo "File compressed.n";
      } else {
      echo "Compression failed on $ARCHIVEDIR/$argument!
	If you called archive with the -Z option check that file is not empty. n";
      }

      }
   }
}

?>