gsl-statique-litterateur/var/lib/gsl/scripts/gsl__post_manager

338 lines
9.3 KiB
Plaintext
Raw Normal View History

#!/bin/bash
# file: gsl__post_manager
# Folder: /var/lib/gsl/scripts
# By echolib
# License: GNU AFFERO GENERAL PUBLIC LICENSE Version 3, 19 November 2007
2022-02-21 17:25:08 +01:00
#-----------------------------------------------------------------------
# -----------------------------------------------------------
# MAIN LOOP. Read from Post list. Do according to Post Status
# -----------------------------------------------------------
#-----------------------------------------------------------------------
gsl__loop_posts() {
2022-02-19 18:17:09 +01:00
gsl__logs_print \
"$gsl_log_i" \
"Found" \
"Posts" \
"$gsl_nbr_posts" \
"${PWD}"
for gsl_post in `ls -1 *.gsl 2>/dev/null`
do
# Check specific asked Post
if [[ "$gsl_this_post" ]];then
! [[ "$gsl_post" == "$gsl_this_post" ]] && continue
fi
2022-02-14 02:57:58 +01:00
# Post too small
gsl__post_hash_size
if (( "$gsl_post_size" <= $gsl_post_min_size ));then
gsl__logs_print \
"$gsl_log_e" \
"Post" \
"Size" \
2022-02-27 19:16:16 +01:00
"$gsl_post - Too small $gsl_post_size <= $gsl_post_min_size" \
2022-02-14 02:57:58 +01:00
"${PWD}/$gsl_post"
continue
fi
2022-02-21 17:25:08 +01:00
# From COMMAND [OPT]
2022-02-14 02:57:58 +01:00
case "$1" in
check)
if ! [[ "$gsl_force_check" ]];then
gsl__post_compare_hash || continue
fi
gsl__logs_print \
2022-02-14 02:57:58 +01:00
"$gsl_log_i" \
"Reading" \
"Post" \
"$gsl_post" \
"${PWD}"
gsl_process="Checked"
2022-02-21 17:25:08 +01:00
gsl__all_checkers
2022-02-14 02:57:58 +01:00
;;
make)
gsl_process="Made"
2022-02-19 18:17:09 +01:00
gsl__db_exists "$gsl_file_db_posts"
2022-02-17 17:21:35 +01:00
case "$gsl_db_post_status" in
Checked)
2022-02-19 18:17:09 +01:00
gsl__post_compare_hash || continue
2022-02-21 17:25:08 +01:00
gsl__all_makers
2022-02-17 17:21:35 +01:00
;;
Made)
gsl__logs_print
"$gsl_log_w" \
"Post" \
"Make" \
"$gsl_post already Converted" \
"$gsl_file_db_posts"
continue
;;
esac
2022-02-14 02:57:58 +01:00
;;
esac
done
2022-02-19 18:17:09 +01:00
[[ "$gsl_checker_war" ]] \
&& gsl log -s -w
[[ "$gsl_checker_err" ]] \
&& gsl log -s -e
[[ "$gsl_check_done" ]] \
&& echo "# Check already done"
}
2022-02-21 17:25:08 +01:00
#-----------------------------------------------------------------------
# --------------------
# Doing some checkings
# --------------------
#-----------------------------------------------------------------------
#=======================================================================
2022-02-21 17:25:08 +01:00
# Check NAME post from [OPT] ; should be from autocompletion
#=======================================================================
2022-02-21 17:25:08 +01:00
gsl__check_opt_post() {
[[ `ls -1 *.gsl | grep "$1"` ]] \
&& gsl_this_post="$1"
}
#=======================================================================
# Check if posts from PWD folder
#=======================================================================
gsl__check_nbr_posts() {
gsl_nbr_posts=`ls -1 *.gsl 2>/dev/null | wc -l`
! (( $gsl_nbr_posts >= 1 )) \
&& echo "! No Posts found with .gsl extension" \
&& exit 1
}
#=======================================================================
# Check if Post has same Hash
#=======================================================================
gsl__post_compare_hash() {
gsl__db_post_hash
2022-02-19 18:17:09 +01:00
case "$gsl_process" in
Checked)
if [[ "$gsl_db_post_hash" == "$gsl_post_hash" ]];then
gsl__logs_print \
"$gsl_log_i" \
"Post" \
"Hash" \
"$gsl_post already Checked from $gsl_post_hash" \
"$gsl_file_db_posts"
gsl_check_done=true
return 1
fi
;;
Made)
if ! [[ "$gsl_db_post_hash" == "$gsl_post_hash" ]];then
gsl__logs_print \
"$gsl_log_e" \
"Post" \
"Hash" \
"$gsl_post has changed. Check it again" \
"$gsl_file_db_posts"
unset gsl_check_done
gsl_checker_err=true
return 1
fi
# Compare external content hash
while read "gsl_content_filename"
do
#unset gsl_db_file_hash gsl_file_hash
! [[ "$gsl_content_filename" ]] && continue
gsl_db_file_hash=`
grep "$gsl_content_filename" "$gsl_file_db_files" \
| awk -F: '{print $2}'`
gsl_file_hash=`
cksum "$gsl_dir_domain_files/$gsl_content_filename" \
| awk '{print $1}'`
if ! [[ "$gsl_db_file_hash" == "$gsl_file_hash" ]];then
gsl__logs_print \
2022-02-21 17:25:08 +01:00
"$gsl_log_w" \
2022-02-19 18:17:09 +01:00
"File" \
"Hash" \
"$gsl_content_filename has changed. $gsl_db_file_hash $gsl_file_hash" \
"$gsl_file_db_files"
2022-02-21 17:25:08 +01:00
gsl_checker_war=true
2022-02-19 18:17:09 +01:00
return 1
fi
done < <(grep "$gsl_post" "$gsl_file_db_posts" \
| awk -F"|" \
'{for(i=7;i<=NF;i++){print $i}}')
;;
esac
}
2022-02-14 18:54:07 +01:00
#-----------------------------------------------------------------------
2022-02-21 17:25:08 +01:00
# --------------
# Tools for Post
# --------------
2022-02-14 18:54:07 +01:00
#-----------------------------------------------------------------------
2022-02-21 17:25:08 +01:00
#=======================================================================
# Get Post Hash
#=======================================================================
gsl__post_hash_size() {
gsl_post_csum=`cksum "$gsl_post"`
gsl_post_hash=`awk '{print $1}' <<< "$gsl_post_csum"`
gsl_post_size=`awk '{print $2}' <<< "$gsl_post_csum"`
}
#=======================================================================
2022-02-14 02:57:58 +01:00
# Get line nbr from Post... $1: TERM | $2: File
#=======================================================================
gsl__get_line() {
awk -v s="$1" \
'match($0,s) {print NR}' \
"$2"
}
#=======================================================================
# Get everything after the marker... $1: marker | $2: File
#=======================================================================
gsl__get_header() {
awk -F"$1" -v marker="$1" -v l="$gsl_post_begin" \
'NR < l && $0 ~ marker {print $2}' \
2022-02-14 02:57:58 +01:00
"$2" 2>/dev/null
}
#=======================================================================
2022-02-14 18:54:07 +01:00
# Get line nbr from CONTENT Post... $1: TERM | $2: File
2022-02-14 02:57:58 +01:00
#=======================================================================
gsl__get_content_line() {
2022-02-27 19:16:16 +01:00
awk -v l="$gsl_post_begin" -v s="$1" \
'NR >= l && $0 ~ s {print NR}' \
2022-02-15 12:42:54 +01:00
"$2" || return
}
2022-02-14 18:54:07 +01:00
2022-02-15 17:56:45 +01:00
#=======================================================================
2022-02-21 17:25:08 +01:00
# Get CONTENT Post with markers... $1: Open Marker | $2: Close Marker
2022-02-15 17:56:45 +01:00
#=======================================================================
gsl__get_content_with_markers() {
gsl__get_content_finder \
-o "$1" \
-b "$gsl_post_header_field_1" \
-c "$2" \
-f "$gsl_post" \
| tail -1 \
|| return
}
2022-02-14 18:54:07 +01:00
#=======================================================================
# Get CONTENT with marks | ! find from ONE line content
#=======================================================================
gsl__get_content_finder() {
# Get [OPTs]
while (( "$#" ))
do
case "$1" in
-o) mk_o="$2" ;; # Open marker
-c) mk_c="$2" ;; # Close Marker
-b) mk_b="$2" ;; # Between markers
-f) file="$2" ;; # In whole file
-v) ivar="$2" ;; # In variable
-n) line="n" ;; # Give found line number
esac
shift
done
! [[ "$file" ]] && ! [[ "$ivar" ]] && return 1
! [[ "$mk_o" ]] && ! [[ "$mk_c" ]] && return 1
! [[ "$mk_b" ]] && mk_b='.*'
if [[ "$file" ]];then
2022-02-15 12:42:54 +01:00
grep -oP$line "(?<=$mk_o)$mk_b?(?=$mk_c)" "$file" && return
2022-02-14 18:54:07 +01:00
elif [[ "$ivar" ]];then
2022-02-15 12:42:54 +01:00
grep -oP$line "(?<=$mk_o)$mk_b?(?=$mk_c)" <<< "$ivar" && return
fi
}
#=======================================================================
2022-02-21 17:25:08 +01:00
# Get NEEDED HEADERS | $1 VAR CONTENT $2 LOG Proc $3 LOG Act
#=======================================================================
gsl__check_needed_headers() {
! [[ "$1" ]] \
&& gsl__logs_print \
"$gsl_log_e" \
"Post" \
"Header $2" \
2022-02-27 19:16:16 +01:00
"Missing: $3" \
"${PWD}/$gsl_post" \
&& gsl_checker_err=true
}
2022-02-27 19:16:16 +01:00
#=======================================================================
# Get fields from variable... $1: field | $2: variable
#=======================================================================
gsl__get_header_field() {
awk -F" : " -v f="$1" \
'NFS=f {print $NFS}' \
<<< "$2"
}
2022-02-15 12:42:54 +01:00
#=======================================================================
2022-02-27 19:16:16 +01:00
# Get HEADER Fields + line nbr $1:marker
2022-02-15 12:42:54 +01:00
#=======================================================================
gsl__get_header_fields() {
2022-02-27 19:16:16 +01:00
gsl_post_hf_1=`gsl__get_header_field 1 "$gsl_header_content_line"`
gsl_post_hf_2=`gsl__get_header_field 2 "$gsl_header_content_line"`
gsl_post_hf_3=`gsl__get_header_field 3 "$gsl_header_content_line"`
gsl_post_hf_l=`gsl__get_line "$1$gsl_post_hf_1" "$gsl_post"`
}
#=======================================================================
# Check NEEDED Fields from HEADER
#=======================================================================
gsl__check_header_fields() {
if ! [[ "$gsl_post_hf_1" ]] || \
! [[ "$gsl_post_hf_2" ]];then
gsl__logs_print \
"$gsl_log_e" \
"$1" \
"$2" \
"Line:$gsl_post_hf_l > $3" \
"${PWD}/$gsl_post"
gsl_checker_err=true
return
fi
}
#=======================================================================
# Check OPTIONAL Field 3 from HEADER
#=======================================================================
gsl__check_header_field3() {
if ! [[ "$gsl_post_header_field_3" ]];then
case "$3" in
"$gsl_marker_image")
gsl__logs_print \
"$gsl_log_e" \
"$1" \
"$2" \
"Line:$gsl_post_hf_l > No ALT TEXT !" \
"${PWD}/$gsl_post"
gsl_checker_err=true
;;
*)
gsl__logs_print \
"$gsl_log_w" \
"$1" \
"$2" \
"Line:$gsl_post_hf_l > No Alt Text ?" \
"${PWD}/$gsl_post"
gsl_checker_war=true
;;
esac
fi
2022-02-15 12:42:54 +01:00
}