TerabyteTerminator/tools/db/import-md5-list
2019-05-11 23:50:13 +02:00

164 lines
5.7 KiB
Bash
Executable File

#!/bin/bash
# @author 6543
# @version 1.0
# @date 2019-05-11
## Options/Parameter
# $1:
# -a | add files of md5-file to db
# -r | remove files of md5-file from db
# -d | mark files from md5-file ad deleted
# -ee | export md5 of existing files
# -ed | export md5 of deleted fiels
#
# $2: <paht of md5-file>
# [$3]: <path to movie folder>
#load functions from LIB
if [ -f "$(readlink -f "$(dirname $0)/bash-lib")" ]; then
source "$(readlink -f "$(dirname $0)/bash-lib")"
[ $DEBUG == $TRUE ] && echo "LIB VERSION: $LIBVER" >&2
elif [ -f "<LIBDIR>/bash-lib" ]; then #<LIBDIR>
source "<LIBDIR>/bash-lib"
[ $DEBUG == $TRUE ] && echo "LIB VERSION: $LIBVER" >&2
else
echo "ERROR: Global Lib not found"
exit 255
fi
{ #get options & args
readonly mode=$(echo $1 | tr -d '-')
readonly md5list="$2"
moviedir="$3"
#db
dbname=$(grep -v '#' $globalconf | grep ^dbname= | sed "s|^dbname=||g" | tr -d '"')
dbhost=$(grep -v '#' $globalconf | grep ^dbhost= | sed "s|^dbhost=||g" | tr -d '"')
dbuser=$(grep -v '#' $globalconf | grep ^dbuser= | sed "s|^dbuser=||g" | tr -d '"')
dbpwd=$(grep -v '#' $globalconf | grep ^dbpwd= | sed "s|^dbpwd=||g" | tr -d '"')
}
function help() {
#help menue
echo "$0 is part of the TerabyteTerminator Project"
echo ""
echo "import-md5-list <mode> <md5-file> [moviedir]"
echo " modes:"
echo " -a add files of md5-file to db"
echo " -r remove files of md5-file from db"
echo " -d mark files from md5-file ad deleted"
echo " -ee export md5 of existing files"
echo " -ed export md5 of deleted fiels"
echo ""
echo " moviedir: Is optional if it is already in the global config"
exit 0
}
[ "$mode" == "help" ] && help
##check & prepair args
[ "${mode:0:1}" == "e" ] && touch "$md5list"
[ -f "$md5list" ] || { echo "md5list \"$md5list\" do not exist"; exit 1; } # check md5
[ -z "$moviedir" ] && [ -f "$globalconf" ] && { # if (no argument) and (globalconf exist)
if [ -z "$(grep "moviedir=" "$globalconf" )" ]; then # and (moviedir is NOT in globalconf)
echo 'neither "movie folder" is set in global config nor given as argument'
exit 2
else # and (moviedir is in globalconf)
moviedir="$(grep "moviedir=" "$globalconf" | cut -d '=' -f 2)"
fi
}
[ -d "$moviedir" ] || { echo "moviedir \"$moviedir\" do not exist"; exit 3; } # check moviedir
moviedir=""$(echo $moviedir | sed 's|/$||g')""
[ -z "$moviedir" ] && exit 255 #catch case moviedir="/"
#db checks
[ "$(db_check)" == $FALSE ] && {
echo "DB ERROR!!" >&2
exit 1
}
## functions
function md5_in_add() {
cat $md5list | while read line; do #for each line in md5
local md5=$(echo $line | cut -c -32)
local path=$(echo $line | cut -c 33- | sed 's/^\ //g')
local relrevpath=$(echo $path | sed "s|^$moviedir||g" | sed 's|^/||g' | sed 's|/$||g' | rev | sed "s/'/''/g") #last statement set ' as '' so it wont have an evect in the sql statement
local name=$(echo $relrevpath | cut -d '/' -f 1 | rev)
local folder=$(echo $relrevpath | cut -d '/' -f 2- | rev)
if [ "$(db_query "SELECT COUNT(ID) FROM File WHERE MD5='$md5' AND Folder='$folder' AND Name='$name';")" == "0" ]; then #wenn KEIN gleicher Eintrag Vorhanden
db_query "INSERT INTO File (MD5, Folder, Name, TTSync) VALUES ('$md5', '$folder', '$name', $TTSYNC_EXIST );"
else
db_query "UPDATE File SET TTSync=$TTSYNC_EXIST WHERE MD5='$md5' AND Folder='$folder' AND Name='$name';"
fi
#filesize ..
[ -f "$moviedir/$folder/$name" ] && db_query "UPDATE File SET Size=$(file_size "$moviedir/$folder/$name") WHERE MD5='$md5' AND Folder='$folder' AND Name='$name';"
done
exit 0
}
function md5_rm() {
cat $md5list | while read line; do #for each line in md5
local md5=$(echo $line | cut -c -32)
local path=$(echo $line | cut -c 33- | sed 's/^\ //g')
local relrevpath=$(echo $path | sed "s|^$moviedir||g" | sed 's|^/||g' | sed 's|/$||g' | rev | sed "s/'/''/g") #last statement set ' as '' so it wont have an evect in the sql statement
local name=$(echo $relrevpath | cut -d '/' -f 1 | rev)
local folder=$(echo $relrevpath | cut -d '/' -f 2- | rev)
db_query "DELETE FROM File WHERE MD5='$md5' AND Folder='$folder' AND Name='$name';"
done
exit 0
}
function md5_in_del() {
cat $md5list | while read line; do #for each line in md5
local md5=$(echo $line | cut -c -32)
local path=$(echo $line | cut -c 33- | sed 's/^\ //g')
local relrevpath=$(echo $path | sed "s|^$moviedir||g" | sed 's|^/||g' | sed 's|/$||g' | rev | sed "s/'/''/g") #last statement set ' as '' so it wont have an evect in the sql statement
local name=$(echo $relrevpath | cut -d '/' -f 1 | rev)
local folder=$(echo $relrevpath | cut -d '/' -f 2- | rev)
if [ "$(db_query "SELECT COUNT(ID) FROM File WHERE MD5='$md5' AND Folder='$folder' AND Name='$name';")" -gt "0" ]; then #wenn gleicher Eintrag Vorhanden
db_query "UPDATE File SET TTSync=$TTSYNC_DEL WHERE MD5='$md5' AND Folder='$folder' AND Name='$name';"
else #Ansonsten Erstelle Del eintrag
db_query "INSERT INTO File (MD5, Folder, Name, TTSync) VALUES ('$md5', '$folder', '$name', $TTSYNC_DEL );"
fi
done
exit 0
}
function md5_out_exist() {
db_query "SELECT CONCAT(MD5, ' ', ' ' ,'$moviedir/', Folder, '/', Name) FROM File WHERE TTSync=$TTSYNC_EXIST OR TTSync=$TTSYNC_BACKUP;" >> $md5list
exit 0
}
function md5_out_del() {
db_query "SELECT CONCAT(MD5, ' ', ' ' ,'$moviedir/', Folder, '/', Name) FROM File WHERE TTSync=$TTSYNC_DEL;" >> $md5list
exit 0
}
#all loaded stat now
case "$mode" in
"a") #in_add
md5_in_add
;;
"r") #rm
md5_rm
;;
"d")
md5_in_del
;;
"ee" | "e" )
md5_out_exist
;;
"ed" )
md5_out_exist
;;
*)
echo "wrong arguments"
echo ""
help
;;
esac