#!/bin/sh
#
# $Id: cleandb.in,v 1.2 2001/04/30 09:31:57 kreator Exp $
# Copyright (C) 1999 Patrick Alken
#
# This script will remove all backup directories created
# from past months. Directories created in the current month
# will not be touched. The idea is to keep the backup
# directory looking neat, instead of having hundreds of
# different directories with similar database backups :-)
#
# Usage: ./cleandb [--help | --confirm] [backup-directory]
#
# The --confirm option will cause the script to ask for
# confirmation for each directory being deleted.

# Set this to the default backup directory
BackupDir="/usr/hybserv/backup/"

# Don't touch anything below here

# Initialize some variables
confirm=""
datecheck="$(date '+%Y%m')"
default="y"
count=0

if [ ! "x$1" = "x" ]; then
  case "$1" in
    -c | --confirm | --confir | --confi | --conf | --con | --co | --c)
      confirm="yes"
      if [ ! "x$2" = "x" ]; then
        BackupDir="$2"
      fi
    ;;
    -h | --help)
      echo "Usage: $0 [-h | -c] [backup-dir]"
      echo "  -h | --help    : display this help screen"
      echo "  -c | --confirm : ask for confirmation before deleting directories"
      exit 0
    ;;
    *)
      BackupDir="$1"
    ;;
  esac
fi

if [ ! -d "$BackupDir" ]; then
  echo "Invalid directory: $BackupDir"
  exit 0
fi

cd "$BackupDir"

# Use echo here to take the \n characters out of the ls output
# and replace them with spaces. This probably isn't the best
# way to do it :-/
directories="`echo *`"

for backdir in $directories; do

  # If the length of $backdir is not 8, it does not satisfy
  # the YYYYMMDD form, assume we are in the wrong directory and
  # exit because we don't want to delete something important, etc
  if [ ! "${#backdir}" = "8" ]; then
    echo "$BackupDir: $backdir is an invalid backup directory, exiting"
    exit 1
  fi

  # Check to make sure the file we're about to delete is
  # actually a directory. The backup directory should contain
  # ONLY sub-directories, so if we come across a regular file etc,
  # we are probably in the wrong place.
  if [ ! -d "${backdir}" ]; then
    echo "$BackupDir: $backdir is an invalid backup directory, exiting"
    exit 1
  fi

  # Set tmp to the first 6 letters of $backdir (the first
  # six letters will be the year and month: YYYYMM)
  tmp=`echo "$backdir" | cut -c1-6`

  # If the YYYYMM of the current directory does not match
  # the current year/month ($datecheck) delete the directory
  if [ ! "$tmp" = "$datecheck" ]; then
    if [ "$confirm" = "yes" ]; then
      gotyn=0
      while [ $gotyn -eq 0 ]; do
        echo -n "Remove $backdir? [$default] "
        read Input
        if [ ! "x$Input" = "x" ]; then
          Input="$default"
        fi

        case "$Input" in
          n* | N* | y* | Y*)
            gotyn=1
            ;;
          *)
            echo "Please enter \"yes\" or \"no\""
            ;;
        esac
      done

      case "$Input" in
        n* | N*)
          echo "Skipping $backdir..."
          continue
          ;;
      esac
    fi

    # Delete the old directory
    echo "Removing $backdir..."
    rm -rf $backdir

    count=`expr $count + 1`
  fi
done

echo "Deleted $count directories in $BackupDir"
