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"
dbuser="root"
dbpwd=""
dbport=

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>
# [$3]: <path to movie folder>

{ #set enviroemnt
readonly TRUE=1 #like <stdbool.h>
readonly FALSE=0 #like <stdbool.h>
readonly TTSYNC_EXIST=0
readonly TTSYNC_BACKUP=1
readonly TTSYNC_REMOTE=2
readonly TTSYNC_DEL=3
readonly globalconf="/etc/tt/global"
[ -z "$DEBUG" ] && readonly DEBUG=$FALSE
}
#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"
[ -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
echo "$0 is part of the TerabyteTerminator Project"
echo ""
@ -53,22 +53,17 @@ function help() {

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
## Checks
[ "${mode:0:1}" == "e" ] && touch "$md5list" || {
echo "ERROR: cant creat MD5 File on this PLACE"
exit 1
}
# check if md5 file exist
[ -f "$md5list" ] || {
echo "md5list \"$md5list\" do not exist"
exit 1
}
[ -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
function md5_in_add() {
@ -131,27 +126,7 @@ function md5_out_del() {
exit 0
}

# help functions

# 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
}

#all loaded stat now
case "$mode" in
"a") #in_add
md5_in_add

View File

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

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

#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
echo "$0 is part of the TerabyteTerminator Project"
echo ""
echo "update-file-state [moviedir]"
echo " moviedir: Is optional if it is already in the global config"

echo "update-file-state (moviedir)"
echo "config: $globalconf"
exit 0
}
[ "$mode" == "help" ] && help

#load config (DB,...)
config_load

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

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

#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);"
@ -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)
function file_add() {
local MD5="$2"
@ -206,25 +162,5 @@ function file_add() {

}


# 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"
[ $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)
#all loaded stat now
main