352 lines
8.6 KiB
Bash
352 lines
8.6 KiB
Bash
#!/bin/bash
|
|
# file: gsl__srv_manager
|
|
# Folder: /var/lib/gsl/scripts
|
|
# By echolib (XMPP: im@echolib.re)
|
|
# License: GNU AFFERO GENERAL PUBLIC LICENSE Version 3, 19 November 2007
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
# Manage server options
|
|
#----------------------------------------------------------------------
|
|
#======================================================================
|
|
# www|wip (ARGUMENT)
|
|
# $1: process www/wip
|
|
# $2: sync/add/remove
|
|
# $3: (file)/all
|
|
#======================================================================
|
|
srv__OPTIONS() {
|
|
case "$2" in
|
|
sync) sync__OPTIONS $1 ;;
|
|
add) true ;;
|
|
remove) true ;;
|
|
*) gsl__invalid_option "$2" "add|remove|sync"
|
|
esac
|
|
|
|
case "$3" in
|
|
*".gsl")
|
|
if__file "$3" post
|
|
gsl_post="$3"
|
|
post_only="$3"
|
|
;;
|
|
|
|
all)
|
|
echo "! You're about to $2 all $1 articles"
|
|
read -rp "- Continue (Y|*) ? " www_confirm
|
|
case "$www_confirm" in
|
|
yes|YES|Y) gsl_all="$3" ;;
|
|
*) echo "# Abandon"; exit ;;
|
|
esac
|
|
;;
|
|
|
|
"")
|
|
gsl__invalid_option "$3" "all|(file)"
|
|
;;
|
|
esac
|
|
|
|
# wip add [OPT]: A shortcut of make -F
|
|
case "$1" in
|
|
wip)
|
|
case "$2" in
|
|
add)
|
|
if [[ "$post_only" ]];then
|
|
gsl make -F "$3"
|
|
exit
|
|
|
|
elif [[ "$gsl_all" ]];then
|
|
gsl make -F
|
|
exit
|
|
fi
|
|
;;
|
|
esac
|
|
;;
|
|
esac
|
|
|
|
[[ "$gsl_post" ]] || log_info_all=" ALL"
|
|
|
|
log__process_begin "Server ($1 online)${log_info_all}"
|
|
posts__loop "$1" "$2"
|
|
|
|
[[ "$1" == "www" && "$www_new" ]] \
|
|
&& create__RSS_feed \
|
|
&& stats__datas # Statoolinfos
|
|
|
|
# For logs ; set post or unset if all
|
|
[[ $log_info_all ]] && unset gsl_post
|
|
[[ $post_only ]] && gsl_post="$post_only"
|
|
|
|
log__process_end "Server ($1 online)${log_info_all}"
|
|
}
|
|
|
|
|
|
#======================================================================
|
|
# Set Post/Page file for www+wip from page type and slug
|
|
# Find server post/page file and log
|
|
#======================================================================
|
|
srv__files() {
|
|
gsl_srv_wip="$site_server/$site_ndd/wip"
|
|
gsl_srv_www="$site_server/$site_ndd/www"
|
|
|
|
case "$1" in
|
|
datas)
|
|
unset srv_uri
|
|
case "$article_Type" in
|
|
post)
|
|
gsl_srv_post_wip="$gsl_srv_wip/$article_Slug/index.html"
|
|
gsl_srv_post_www="$gsl_srv_www/$article_Slug/index.html"
|
|
gsl_slug_file="$article_Slug/index.html"
|
|
gsl_slug_dir_wip=${gsl_srv_post_wip%\/*}
|
|
gsl_slug_dir_www=${gsl_srv_post_www%\/*}
|
|
|
|
# Count sub directories
|
|
sub_uri="${article_Slug//[^\/]}"
|
|
srv_uri+=`printf '%0.s../' $(seq 0 ${#sub_uri})`
|
|
;;
|
|
|
|
page)
|
|
gsl_srv_post_wip="$gsl_srv_wip/$article_Slug.html"
|
|
gsl_srv_post_www="$gsl_srv_www/$article_Slug.html"
|
|
gsl_slug_file="$article_Slug.html"
|
|
|
|
srv_uri='./'
|
|
;;
|
|
|
|
*)
|
|
return
|
|
;;
|
|
esac
|
|
|
|
# wip file ?
|
|
if [[ -f "$gsl_srv_post_wip" ]];then
|
|
[[ "$article_Status_wip" == "$article_Status_chk" ]] \
|
|
&& wip_status="(current)" \
|
|
|| wip_status="(old)"
|
|
|
|
gsl_srv_wip_file=true
|
|
|
|
else
|
|
unset gsl_srv_wip_file
|
|
fi
|
|
|
|
# www file ?
|
|
if [[ -f "$gsl_srv_post_www" ]];then
|
|
[[ "$article_Status_www" == "$article_Status_chk" ]] \
|
|
&& www_status="(current)" \
|
|
|| www_status="(old)"
|
|
|
|
gsl_srv_www_file=true
|
|
|
|
else
|
|
unset gsl_srv_www_file
|
|
fi
|
|
;;
|
|
|
|
logs)
|
|
# WIP file
|
|
if [[ $gsl_srv_wip_file ]];then
|
|
gsl__logs_print -i -srv -wip \
|
|
"HTML $article_Type Found $wip_status" \
|
|
"$gsl_srv_post_wip"
|
|
|
|
else
|
|
gsl__logs_print -w -srv -wip \
|
|
"HTML $article_Type not created yet" \
|
|
"$gsl_srv_post_wip"
|
|
fi
|
|
|
|
# WWW file
|
|
if [[ $gsl_srv_www_file ]];then
|
|
gsl__logs_print -i -srv -www \
|
|
"HTML $article_Type Found $www_status" \
|
|
"$gsl_srv_post_www"
|
|
else
|
|
gsl__logs_print -w -srv -www \
|
|
"HTML $article_Type not created yet" \
|
|
"$gsl_srv_post_www"
|
|
fi
|
|
;;
|
|
esac
|
|
}
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
# Manage wip option (Only remove, as add is same as make)
|
|
#----------------------------------------------------------------------
|
|
#======================================================================
|
|
# wip: remove online
|
|
#======================================================================
|
|
WIP__remove() {
|
|
if [[ -f "$gsl_srv_post_wip" ]];then
|
|
rm -f "$gsl_srv_post_wip" && \
|
|
gsl__logs_print -w -srv wip \
|
|
"Article deleted ; no more preview online" \
|
|
"$gsl_srv_post_wip"
|
|
|
|
find "$gsl_srv_wip" -type d -empty -delete && \
|
|
gsl__logs_print -w -srv wip \
|
|
"Cleaned empty directories" \
|
|
"$gsl_srv_wip"
|
|
|
|
DB__set_status "wip-del"
|
|
fi
|
|
return
|
|
}
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
# Manage www options
|
|
#----------------------------------------------------------------------
|
|
#======================================================================
|
|
# www OPTIONS
|
|
# $1: add/remove
|
|
#======================================================================
|
|
WWW__OPTIONS() {
|
|
case "$1" in
|
|
add) WWW__online ;;
|
|
remove) WWW__remove ;;
|
|
esac
|
|
}
|
|
|
|
|
|
#======================================================================
|
|
# www remove online
|
|
#======================================================================
|
|
WWW__remove() {
|
|
if [[ -f "$gsl_srv_post_www" ]];then
|
|
rm -f "$gsl_srv_post_www" && \
|
|
gsl__logs_print -w -srv www \
|
|
"Article deleted ; no more online" \
|
|
"$gsl_srv_post_www"
|
|
|
|
find "$gsl_srv_www" -type d -empty -delete && \
|
|
gsl__logs_print -w -srv www \
|
|
"Cleaned empty directories" \
|
|
"$gsl_srv_www"
|
|
|
|
DB__set_status "www-del"
|
|
www_new=true
|
|
fi
|
|
return
|
|
}
|
|
|
|
|
|
#======================================================================
|
|
# www: Valid online
|
|
#======================================================================
|
|
WWW__online() {
|
|
|
|
# Add wip article to www server
|
|
WWW__add() {
|
|
[[ "$article_Type" == "post" ]] \
|
|
&& mkdir -p "$gsl_slug_dir_www"
|
|
|
|
rsync -a "$gsl_srv_post_wip" "$gsl_srv_post_www" && \
|
|
gsl__logs_print -i -srv www \
|
|
"Article validated online" \
|
|
"$gsl_srv_post_www" && \
|
|
DB__set_status "www"
|
|
www_new=true
|
|
}
|
|
|
|
if ! [[ $article_Status_chk ]];then
|
|
gsl__logs_print -e -srv www \
|
|
"Article not yet checked" \
|
|
"$PWD/$gsl_post"
|
|
return
|
|
|
|
elif (( $article_Status_chk != $article_Status_wip ));then
|
|
gsl__logs_print -e -srv www \
|
|
"Article not yet converted with make" \
|
|
"$gsl_srv_post_wip"
|
|
return
|
|
|
|
else
|
|
if [[ ! -f "$gsl_srv_post_www" ]];then
|
|
WWW__add
|
|
|
|
else
|
|
if [[ "$gsl_all" ]];then
|
|
WWW__add
|
|
|
|
elif (( $article_Status_chk != $article_Status_www ));then
|
|
WWW__add
|
|
|
|
else
|
|
gsl__logs_print -w -srv www \
|
|
"Article already online" \
|
|
"$gsl_srv_post_www"
|
|
fi
|
|
fi
|
|
fi
|
|
}
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
# Manage sync options
|
|
#----------------------------------------------------------------------
|
|
#======================================================================
|
|
# Get arguments from COMMAND (sync)
|
|
# $2: process (wip|www|"")
|
|
#======================================================================
|
|
sync__OPTIONS() {
|
|
gsl_srv_wip="$site_server/$site_ndd/wip"
|
|
gsl_srv_www="$site_server/$site_ndd/www"
|
|
|
|
case "$1" in
|
|
""|www|wip)
|
|
! [[ "$1" ]] \
|
|
&& srv_proc="wip+www" \
|
|
|| srv_proc="$1"
|
|
|
|
log__process_begin "Sync $srv_proc"
|
|
case "$1" in
|
|
"")
|
|
read -rp "! Sync Template for both wip and www (Y|*) ? " sync_ask
|
|
case "$sync_ask" in
|
|
Y)
|
|
sync__srv "$srv_proc" "$gsl_srv_wip"
|
|
sync__srv "$srv_proc" "$gsl_srv_www"
|
|
;;
|
|
*)
|
|
echo "Abandon..."
|
|
exit
|
|
;;
|
|
esac
|
|
;;
|
|
|
|
www)
|
|
sync__srv "$srv_proc" "$gsl_srv_www"
|
|
;;
|
|
|
|
wip)
|
|
sync__srv "$srv_proc" "$gsl_srv_wip"
|
|
;;
|
|
esac
|
|
log__process_end "Sync $srv_proc"
|
|
;;
|
|
|
|
*)
|
|
gsl__invalid_option "$2" "www|wip"
|
|
;;
|
|
esac
|
|
exit
|
|
}
|
|
|
|
|
|
#======================================================================
|
|
# Sync Files from Templates to webserver
|
|
# $1: process (wip|www|wip+www)
|
|
# $2: Folder on srv_wip or srv_www
|
|
#======================================================================
|
|
sync__srv() {
|
|
# Sync Files and Folders
|
|
mkdir -p "$2"
|
|
rsync -a --delete --exclude ".*" "$gsl_dir_domain_tpl" "$2"
|
|
rsync -a --delete --exclude ".*" "$gsl_dir_domain_files" "$2"
|
|
rsync -a --delete --exclude ".*" "$gsl_dir_domain_images" "$2"
|
|
rsync -a --delete --exclude ".*" "$gsl_dir_domain_listings" "$2"
|
|
|
|
gsl__logs_print -i -srv $1 \
|
|
"Sync: $gsl_dir_domain/" \
|
|
"$2/"
|
|
}
|