#!/bin/bash # file: gsl__post_manager # Folder: /var/lib/gsl/scripts # By echolib # License: GNU AFFERO GENERAL PUBLIC LICENSE Version 3, 19 November 2007 #======================================================================= # Check if posts from PWD folder #======================================================================= gsl__check_nbr_posts() { gsl_nbr_posts=`ls -1 *.gsl 2>/dev/null | wc -l` (( $gsl_nbr_posts == 0 )) \ && echo "! No Posts found with .gsl extension" } #======================================================================= # Read from Post list #======================================================================= gsl__loop_posts() { for gsl_post in `ls -1 *.gsl 2>/dev/null` do # Post too small gsl__post_hash_size if (( "$gsl_post_size" <= $gsl_post_min_size ));then gsl__logs_print \ "$gsl_log_e" \ "Post" \ "Size" \ "Too small $gsl_post_size <= $gsl_post_min_size" \ "${PWD}/$gsl_post" continue fi case "$1" in check) if ! [[ "$gsl_force_check" ]];then gsl__post_compare_hash || continue fi gsl__logs_print \ "$gsl_log_i" \ "Reading" \ "Post" \ "$gsl_post" \ "${PWD}" gsl__post_all_checkers # No error: Check/Write to DB (posts.db) gsl_process="Checked" gsl__db_line_post ;; make) gsl_process="Made" gsl__db_post_exists "$gsl_file_db_posts" if [[ "$gsl_db_post_status" == "Made" ]];then gsl__logs_print "$gsl_log_w" \ "Post" \ "Make" \ "$gsl_post already Made (Converted)" \ "$gsl_file_db_posts" continue fi ;; esac done [[ "$gsl_check_done" ]] \ && echo "# All posts already checked" } #======================================================================= # 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"` } #======================================================================= # Check if Post has same Hash #======================================================================= gsl__post_compare_hash() { gsl__db_post_hash 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 } #----------------------------------------------------------------------- # # TOOLS. Getting line, markers, fields in headers... # #----------------------------------------------------------------------- #======================================================================= # 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" \ '{if ($0 ~ marker) print $2}' \ "$2" 2>/dev/null } #======================================================================= # Get fields from variable... $1: field | $2: variable #======================================================================= gsl__get_header_field() { awk -F" : " -v f="$1" \ 'NFS=f {print $NFS}' \ <<< "$2" } #======================================================================= # Get line nbr from CONTENT Post... $1: TERM | $2: File #======================================================================= gsl__get_content_line() { awk -v line="$gsl_post_begin" -v s="$1" \ 'NR >= line && $0 ~ s {print NR}' \ "$2" || return } #======================================================================= # 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 grep -oP$line "(?<=$mk_o)$mk_b?(?=$mk_c)" "$file" && return elif [[ "$ivar" ]];then grep -oP$line "(?<=$mk_o)$mk_b?(?=$mk_c)" <<< "$ivar" && return fi } #======================================================================= # Get Fields and line nbr from header from marker #======================================================================= gsl__get_header_fields() { gsl_post_header_field_1=` gsl__get_header_field 1 "$gsl_header_content_line"` gsl_post_header_field_2=` gsl__get_header_field 2 "$gsl_header_content_line"` gsl_post_header_field_3=` gsl__get_header_field 3 "$gsl_header_content_line"` gsl_post_header_line_nbr=` gsl__get_line "$1$gsl_post_header_field_1" "$gsl_post"` } #======================================================================= # Get Fields and line nbr from header from marker #======================================================================= gsl__check_header_fields() { if ! [[ "$gsl_post_header_field_1" ]] || \ ! [[ "$gsl_post_header_field_2" ]];then gsl__logs_print \ "$gsl_log_e" \ "$1" \ "$2" \ "Line:$gsl_post_header_line_nbr > Content: $3" \ "${PWD}/$gsl_post" gsl_checker_err=true return fi }