#!/bin/bash # Version: 0.0.36 # file: gsl # Folder: /usr/local/bin # By echolib # License: GNU AFFERO GENERAL PUBLIC LICENSE Version 3, 19 November 2007 #======================================================================= # Tools #======================================================================= if__command() { ! [[ `command -v $1` ]] \ && echo -e "$I_Red Missing $1 command" \ && exit } gsl__if_file() { if ! [[ -f "$1" ]];then echo "! Missing file: $1" exit fi case "$2" in source) source "$1" || exit ;; read) cat "$1" ;; post) gsl_this_post="$1" ;; esac } gsl__invalid_option() { echo "! Invalid argument '$1': $2" exit } # Get conf gsl__if_file "/etc/gsl/gsl.conf" source gsl__if_file "$gsl_dir_scripts/gsl__log_manager" source gsl__if_file "/var/lib/gsl/scripts/gsl__tools" source case "$1" in help|--help|-h) clear case "$2" in "") gsl__if_file "$gsl_dir_helps/gsl_help" read ;; new) gsl__if_file "$gsl_dir_helps/gsl_infos_new_website" read ;; install) gsl__if_file "$gsl_dir_helps/gsl_help_install" read ;; write|-w) gsl__if_file "$gsl_dir_lib/help/article-comment-ecrire.md" read ;; template|-t) gsl__if_file "$gsl_dir_lib/help/template-comment-creer.md" read ;; esac exit ;; new|-N) gsl__if_file "$gsl_dir_scripts/gsl__new_website" source gsl__logs_print \ "$gsl_log_i" \ "Starting" \ "Process" \ "New Website" \ "$gsl_dir_scripts/gsl__new_website" gsl__new_website gsl__logs_print \ "$gsl_log_i" \ "Stopping" \ "Process" \ "New Website" \ "$gsl_dir_scripts/gsl__new_website" [[ -f "$gsl_file_db_domains" ]] && \ [[ -d "$gsl_dir_user_posts" ]] \ && cd "$gsl_dir_user_posts" \ && gsl author add echo gsl log -s -w exit ;; author|-A) gsl__find_domain gsl__if_file "$gsl_dir_scripts/gsl__auth_manager" source case "$2" in "") gsl__authors_list ;; add) gsl__logs_print \ "$gsl_log_i" \ "Starting" \ "Process" \ "author $2 for domain $gsl_find_domain" \ "$gsl_file_auth_ndd" gsl__authors_add gsl__logs_print \ "$gsl_log_i" \ "Stopping" \ "Process" \ "author $2 for domain $gsl_find_domain" \ "$gsl_file_auth_ndd" ;; remove) gsl__logs_print \ "$gsl_log_i" \ "Starting" \ "Process" \ "author $2 for domain $gsl_find_domain" \ "$gsl_file_auth_ndd" gsl__authors_remove gsl__logs_print \ "$gsl_log_i" \ "Stopping" \ "Process" \ "author $2 for domain $gsl_find_domain" \ "$gsl_file_auth_ndd" ;; esac exit ;; readme) clear gsl__if_file "/var/lib/gsl/README.md" read exit ;; log|-L) [[ -z `grep '[^[:space:]]' "$gsl_file_logs" 2>/dev/null` ]] \ && echo "# No logs to show. File is empty." \ && exit while test "$2" do case "$2" in clean|-C) gsl_date_logs=`date +%F-%H-%M-%S` mv "$gsl_file_logs" "$gsl_dir_logs/$gsl_date_logs.gsl.log" rm -f "$gsl_file_logs" printf '%s %s %s\n' \ "# Logs saved to" \ "$gsl_dir_logs/$gsl_date_logs.gsl.log" \ "and cleaned" exit ;; -s) gsl_logs_last_session=true ;; -e) gsl_logs_err=true ;; -i) gsl_logs_inf=true ;; -w) gsl_logs_war=true ;; *) gsl_logs_search="$2" ;; esac shift done gsl__logs_show exit 0 ;; -v) awk 'NR==2 {print "# Installed:",$3}' "/usr/local/bin/$app_name" exit ;; -vv) v_cur=`awk 'NR==2 {print $3}' "/usr/local/bin/$app_name"` echo -n "# $app_name Current: $v_cur | " if__command curl v_onl=`curl -s "$repo" | awk 'NR==2 {print $3}'` echo -ne "Online: $v_onl\n" exit ;; --version) awk 'NR==2 {print "# Installed:",$3}' "/usr/local/bin/$app_name" gsl__if_file "$gsl_dir_lib/new-version.md" read exit ;; esac #======================================================================= # Checking Dependancies #======================================================================= # Check/Create Files and Folders gsl__create_ff gsl__find_domain gsl__if_file "$gsl_dir_scripts/gsl__auth_manager" source gsl__authors_list check || exit gsl__if_file "$gsl_dir_scripts/gsl__post_checkers" source gsl__if_file "$gsl_dir_scripts/gsl__post_manager" source gsl__if_file "$gsl_dir_scripts/gsl__db_manager" source gsl__if_file "$gsl_dir_scripts/gsl__post_makers" source gsl__if_file "$gsl_dir_scripts/gsl__page_creator" source gsl__if_file "$gsl_dir_scripts/gsl__do_commons" source #======================================================================= # Main #======================================================================= case "$1" in create) case "$2" in page|post) true ;; *) gsl__invalid_option "$2" "page|post" ;; esac case "$3" in "") gsl__invalid_option "$3" "Please set a name for your $2" ;; esac gsl_postname="$3" [[ "$gsl_postname" =~ ".gsl" ]] \ && gsl_postname=${gsl_postname/.gsl/} gsl__logs_print \ "$gsl_log_i" \ "Starting" \ "Create" \ "$2" \ "$PWD/$3.gsl" if [[ -f "$gsl_postname.gsl" ]];then gsl__logs_print \ "$gsl_log_w" \ "Create" \ "File" \ "$3.gsl already exists !" \ "$PWD/$3.gsl" gsl_checker_war=true else gsl__create_article "$2" "$(date +%F)" "$gsl_postname" && \ gsl__logs_print \ "$gsl_log_i" \ "Create" \ "$2" \ "You can edit it with 'gsl edit $3.gsl'" \ "$PWD/$3.gsl" fi gsl__logs_print \ "$gsl_log_i" \ "Stopping" \ "Create" \ "$2" \ "$PWD/$3.gsl" ;; edit|-E) ! [[ "$2" ]] \ && gsl__invalid_option "$2" "[File]" case "$2" in conf) echo "> $gsl_this_conf_domain/$gsl_find_domain.conf" gsl_this_post="$gsl_this_conf_domain/$gsl_find_domain.conf" gsl__if_file "$gsl_this_post" ;; *) gsl__if_file "$PWD/$2" post ;; esac if ! [[ "$EDITOR" ]];then if__command nano EDITOR=`which nano` fi gsl__logs_print \ "$gsl_log_i" \ "Starting" \ "Process" \ "edit with $EDITOR" \ "$gsl_this_post" gsl__get_sum "$gsl_this_post" gsl__logs_print \ "$gsl_log_i" \ "Edit" \ "File" \ "Hash: $gsl_post_hash | Size: $gsl_post_size" \ "$gsl_this_post" gsl_post_hash_old=$gsl_post_hash $EDITOR "$gsl_this_post" gsl__get_sum "$gsl_this_post" if (( $gsl_post_hash != $gsl_post_hash_old ));then gsl_log_infos="NEW > Hash. Size: $gsl_post_size" gsl_log_level="$gsl_log_w" else gsl_log_infos="Unchanged" gsl_log_level="$gsl_log_i" fi gsl__logs_print \ "$gsl_log_level" \ "Edit" \ "File" \ "$gsl_log_infos" \ "$gsl_this_post" gsl__logs_print \ "$gsl_log_i" \ "Stopping" \ "Process" \ "edit with $EDITOR" \ "$gsl_this_post" ;; db) ! [[ "$2" ]] \ && gsl__invalid_option "$2" "[File]" gsl__if_file "$2" post [[ -f "$gsl_dir_db/$gsl_this_post.db" ]] \ && cat -n "$gsl_dir_db/$gsl_this_post.db" \ || echo "! No DB yet for $gsl_this_post. Check it first." exit ;; check|-C) case "$2" in -F) gsl_force_check=true [[ "$3" ]] && gsl__if_file "$3" post ;; *) [[ "$2" ]] && gsl__if_file "$2" post ;; esac gsl__logs_print \ "$gsl_log_i" \ "Starting" \ "Process" \ "check Post for domain $gsl_find_domain" \ "$PWD" gsl__loop_posts check gsl__logs_print \ "$gsl_log_i" \ "Stopping" \ "Process" \ "check Post for domain $gsl_find_domain" \ "$PWD" ;; sync|-s) gsl_srv_wip="$gsl_site_server/$gsl_site_ndd/wip" gsl_srv_www="$gsl_site_server/$gsl_site_ndd/www" gsl__logs_print \ "$gsl_log_i" \ "Starting" \ "Process" \ "Sync Templates for domain $gsl_find_domain" \ "$PWD" case "$2" in "") gsl__srv_sync "$gsl_srv_wip" gsl__srv_sync "$gsl_srv_www" ;; www) gsl__srv_sync "$gsl_srv_www" ;; wip) gsl__srv_sync "$gsl_srv_wip" ;; *) gsl__invalid_option "$2" "www|wip" gsl__logs_print \ "$gsl_log_i" \ "Sync" \ "Argument" \ "Invalid $2" \ "$PWD" ;; esac gsl__logs_print \ "$gsl_log_i" \ "Stopping" \ "Process" \ "Sync Templates for domain $gsl_find_domain" \ "$PWD" ;; make|-M) gsl_process="wip" gsl_post_list="Add" case "$2" in -F) gsl_force_make=true [[ "$3" ]] && gsl__if_file "$3" post ;; *) [[ "$2" ]] && gsl__if_file "$2" post ;; esac gsl__logs_print \ "$gsl_log_i" \ "Starting" \ "Process" \ "make Post for domain $gsl_find_domain" \ "$PWD" gsl_srv_wip="$gsl_site_server/$gsl_site_ndd/wip" gsl__loop_posts make gsl__logs_print \ "$gsl_log_i" \ "Stopping" \ "Process" \ "make Post for domain $gsl_find_domain" \ "$PWD" ;; www) ! [[ "$2" ]] \ && gsl__invalid_option "$2" "add|remove" if__command rsync gsl_www_act="$2" case "$2" in add) gsl_ask="! Put online ALL wip posts (yes|N)? " ;; remove) gsl_ask="! REMOVE ALL www posts (yes|N)? " ;; *) gsl__invalid_option "$2" "add|remove" ;; esac ! [[ "$3" ]] \ && gsl__invalid_option "$3" "[FILE]|all" case "$3" in all) read -rp "$gsl_ask" gsl_ask_www case "$gsl_ask_www" in YES|yes|Yes) true ;; *) echo "# Abandon" exit ;; esac ;; *) gsl__if_file "$3" post ;; esac gsl__logs_print \ "$gsl_log_i" \ "Starting" \ "Process" \ "$1 ; $2 from domain $gsl_find_domain" \ "$gsl_site_server" gsl_srv_www="$gsl_site_server/$gsl_site_ndd/www" gsl__loop_posts www "$2" gsl__logs_print \ "$gsl_log_i" \ "Stopping" \ "Process" \ "$1 ; $2 from domain $gsl_find_domain" \ "$gsl_site_server" ;; sidebar|-S) gsl__if_file "$gsl_dir_scripts/gsl__sidebar" source gsl__sidebar $1 $2 $3 $4 ;; stats) echo "! Working on it..." gsl__if_file "$gsl_dir_scripts/gsl__stats" source gsl_file_sti="$gsl_site_server/$gsl_find_domain/gsl-sti.properties" gsl__get_stats exit ;; *) gsl help exit ;; esac #---------------------------------------------------------------------- # Check for process errors or warnings #---------------------------------------------------------------------- gsl_logs_last_session=true gsl__logs_show if [[ "$gsl_checker_war" ]];then echo gsl_logs_war=true gsl__logs_show fi if [[ "$gsl_checker_err" ]];then echo gsl_logs_err=true gsl__logs_show exit 1 fi