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

272 lines
7.8 KiB
Bash

#!/bin/bash
# file: gsl__tools
# Folder: /var/lib/gsl/scripts
# By echolib
# License: GNU AFFERO GENERAL PUBLIC LICENSE Version 3, 19 November 2007
#=======================================================================
# Tool: Source if found
#=======================================================================
gsl__check_source() {
if [[ -f "$1" ]];then
source "$1"
else
echo "! Missing file: $1"
return 1
fi
}
#=======================================================================
# Tool: Check needed file
#=======================================================================
gsl__check_file() {
if ! [[ -f "$1" ]];then
return
fi
}
#=======================================================================
# Check Needed Folders, and create needed files
#=======================================================================
# Folders
gsl__create_ff() {
for gsl_folder in "$gsl_dir_db" \
"$gsl_dir_logs"
do
if ! [[ -d "$gsl_folder" ]];then
echo ": Creating $gsl_folder and give permissions to $USER"
sudo mkdir -p "$gsl_folder" && \
sudo chown -R $USER:$USER "$gsl_folder"
fi
done
# Files
for gsl_file in "$gsl_file_logs" \
"$gsl_file_db_domains"
do
gsl__check_file "$gsl_file" && touch "$gsl_file"
done
}
#=======================================================================
# Check if PWD is configure for DOMAIN
#=======================================================================
gsl__find_domain() {
gsl_nbr_domains=`
find . -maxdepth 1 -not -type d -type f \
-iname "gsl.*.*" 2>/dev/null \
| wc -l`
(( $gsl_nbr_domains > 1 )) \
&& echo "! Too much domains registred here: Set only One per folder" \
&& exit $gsl_nbr_domains
(( $gsl_nbr_domains == 0 )) \
&& echo "! No registred domain here: change or set folder. See help new" \
&& exit 1
gsl_find_domain=`
find . -maxdepth 1 -not -type d -type f \
-iname "gsl.*.*" 2>/dev/null`
gsl_find_domain=`
awk -F"." '{print $(NF-1)"."$NF}' \
<<< $gsl_find_domain`
gsl_find_domain=${gsl_find_domain,,}
gsl__check_domain "$gsl_find_domain"
}
#=======================================================================
# Check and set domain from DB
#=======================================================================
gsl__check_domain() {
gsl_this_conf_domain=`grep "$1" "$gsl_file_db_domains"`
! [[ "$gsl_this_conf_domain" ]] \
&& echo "! Domain Not found: $1" \
&& exit 1
gsl_dir_domain_tpl="$gsl_this_conf_domain/templates"
gsl_dir_domain_files="$gsl_this_conf_domain/files"
gsl_dir_domain_images="$gsl_this_conf_domain/images"
gsl_dir_domain_listings="$gsl_this_conf_domain/X-tra"
for gsl_new_dir in `set -o posix ; set \
| grep "gsl_dir_domain" \
| awk -F= '{print $2}'`
do
if ! [[ -d "$gsl_new_dir" ]];then
mkdir -p "$gsl_new_dir" 2>/dev/null && \
gsl__logs_print \
"$gsl_log_i" \
"Folder" \
"Init..." \
"Created" \
"$gsl_new_dir"
fi
done
}
#-----------------------------------------------------------------------
# --------------
# Tools for Post
# --------------
#-----------------------------------------------------------------------
#=======================================================================
# Get Size & cksum Hash from File... $1 hash/size $2: File
#=======================================================================
gsl__get_sum() {
gsl_post_sum=`cksum "$1"`
gsl_post_hash=`awk '{print $1}' <<< "$gsl_post_sum"`
gsl_post_size=`awk '{print $2}' <<< "$gsl_post_sum"`
}
#=======================================================================
# 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}' \
"$2" 2>/dev/null
}
#=======================================================================
# Get line nbr from CONTENT Post... $1: TERM | $2: File
#=======================================================================
gsl__get_content_line() {
awk -v l="$gsl_post_begin" -v s="$1" \
'NR >= l && $0 ~ s {print NR}' \
"$2" || return
}
#=======================================================================
# Get CONTENT Post with markers... $1: Open Marker | $2: Close Marker
#=======================================================================
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
}
#=======================================================================
# 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 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" \
"Missing: $3" \
"${PWD}/$gsl_post" \
&& gsl_checker_err=true
}
#=======================================================================
# Get fields from variable... $1: field | $2: variable
#=======================================================================
gsl__get_header_field() {
awk -F" : " -v f="$1" \
'NFS=f {print $NFS}' \
<<< "$2"
}
#=======================================================================
# Get HEADER Fields + line nbr $1:marker
#=======================================================================
gsl__get_header_fields() {
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_hf_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
}