Check Mp3S Directories For Hash Size And Unneeded Files

A bash script used to create hashes and check them

#!/bin/bash
# requires: cfv
# tested: debian amd64 testing

# this script scan a directory tree with many mp3s, composed by folders called
# like "authorname - diskname" (part " - " is meaning the album root), then, if
# not exists, build an info.txt file, containing md5 hash. If file exists and is
# not correct, it rebuild one, with the quoting of the previous after the new part.


# Then, look for zero length files and to the files with some extensions that need
# to be removed. It could need of many time to complete, and if you have a low power
# machine, you can run it via nice to down priority.


# All the work is logged into the file $resultfile. The first time it runs, if no
# info.txt files were compiled before, you will find a lot of messages about this.
# After, log should be really small or empty. Please try it in a test directory,
# I've used it only on my server, via cron. If you need infos, you can ask me to
# g at antani dot se

function checkdirectory {
curdir="$1"

echo "Checking ""$curdir"
cd "$curdir"
cfv -tsfvmd5 -q -T -f "info.txt"
result=`echo $?`


if [ "$result" -gt 0 ];
    then
        echo "$separator" >> "$resultfile"
        echo "[CFV ""$result""]""$curdir" >> "$resultfile"
        case $result in
    1)
        echo "bad commandline" >> "$resultfile"
        ;;
    2)
        echo "badcrc" >> "$resultfile"
        ;;
    4)
        echo "badsize" >> "$resultfile"
        ;;
    8)
        echo "file not found" >> "$resultfile"
        ;;
    16)
        echo "file read error" >> "$resultfile"
        ;;
    32)
        echo "file unverified" >> "$resultfile"
        ;;
    64)
        echo "checksum file was not found or not recognized" >> "$resultfile"
        echo "trying to rebuild a correct file" >> "$resultfile"
        echo "next check should be ok after reconstruction" >> "$resultfile"
        rebuildinfo "$curdir"
        ;;
    *)
        echo > /dev/null
        ;;
    esac
fi


checkfiles "$curdir"
}

function checkfiles {
foldername="$1"
# check for zero length files
find "$foldername" -size 0 -name "*" -type f | while read filename
do
    echo "$separator" >> "$resultfile"
    echo "[ZERO LENGTH FILE] " "/home/gp/sites/antani.li/build/bash/check-mp3s-directories-for-hash-size-and-unneeded-files.md"  >> "$resultfile"
done;

# check for some kind of files to delete
# for non GNU Find you need find ./ -type f | egrep ".(bmp|jpg|m3u|pdf|zip)$"
find "$foldername" -maxdepth 3 -regextype posix-extended -regex ".*.(bmp|jpg|m3u|pdf|zip)" -type f | while read filename
do
    echo "$separator" >> "$resultfile"
    echo "[DELETING FILE] " "/home/gp/sites/antani.li/build/bash/check-mp3s-directories-for-hash-size-and-unneeded-files.md"  >> "$resultfile"
    echo "next check should be ok after deletion" >> "$resultfile"
    rm "/home/gp/sites/antani.li/build/bash/check-mp3s-directories-for-hash-size-and-unneeded-files.md"
done;
}

function rebuildinfo {
foldername="$1"
infofile="$foldername"/"info.txt"
tmpfile="/tmp/tmp.cfv"
prefile="/tmp/tmp.pre"

echo "Rebuilding info.txt in" "$foldername"

tmp=`date`
if [ -f "$infofile" ]
    then
    mv "$infofile" "$prefile"
fi

# echo ";Creation date: ""$tmp" > "$infofile"
# echo ";-----------------------" >> "$infofile"
cfv -C -rr -tsfvmd5 -f "$tmpfile"
cat "$tmpfile" >> "$infofile"
rm "$tmpfile"


if [ -f "$prefile" ]
    then
    echo ";" >> "$infofile"
    sed 's/^/;/' "$prefile" >> "$infofile"
    rm "$prefile"
fi

checkfiles "$foldername"
echo "Done with " "$foldername"
}

########################################################
# this is the start. change dirs and go.
# uncomment to test
workdir="/home/gp/tmp"                 # test
# workdir="/home/gp/mnt/hd1000/mp3"    # real
separator="--------------------------------------------"
resultfile="/home/gp/batch/tmp/checkmp3.log"
startdir=`pwd`

echo `date` > "$resultfile"
echo "Generated by ""$0" >> "$resultfile"
if [ ! -d "$workdir" ];
    then
    echo "$workdir"" doesn't exist"
exit 1
fi


# looking for directories and successive work
find "$workdir" -maxdepth 5 -type d -name "*" | while read foldername
do
    tmp=`basename "$foldername"`
    if [[ "$tmp" == *" "-" "* ]]
        then
        checkdirectory "$foldername"
    fi
done;

cd "$startdir"