2019-02-07 14:16:57 +00:00
#!/bin/bash
# @author 6543
2019-05-11 17:34:51 +00:00
# @version 1.0
# @date 2019-05-11
2019-02-07 14:16:57 +00:00
## Options/Parameter
# $1:
2019-02-07 16:02:29 +00:00
# -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
2019-02-07 14:16:57 +00:00
#
# $2: <paht of md5-file>
# [$3]: <path to movie folder>
2019-05-11 21:38:21 +00:00
#load functions from LIB
if [ -f "$(readlink -f "$(dirname $0)/bash-lib")" ]; then
source "$(readlink -f "$(dirname $0)/bash-lib")"
2019-05-11 21:41:58 +00:00
[ $DEBUG == $TRUE ] && echo "LIB VERSION: $LIBVER" >&2
2019-05-11 21:38:21 +00:00
elif [ -f "<LIBDIR>/bash-lib" ]; then #<LIBDIR>
source "<LIBDIR>/bash-lib"
2019-05-11 21:41:58 +00:00
[ $DEBUG == $TRUE ] && echo "LIB VERSION: $LIBVER" >&2
2019-05-11 21:38:21 +00:00
else
echo "ERROR: Global Lib not found"
exit 255
fi
2019-02-07 21:23:19 +00:00
2019-02-07 21:51:17 +00:00
{ #get options & args
2019-02-07 23:15:44 +00:00
readonly mode=$(echo $1 | tr -d '-')
2019-02-08 17:51:30 +00:00
readonly md5list="$2"
2019-05-11 22:31:20 +00:00
[ -n "$3" ] && [ -d "$3" ] && moviedir="$3"
2019-02-07 16:02:29 +00:00
2019-05-11 21:59:47 +00:00
#load config (DB,...)
config_load
2019-02-07 21:51:17 +00:00
}
2019-02-07 16:02:29 +00:00
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"
2019-02-08 17:51:30 +00:00
echo " -d mark files from md5-file ad deleted"
2019-02-07 16:02:29 +00:00
echo " -ee export md5 of existing files"
2019-02-08 17:51:30 +00:00
echo " -ed export md5 of deleted fiels"
2019-02-07 16:02:29 +00:00
echo ""
echo " moviedir: Is optional if it is already in the global config"
2019-02-07 21:14:31 +00:00
2019-02-07 16:02:29 +00:00
exit 0
}
2019-02-08 13:29:50 +00:00
[ "$mode" == "help" ] && help
2019-02-07 16:02:29 +00:00
##check & prepair args
2019-02-08 17:51:30 +00:00
[ "${mode:0:1}" == "e" ] && touch "$md5list"
[ -f "$md5list" ] || { echo "md5list \"$md5list\" do not exist"; exit 1; } # check md5
2019-02-07 16:02:29 +00:00
[ -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'
2019-02-07 21:16:45 +00:00
exit 2
2019-02-07 16:02:29 +00:00
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
2019-02-07 22:40:32 +00:00
moviedir=""$(echo $moviedir | sed 's|/$||g')""
[ -z "$moviedir" ] && exit 255 #catch case moviedir="/"
2019-02-07 16:02:29 +00:00
2019-05-11 21:46:28 +00:00
#db checks
[ "$(db_check)" == $FALSE ] && {
echo "DB ERROR!!" >&2
2019-05-11 22:03:26 +00:00
exit $ERROR_DBLINK
2019-05-11 21:46:28 +00:00
}
2019-02-07 16:02:29 +00:00
## functions
function md5_in_add() {
2019-02-08 17:51:30 +00:00
cat $md5list | while read line; do #for each line in md5
2019-02-07 23:31:42 +00:00
local md5=$(echo $line | cut -c -32)
local path=$(echo $line | cut -c 33- | sed 's/^\ //g')
2019-02-09 03:48:25 +00:00
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
2019-02-07 23:31:42 +00:00
local name=$(echo $relrevpath | cut -d '/' -f 1 | rev)
local folder=$(echo $relrevpath | cut -d '/' -f 2- | rev)
2019-02-08 17:51:30 +00:00
if [ "$(db_query "SELECT COUNT(ID) FROM File WHERE MD5='$md5' AND Folder='$folder' AND Name='$name';")" == "0" ]; then #wenn KEIN gleicher Eintrag Vorhanden
2019-02-09 03:48:25 +00:00
db_query "INSERT INTO File (MD5, Folder, Name, TTSync) VALUES ('$md5', '$folder', '$name', $TTSYNC_EXIST );"
2019-02-08 17:51:30 +00:00
else
db_query "UPDATE File SET TTSync=$TTSYNC_EXIST WHERE MD5='$md5' AND Folder='$folder' AND Name='$name';"
fi
2019-05-11 16:12:12 +00:00
#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';"
2019-02-07 21:49:08 +00:00
done
2019-02-07 21:16:45 +00:00
exit 0
2019-02-07 16:02:29 +00:00
}
function md5_rm() {
2019-02-08 17:51:30 +00:00
cat $md5list | while read line; do #for each line in md5
2019-02-07 23:31:42 +00:00
local md5=$(echo $line | cut -c -32)
local path=$(echo $line | cut -c 33- | sed 's/^\ //g')
2019-02-09 03:48:25 +00:00
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
2019-02-07 23:31:42 +00:00
local name=$(echo $relrevpath | cut -d '/' -f 1 | rev)
local folder=$(echo $relrevpath | cut -d '/' -f 2- | rev)
2019-02-09 03:48:25 +00:00
db_query "DELETE FROM File WHERE MD5='$md5' AND Folder='$folder' AND Name='$name';"
2019-02-07 23:31:42 +00:00
done
2019-02-07 21:16:45 +00:00
exit 0
2019-02-07 16:02:29 +00:00
}
function md5_in_del() {
2019-02-08 17:51:30 +00:00
cat $md5list | while read line; do #for each line in md5
2019-02-07 23:31:42 +00:00
local md5=$(echo $line | cut -c -32)
local path=$(echo $line | cut -c 33- | sed 's/^\ //g')
2019-02-09 03:48:25 +00:00
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
2019-02-07 23:31:42 +00:00
local name=$(echo $relrevpath | cut -d '/' -f 1 | rev)
local folder=$(echo $relrevpath | cut -d '/' -f 2- | rev)
2019-02-07 23:43:40 +00:00
if [ "$(db_query "SELECT COUNT(ID) FROM File WHERE MD5='$md5' AND Folder='$folder' AND Name='$name';")" -gt "0" ]; then #wenn gleicher Eintrag Vorhanden
2019-02-09 03:48:25 +00:00
db_query "UPDATE File SET TTSync=$TTSYNC_DEL WHERE MD5='$md5' AND Folder='$folder' AND Name='$name';"
2019-02-07 23:43:40 +00:00
else #Ansonsten Erstelle Del eintrag
2019-02-09 03:48:25 +00:00
db_query "INSERT INTO File (MD5, Folder, Name, TTSync) VALUES ('$md5', '$folder', '$name', $TTSYNC_DEL );"
2019-02-07 23:43:40 +00:00
fi
2019-02-07 23:31:42 +00:00
done
2019-02-07 21:16:45 +00:00
exit 0
2019-02-07 16:02:29 +00:00
}
function md5_out_exist() {
2019-02-08 17:51:30 +00:00
db_query "SELECT CONCAT(MD5, ' ', ' ' ,'$moviedir/', Folder, '/', Name) FROM File WHERE TTSync=$TTSYNC_EXIST OR TTSync=$TTSYNC_BACKUP;" >> $md5list
2019-02-07 21:16:45 +00:00
exit 0
2019-02-07 16:02:29 +00:00
}
function md5_out_del() {
2019-02-08 17:51:30 +00:00
db_query "SELECT CONCAT(MD5, ' ', ' ' ,'$moviedir/', Folder, '/', Name) FROM File WHERE TTSync=$TTSYNC_DEL;" >> $md5list
2019-02-07 21:16:45 +00:00
exit 0
2019-02-07 16:02:29 +00:00
}
2019-05-11 21:50:13 +00:00
#all loaded stat now
2019-02-07 16:02:29 +00:00
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"
2019-02-07 21:16:45 +00:00
echo ""
help
2019-02-07 16:02:29 +00:00
;;
2019-02-07 21:14:31 +00:00
esac