Merge branch 'func2lib'

This commit is contained in:
6543 2019-05-12 00:55:06 +02:00
commit 9f1609495f
4 changed files with 145 additions and 141 deletions

View File

@ -4,5 +4,6 @@ dbname="tt_test"
dbhost="localhost" dbhost="localhost"
dbuser="root" dbuser="root"
dbpwd="" dbpwd=""
dbport=


moviedir="/data/share/Filme" moviedir="/data/share/Filme"

92
tools/db/bash-lib Normal file
View File

@ -0,0 +1,92 @@
#!/bin/bash
#Bash-Library for TerabyteTerminator Project
LIBVER=1.1

{ #set enviroemnt
readonly TRUE=1 #like <stdbool.h>
readonly FALSE=0 #like <stdbool.h>
readonly TTSYNC_EXIST=0 #file exist local
readonly TTSYNC_BACKUP=1 #file exist local - backup exist
readonly TTSYNC_REMOTE=2 #file exists remote - not used jet
readonly TTSYNC_DEL=3 #local file deleted
readonly globalconf="/etc/tt/global"
[ -z "$DEBUG" ] && readonly DEBUG=$FALSE

#ERROR Returns
readonly ERROR_CONFIG=2
readonly ERROR_DBLINK=20
}

# file_size <path> -> (du -b)/1024 [Kb]
function file_size() {
if [ -f "$1" ]; then
echo $(($(du -b "$1" | cut -f 1)/1024))
else
echo 0
fi
}

# db_check -> TRUE/FALSE
function db_check() {
db_query "SHOW GRANTS FOR CURRENT_USER();" > /dev/null 2> /dev/null
if [ $? == 0 ]; then
echo $TRUE
else
echo $FALSE
fi
}

# db_query <query> -> result
function db_query() {
local args="-sN" #no headers, no grid
args+=" -h$dbhost"
args+=" -u$dbuser"
[ -n "$dbpwd" ] && args+=" -p$dbpwd"
[ -n "$dbport" ] && args+=" -P$dbport"
args+=" $dbname"
[ $DEBUG == $TRUE ] && echo $@ >&2
echo $@ | mysql $args
}

# config_load()
function config_load() {

#load functions from LIB
if [ -f "$globalconf" ]; then
local config="$globalconf"
elif [ -f "$(readlink -f "$(dirname $0)/../../src/etc/tt/global")" ]; then
local config="$(readlink -f "$(dirname $0)/../../src/etc/tt/global")"
else
echo "ERROR: Global Config not found"
exit 255
fi

#load db settings
readonly dbname=$(grep -v '#' $config | grep ^dbname= | sed "s|^dbname=||g" | tr -d '"')
readonly dbhost=$(grep -v '#' $config | grep ^dbhost= | sed "s|^dbhost=||g" | tr -d '"')
readonly dbuser=$(grep -v '#' $config | grep ^dbuser= | sed "s|^dbuser=||g" | tr -d '"')
readonly dbpwd=$(grep -v '#' $config | grep ^dbpwd= | sed "s|^dbpwd=||g" | tr -d '"')
readonly dbport=$(grep -v '#' $config | grep ^dbport= | sed "s|^dbport=||g" | tr -d '"')
#db checks
[ "$(db_check)" == $FALSE ] && {
echo "DB ERROR!!" >&2
exit $ERROR_DBLINK
}

#set moviedir
{
moviedir="$(echo $moviedir | sed 's|/$||g')" #remove last '/'
[ -z "$moviedir" ] && [ -f "$config" ] && { # if (no argument) and (globalconf exist)
if [ -z "$(grep "moviedir=" "$config" )" ]; then # and (moviedir is NOT in globalconf)
echo 'ERROR: NO Guilty moviedir SET'
exit $ERROR_CONFIG
else # and (moviedir is in globalconf)
readonly moviedir="$(grep "moviedir=" "$config" | cut -d '=' -f 2 | tr -d '"' | sed 's|/$||g')"
fi
}
[ -d "$moviedir" ] || {
echo 'ERROR: NO Guilty moviedir SET'
exit $ERROR_CONFIG
}
}
}

