#!/bin/bash # Repodoc Tool written by José Luis Rivero (yoswink@gentoo.org) # Version 0.2_alpha # # Repodoc tries to make Gentoo Documentation Project Developer's # work easier (yeah! we are slackers). # Thanks a lot to Fernando J. Pereda (ferdy@gentoo.org) who # is my bash-ninja-sensei. # # This script was butchered by Marcelo Góes (vanquirius@gentoo.org) # 2005/12/14 # Distributed under the terms of the GNU General Public License v2 VERSION="0.2_alpha" # ------ # CHECK_EXISTS Function. Private # Checks if file to be analyzed exists check_exists () { # Check if OBJECT_TO_ANALYZE exists if [[ ${OBJECT_TO_ANALYZE} = "nada" ]]; then echo "Error: no document to analyze." show_version_usage exit 1 fi if [[ ! -f ${OBJECT_TO_ANALYZE} ]]; then echo "Error: ${OBJECT_TO_ANALYZE} does not exist." show_version_usage exit 1 fi } # ----- # SHOW_REPODOC_VERSION Function. Private # Show version show_repodoc_version () { echo "Repodoc tool written by Jose Luis Rivero " echo "Repodoc $VERSION" } # ----- # SHOW_VERSION_USAGE Function. Private # Show quick help about how to run repodoc show_version_usage () { show_repodoc_version echo echo "Usage: repodoc [options] document.xml" } # ---- # SHOW_REPODOC_HELP Function. Private # Show full help show_repodoc_help () { show_version_usage echo "Options:" echo " --scan, scan Scans the whole directory" echo " --commit, commit Interactively commits files" echo " --help, -h Display this information" echo " --version, -v Show version" echo " --noheader Do not run header module" echo " --nolang Do not run lang module" echo " --nolength Do not run length module" echo " --nopath Do not run path module" echo " --notrans Do not run trans module" echo " --noutf8 Do not run utf8 module" echo " --noxml Do not run xml module" } # ----- # SEARCH DIR_ROOT Function. Private # search_dir_root () { DIR_PROJ_TMP=${DIR##*/proj/*} [[ -z ${DIR_PROJ_TMP} ]] && DIR_ROOT="proj" || DIR_ROOT="doc" } # ------ # SEARCH LANGUAGE Function. Private # search_language () { # First, check if it's under /doc/ or under /proj/ using WWW_DIR [[ ${DIR_ROOT} = "proj" ]] && TMP1=${DIR##*/proj/} || TMP1=${DIR##*/doc/} # Next "/item/" after /proj/ or /doc/ should be the language LANGUAGE=${TMP1%%/*} # Set translation variable [[ ${LANGUAGE} = "en" ]] && TRANSLATION="no" || TRANSLATION="yes" } # ------ # SEARCH ORIGINAL_DOC Function. Private # Search for path of original version only in case of doc is a translation search_original_doc (){ if [[ ${DIR_ROOT} = "doc" ]]; then # Doc is under /doc/ ORIGINAL_DOC=$(sed -e "s:doc/${LANGUAGE}:doc/en:g" <<< ${DOC}) else # Doc is under /proj/ ORIGINAL_DOC=$(sed -e "s:proj/${LANGUAGE}:proj/en:g" <<< ${DOC}) fi } # ----- # SEARCH WWW_DIR Function. Private # search_www_dir () { if [[ ${DIR_ROOT} = "proj" ]]; then WWW_DIR=${DIR/*proj/\/proj} else WWW_DIR=${DIR/*doc/\/doc} fi } # ------ # CREATE ENVIRONMENT Function. Private # Create REPO_DIR and REPO_TMP_DIR if needed and clean log create_environment () { # Create dirs. -p will create REPO_DIR if needed. [[ -d ${REPO_TMP_DIR} ]] || mkdir -p "${REPO_TMP_DIR}" # Remove old log [[ -f ${REPODOC_LOG} ]] && rm ${REPODOC_LOG} } # ------ # SEARCH DOCTYPE Function. Private # search_doctype () { TMP_DOC_TYPE=$(grep -m 1 DOCTYPE ${DOC}) TMP_DOC_TYPE2=${TMP_DOC_TYPE##> ${REPODOC_LOG} ------- Module ${MOD} ------- Result: ${OUTPUT_RESULT} EOF # Checking if output is not empty if [[ -n ${OUTPUT_TEXT} ]]; then # Checking if output is a command if [[ -z ${OUTPUT_TEXT##*::*} ]]; then # Executing the command # need to use eval since otherwise # parameters doesn't work as expected eval ${OUTPUT_TEXT##*::} else # Output is just plain text cat <<- EOF >> ${REPODOC_LOG} Reason: ${OUTPUT_TEXT} EOF fi fi } # ------ # SHOW LOG Function. Private # show_log() { # Set up the log viewer app LOG_VIEWER="less ${REPODOC_LOG}" echo echo -n "Do you want to see the error log? [y/n] " read TMP_OPT [[ ${TMP_OPT} = "y" ]] && ${LOG_VIEWER} } # ----- # HANDLE_MODULE Function. Private # Processes a module handle_module() { # Initializing variables TRANSLATION_MODULE="" OUTPUT_RESULT="" OUTPUT_TEXT="" LEVEL="" # Read the module source "${MOD}" # Checking module level #if [ "${SCRIPT_LEVEL}" = "${LEVEL}" ] #then if [[ ${TRANSLATION_MODULE} != "yes" ]] \ || [[ ${TRANSLATION} = "yes" ]]; then # Checking module's keywords for TMP_DOC_TYPE in ${KEYWORDS}; do # Checking "-KEYWORD" to jump the module if [[ ${TMP_DOC_TYPE} != "-${DOC_TYPE}" ]]; then # Checking keyword if [[ ${TMP_DOC_TYPE} = ${DOC_TYPE} ]] \ || [[ ${TMP_DOC_TYPE} = "ALL" ]]; then echo -n " * Processing module " ${MOD##*\/} " ... " # Call exec_module (function from module) exec_module # Generating output for the log generate_output # Checking if there was any error [[ ${OUTPUT_RESULT} != "ok" ]] && VIEW_LOG="yes" fi else # If found "-keyword", exit the loop break fi done fi #fi end of LEVEL #fi } # ----- # RUN_MODULES Function. Private # Runs all modules that have been set to run run_modules () { if [[ ${HEADER_MOD} = "yes" ]]; then MOD="${MOD_DIR}/header.module" handle_module fi if [[ ${LANG_MOD} = "yes" ]]; then MOD="${MOD_DIR}/lang.module" handle_module fi if [[ ${LENGTH_MOD} = "yes" ]]; then MOD="${MOD_DIR}/length.module" handle_module fi if [[ ${TRANS_MOD} = "yes" ]]; then MOD="${MOD_DIR}/trans.module" handle_module fi if [[ ${UTF8_MOD} = "yes" ]]; then MOD="${MOD_DIR}/utf8.module" handle_module fi if [[ ${XML_MOD} = "yes" ]]; then MOD="${MOD_DIR}/xml.module" handle_module fi } # ----- # DISPLAY_FILE_INFO Function. Private # display_file_info () { echo " ---------- Info ------------------------------" echo "Doc: " ${DOC_NAME} echo "Dir: " ${DIR} echo "Lang: " ${LANGUAGE} echo "Trans: " ${TRANSLATION} echo "Doc-type: " ${DOC_TYPE} #echo "www-dir: " $WWW_DIR #echo "cvs_root: " $CVS_ROOT #echo "original_doc: " $ORIGINAL_DOC #echo "Level: " $SCRIPT_LEVEL echo " ----------------------------------------------" echo } # ----- # DO_ANALYZE Function. Private # do_analyze() { # Knowing Script dir beware of symlink [[ -L ${0} ]] && TMP_SCRIPT_DIR=$(readlink ${0}) || TMP_SCRIPT_DIR=${0} SCRIPT_DIR="${TMP_SCRIPT_DIR%/*}" # Paths MOD_DIR="${SCRIPT_DIR}/modules" EXTRA_DIR="${SCRIPT_DIR}/extras" MOD_COMM="${MOD_DIR}/commons" MOD_TRANS="${MOD_DIR}/trans" MOD_HAND="${MOD_DIR}/handbook" REPO_DIR="${HOME}/.repodoc" REPO_TMP_DIR="${REPO_DIR}/tmp" REPODOC_LOG="${REPO_DIR}/repodoc.log" #SCRIPT_LEVEL="BASIC" # Getting the doc_name and dir taking care of # param as a dir (absolute or relative path) # or as file. DIR_FILTER=${OBJECT_TO_ANALYZE%/*} DOC_NAME=${OBJECT_TO_ANALYZE##*/} if [[ -f ${OBJECT_TO_ANALYZE#*/} ]]; then DIR=${PWD} else # Param is a dir DIR=${OBJECT_TO_ANALYZE%/*} # Using push and pop to get the absolute path pushd ${DIR} > /dev/null DIR=${PWD} popd > /dev/null fi DOC=${DIR}/${DOC_NAME} # Starting environment and variables create_environment search_dir_root search_language search_original_doc search_www_dir search_doctype search_cvs_root display_file_info # Set up VIEW_LOG to "no" until an error appears VIEW_LOG="no" # Run each module we are supposed to run_modules # Show the log if needed [[ ${VIEW_LOG} = "yes" ]] && show_log } # ----- # DO_COMMIT Function. Private # Adds, then commits file to the tree do_commit () { echo "Checking if there is anything to commit..." TMP_CVS="$(cvs diff ${OBJECT_TO_ANALYZE})" if [[ -z ${TMP_CVS} ]]; then echo "Nothing to commit." else echo -n "Do you want to commit this file? [y/n] " read TMP_OPT if [[ ${TMP_OPT} = "y" ]] ; then cvs add "${OBJECT_TO_ANALYZE}" cvs commit "${OBJECT_TO_ANALYZE}" fi fi } # # MAIN CODE # Program starts here # # -- VARIABLES -- NUM_PARAMS="$#" # Initialize variables for modules # yes means it runs, anything else means it doesn't HEADER_MOD="yes" LANG_MOD="yes" LENGTH_MOD="yes" PATH_MOD="yes" TRANS_MOD="yes" UTF8_MOD="yes" XML_MOD="yes" # Processes arguments # Cannot be in its own function for option in "$@" ; do case ${option} in --help|-h) show_repodoc_help exit 0;; --version|-v) show_repodoc_version exit 0;; scan|--scan) SCAN_MODE="yes";; commit|--commit) COMMIT_MODE="yes";; --noheader) HEADER_MOD="no";; --nolang) LANG_MOD="no";; --nolength) LENGTH_MOD="no";; --nopath) PATH_MOD="no";; --notrans) TRANS_MOD="no";; --noutf8) UTF8_MOD="no";; --noxml) XML_MOD="no";; esac done # Pick out xml files in argument list # Cannot be in its own function if [[ ${SCAN_MODE} = "yes" ]]; then echo -e '\E[32;1mRepoDoc scours the neighborhood...'; tput sgr0 OBJECTS="*.xml" else for document in "$@" ; do case "${document##*.}" in xml) OBJECTS="${OBJECTS} ${document}";; esac done fi # Analyze each document if we have any if [[ ${OBJECTS} = "" ]]; then OBJECT_TO_ANALYZE="nada" check_exists else for document in ${OBJECTS}; do OBJECT_TO_ANALYZE="${document}" check_exists do_analyze if [[ ${COMMIT_MODE} = "yes" ]]; then do_commit fi done fi