#!/bin/sh

################################################################################
# Script name:  zip_cleaner_v0.3.1.sh                                          #
# Author:       SoD-                                                           #
# Contact:      SoD- @ EFNet                                                   #
# Releasedate:  2004-11-07                                                     #
#                                                                              #
# This script uses zip and unzip to remove garbage files and comments from zip #
# files. Just run it with the file to be cleaned as argument. It can also      #
# extract nfo and diz and repair corrupt files.                                #
#                                                                              #
# Installation: -Add any files you want denied in the $REMOVE_LIST variable.   #
#               -Set EXTRACT_NFO_DIZ and EXTRACT_TO_LOWER.                     #
#               -For usage with Dark0n3's zipscript-c:                         #
#                +Add "char *zip_cleaner_arg;" to the variable declarations in #
#                 zipscript-c.c's main().                                      #
#                +Add these lines above the line "d_log("Integrity ok\n");" :  #
#                 if((zip_cleaner_arg = malloc(strlen(argv[2]) + strlen(argv[1]) + 24)) == NULL) {
#                   d_log("zip_cleaner: couldn't allocate memory, aborting\n");#
#                   break;                                                     #
#                 }                                                            #
#                 sprintf(zip_cleaner_arg, "zip_cleaner_v0.3.sh %s/%s\n", argv[2], argv[1]);
#                 if(system(zip_cleaner_arg) != 0)                             #
#                   d_log("zip_cleaner: failed to execute\n");                 #
#                 else                                                         #
#                   d_log("zip_cleaner: executed ok\n");                       #
#                                                                              #
# Requirements: awk, echo, grep, ls, unzip and zip.                            #
#                                                                              #
# Limitations:  -Can't change case of nfo and diz files if they contain spaces.#
#                                                                              #
# Changelog:    v0.1beta -> v0.2:    Made the match for unwanted files case    #
#                                    insensitive. Also added support for       #
#                                    extraction of .nfo and .diz files.        #
#               Changes in settings: Use lower case in $REMOVE_LIST and set    #
#                                    EXTRACT_NFO_DIZ and EXTRACT_TO_LOWER.     #
#               v0.2 -> v0.3beta:    Made the script try to fix corrupt zip's. #
#                                    Fixed a bug in the .nfo/.diz extraction.  #
#                                    Files are no longer chown'ed since it     #
#                                    seems unnescessary.                       #
#               Changes in settings: None.                                     #
#               v0.3beta -> v0.3:    Made the script MUCH MUCH faster when the #
#                                    zip file contains many files and/or       #
#                                    $REMOVE_LIST is large.                    #
#                                    Also Fixed the EXTRACT_TO_LOWER feature.  #
#               Changes in settings: None.                                     #
#               v0.3 -> v0.3.1:      Fixed the EXTRACT_TO_LOWER feature (for   #
#                                    real this time). Also removed some ugly   #
#                                    error messages when run from shell.       #
#               Changes in settings: None.                                     #
#                                                                              #
# NOTE:         I normally wouldn't dream of using site names in a script but  #
#               since those listed in $REMOVE_LIST have been found in loads of #
#               zip files i guess they want the publicity.                     #
################################################################################
# Add any file names you want removed from zip files here. In lower case!
REMOVE_LIST="nf2k2.nfo twh.nfo ph.nfo bar.nfo ti.nfo usux.nfo paranoia.nfo
             echobase.nfo nia.nfo trope.exe stb.nfo ghetto.txt tnl.nfo tnl.exe
             pre.nfo hello.kitty rb.nfo pbox.nfo kane.ans kane.jpg kane.nfo
             asylum.txt ghetto.nfo tqs.nfo topcn.nfo spy.nfo ts.nfo playboys.nfo
             dl.nfo ptp.nfo wizard.nfo bkn.nfo wto.nfo me.nfo tdi.nfo dm.nfo
             kane-xmas.nfo tct.nfo atom.nfo tp.nfo vdrlake.nfo lw.nfo cb.nfo"

# Do you want the script to extract any remaining .nfo and .diz files? YES/NO.
# (.nfo and .diz files in subdirectories isn't processed.)
EXTRACT_NFO_DIZ=YES

# If EXTRACT_NFO_DIZ=YES, do you want the extracted .nfo and .diz to be lower
# case or left as they are? Set to "YES" for lower case and "NO" to leave as is.
EXTRACT_TO_LOWER=YES

################################################################################
# No more settings needed below, just some editing if the script won't work.
################################################################################

if [ ! "$1" ] || [ ! -f "$1" ] || [ ! -w "$1" ]; then
  exit 1
fi

# See if the zip file is corrupt. If it is, try and fix it.
zip -Tq "$1"
if [ $? == 2 ]; then
  echo -n "`basename "$1"` failed integrity test..."
  zip -Fq "$1"
  if [ $? == 0 ]; then
    echo "but got successfully restored"
  else
    echo "and could not be restored"
  fi
fi

FILE_LIST=`unzip -l "$1" | tail -n+4 | awk '{print $4}' | grep -iv "\.rar$\| \
\.r[[:digit:]][[:digit:]]$\|\.zip$\|\.ace\|\.c[[:digit:]][[:digit:]]$\|\.diz$"`

# Find all unwanted files (those in $REMOVE_LIST) in the zip. Skip sub dirs.
REMOVE_FILES=`echo $FILE_LIST | awk -v REMOVE_LIST="$REMOVE_LIST" '{
  items=split(REMOVE_LIST, a)
  for(i=1; i<=items; i++) {
    for(j=1; j<=NF; j++) {
      if(tolower($j) == a[i])
        print $j
    }
  }
}'`
if [ "$REMOVE_FILES" ]; then
  echo "Removing "$REMOVE_FILES" from `basename "$1"`"
  zip -dq "$1" $REMOVE_FILES
fi

echo "Removing any multi-line comment from `basename "$1"`"
zip -qz "$1" < .

if [ $EXTRACT_NFO_DIZ == YES ]; then
  echo "Extracting .nfo and .diz files from `basename "$1"`"
  EXTRACTED_FILES=`unzip -Cn "$1" "*.nfo" "*.diz" -x "*/*"  2> /dev/null | \
  grep -e "\<inflating: \<\|\<extracting: \<" | awk '{print $2}'`
  for FILE in $EXTRACTED_FILES; do
    if [ $EXTRACT_TO_LOWER == YES ]; then
      mv "$FILE" "`echo $FILE | tr '[:upper:]' '[:lower:]'`" > /dev/null 2>&1
    fi
  done
fi

exit