View File

@ -14,30 +14,30 @@
# $2: <paht of md5-file> # $2: <paht of md5-file>
# [$3]: <path to movie folder> # [$3]: <path to movie folder>


{ #set enviroemnt #load functions from LIB
readonly TRUE=1 #like <stdbool.h> if [ -f "$(readlink -f "$(dirname $0)/bash-lib")" ]; then
readonly FALSE=0 #like <stdbool.h> source "$(readlink -f "$(dirname $0)/bash-lib")"
readonly TTSYNC_EXIST=0 [ $DEBUG == $TRUE ] && echo "LIB VERSION: $LIBVER" >&2
readonly TTSYNC_BACKUP=1 elif [ -f "<LIBDIR>/bash-lib" ]; then #<LIBDIR>
readonly TTSYNC_REMOTE=2 source "<LIBDIR>/bash-lib"
readonly TTSYNC_DEL=3 [ $DEBUG == $TRUE ] && echo "LIB VERSION: $LIBVER" >&2
readonly globalconf="/etc/tt/global" else
[ -z "$DEBUG" ] && readonly DEBUG=$FALSE echo "ERROR: Global Lib not found"
} exit 255
fi


{ #get options & args { #get options & args
readonly mode=$(echo $1 | tr -d '-') readonly mode=$(echo $1 | tr -d '-')
readonly md5list="$2" readonly md5list="$2"
moviedir="$3" [ -n "$3" ] && [ -d "$3" ] && moviedir="$3"

#load config (DB,...)
config_load


#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 function
[ "$mode" == "help" ] && {
#help menue #help menue
echo "$0 is part of the TerabyteTerminator Project" echo "$0 is part of the TerabyteTerminator Project"
echo "" echo ""
@ -53,22 +53,17 @@ function help() {


exit 0 exit 0
} }
[ "$mode" == "help" ] && help


##check & prepair args ## Checks
[ "${mode:0:1}" == "e" ] && touch "$md5list" [ "${mode:0:1}" == "e" ] && touch "$md5list" || {
[ -f "$md5list" ] || { echo "md5list \"$md5list\" do not exist"; exit 1; } # check md5 echo "ERROR: cant creat MD5 File on this PLACE"
[ -z "$moviedir" ] && [ -f "$globalconf" ] && { # if (no argument) and (globalconf exist) exit 1
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' # check if md5 file exist
exit 2 [ -f "$md5list" ] || {
else # and (moviedir is in globalconf) echo "md5list \"$md5list\" do not exist"
moviedir="$(grep "moviedir=" "$globalconf" | cut -d '=' -f 2)" exit 1
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="/"


## functions ## functions
function md5_in_add() { function md5_in_add() {
@ -131,27 +126,7 @@ function md5_out_del() {
exit 0 exit 0
} }


# help functions #all loaded stat now

# db_query <query> -> result
function db_query() {
local args="-sN" #no headers, no grid
args+=" -h$dbhost"
args+=" -u$dbuser"
[ -n "$dbpwd" ] && args+=" -p$dbpwd"
args+=" $dbname"
echo $@ | mysql $args
}

# file_size <path> -> (du -b)/1024 [Kb]
function file_size() {
if [ -f "$1" ]; then
echo $(($(du -b "$1" | cut -f 1)/1024))
else
echo 0
fi
}

case "$mode" in case "$mode" in
"a") #in_add "a") #in_add
md5_in_add md5_in_add

View File

@ -6,62 +6,41 @@
## Options/Parameter ## Options/Parameter
# [$1]: <path to movie folder> # [$1]: <path to movie folder>


{ #set enviroemnt #load functions from LIB
readonly TRUE=1 #like <stdbool.h> if [ -f "$(readlink -f "$(dirname $0)/bash-lib")" ]; then
readonly FALSE=0 #like <stdbool.h> source "$(readlink -f "$(dirname $0)/bash-lib")"
readonly TTSYNC_EXIST=0 #file exist local [ $DEBUG == $TRUE ] && echo "LIB VERSION: $LIBVER" >&2
readonly TTSYNC_BACKUP=1 #file exist local - backup exist elif [ -f "<LIBDIR>/bash-lib" ]; then
readonly TTSYNC_REMOTE=2 #file exists remote - not used jet source "<LIBDIR>/bash-lib"
readonly TTSYNC_DEL=3 #local file deleted [ $DEBUG == $TRUE ] && echo "LIB VERSION: $LIBVER" >&2
readonly globalconf="/etc/tt/global" else
[ -z "$DEBUG" ] && readonly DEBUG=$FALSE echo "ERROR: Global Lib not found"
} exit 255
fi


{ #get options & args #get options & args
readonly mode=$(echo $1 | tr -d '-') readonly mode=$(echo $1 | tr -d '-')
moviedir="$1" [ -n "$1" ] && [ -d "$1" ] && moviedir="$1"
[ -z "$moviedir" ] && [ -f "$globalconf" ] && { # if (no argument) and (globalconf exist) readonly DATE=$(date '+%Y-%m-%d')
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="/"
readonly DATE=$(date '+%Y-%m-%d')


#db #help function
dbname=$(grep -v '#' $globalconf | grep ^dbname= | sed "s|^dbname=||g" | tr -d '"') [ "$mode" == "help" ] && {
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 #help menue
echo "$0 is part of the TerabyteTerminator Project" echo "$0 is part of the TerabyteTerminator Project"
echo "" echo ""
echo "update-file-state [moviedir]" echo "update-file-state (moviedir)"
echo " moviedir: Is optional if it is already in the global config" echo "config: $globalconf"

exit 0 exit 0
} }
[ "$mode" == "help" ] && help
#load config (DB,...)
config_load


########################### ###########################
## Programm Haupt Ablauf ## ## Programm Haupt Ablauf ##
########################### ###########################
function main() { function main() {


#db checks
[ "$(db_check)" == $FALSE ] && {
echo "DB ERROR!!" >&2
exit 1
}

#clean DB from old stuff #clean DB from old stuff
{ {
db_query "DELETE From File WHERE (Size=0 OR Size IS NULL OR MD5='' OR MD5 IS NULL OR Name='') AND (TTSync=$TTSYNC_EXIST OR TTSync=$TTSYNC_BACKUP);" db_query "DELETE From File WHERE (Size=0 OR Size IS NULL OR MD5='' OR MD5 IS NULL OR Name='') AND (TTSync=$TTSYNC_EXIST OR TTSync=$TTSYNC_BACKUP);"
@ -130,32 +109,9 @@ function main() {
} }


########################### ###########################
## help functions ######### ## sub functions #########
########################### ###########################


##check & prepair args
[ -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="/"


# file_size <path> -> (du -b)/1024 [Kb]
function file_size() {
if [ -f "$1" ]; then
echo $(($(du -b "$1" | cut -f 1)/1024))
else
echo 0
fi
}

# file_add [rel.path] (md5) # file_add [rel.path] (md5)
function file_add() { function file_add() {
local MD5="$2" local MD5="$2"
@ -206,25 +162,5 @@ function file_add() {


} }



#all loaded stat now
# db_query <query> -> result main
function db_query() {
local args="-sN" #no headers, no grid
args+=" -h$dbhost"
args+=" -u$dbuser"
[ -n "$dbpwd" ] && args+=" -p$dbpwd"
args+=" $dbname"
[ $DEBUG == $TRUE ] && echo $@ >&2
echo $@ | mysql $args
}

function db_check() {
db_query "SHOW GRANTS FOR CURRENT_USER();" > /dev/null 2> /dev/null
if [ $? == 0 ]; then
echo $TRUE
else
echo $FALSE
fi
}

main #aufruf der main function (eig. programmstart)