/usr/sbin/scribbler

     1	#!/bin/bash
     2	#---------------------------------------------------------------------
     3	##
     4	## @Synopsis This utility lets you manage moving spells to your private
     5	## @Synopsis grimoire so you can mix-and-match versions from various other grimoires.
     6	##
     7	## @Copyright
     8	##
     9	## Copyright 2002 by the Source Mage Team
    10	##
    11	#---------------------------------------------------------------------
    12	
    13	
    14	#---------------------------------------------------------------------
    15	#  Scribbler
    16	#  Usage: scribbler add|remove spell from-grimoire to-grimoire [to-section]
    17	#---------------------------------------------------------------------
    18	
    19	. /etc/sorcery/config
    20	
    21	usage ()
    22	{
    23	cat << EOF
    24	
    25	Scribbler is a utility for controlling the spells in your private grimoire.
    26	
    27	Example: scribbler add bash test stable
    28	Usage:
    29	scribbler add spell from-grimoire to-grimoire [to-section]
    30	        copies a spell from one grimoire to
    31	        another grimoire into the specified section.
    32	        The to-section is the same as the source section if unspecified.
    33	        The to-grimoire is optionally created if it doesn't exist.
    34	
    35	scribbler remove spell from-grimoire
    36	        removes a spell from a grimoire
    37	
    38	scribbler add-grimoire to-grimoire
    39	        creates a local grimoire to-grimoire
    40	EOF
    41	}
    42	
    43	function scribbler_remove() {
    44	  rm -rfv $SPELL_DIRECTORY
    45	  # remove empty sections
    46	  rmdir $(smgl_dirname $SPELL_DIRECTORY)
    47	  scribe reindex $3
    48	}
    49	
    50	function scribbler_add_grimoire() {
    51	  local grimoire=$1
    52	
    53	  TO_GRIMOIRE_DIRECTORY=$(codex_canonicalize_grimoire_name $grimoire) &&
    54	  mkdir -p $TO_GRIMOIRE_DIRECTORY &&
    55	  codex_add_grimoire $TO_GRIMOIRE_DIRECTORY 0 &&
    56	  message "Created grimoire $grimoire" &&
    57	  scribe localize $TO_GRIMOIRE_DIRECTORY
    58	}
    59	
    60	function scribbler_add() {
    61	  ##  Set and validate (and possibly create) the to-grimoire name
    62	  if ! TO_GRIMOIRE_DIRECTORY=$(codex_find_grimoire $4) ; then
    63	    message "${PROBLEM_COLOR}The grimoire you specified" \
    64	            "(${SPELL_COLOR}${4}${PROBLEM_COLOR}) is not a" \
    65	            "valid grimoire.${DEFAULT_COLOR}"
    66	    if query "Shall I add it for you?" y; then
    67	      scribbler_add_grimoire $4
    68	    else
    69	      return $?
    70	    fi || return $?
    71	  fi
    72	
    73	  codex_set_current_spell $SPELL_DIRECTORY || return $?
    74	
    75	  ##  Validate and create the link
    76	  if [[ "${5}" ]] ; then
    77	    TO_SECTION=$5
    78	  else
    79	    TO_SECTION=$SECTION
    80	  fi
    81	
    82	  TO_SPELL_DIRECTORY=${TO_GRIMOIRE_DIRECTORY}/${TO_SECTION}/$2
    83	
    84	  if test -e ${TO_SPELL_DIRECTORY}; then
    85	    message "${PROBLEM_COLOR}The spell you specified" \
    86	            "(${SPELL_COLOR}${2}${PROBLEM_COLOR}) already exists" \
    87	            "in the specified grimoire/section" \
    88	           "(${SPELL_COLOR}${4}/${TO_SECTION}${PROBLEM_COLOR}).${DEFAULT_COLOR}"
    89	    if query "Shall I re-scribble it?" y; then
    90	      rm -rf $TO_SPELL_DIRECTORY
    91	    else
    92	      return 1
    93	    fi
    94	  fi
    95	
    96	
    97	  mkdir -p ${TO_SPELL_DIRECTORY}
    98	
    99	  cp -r $SPELL_DIRECTORY/* $TO_SPELL_DIRECTORY &&
   100	  touch $TO_SPELL_DIRECTORY/SCRIBBLED &&
   101	  mkdir -p $TO_SPELL_DIRECTORY/grimoire
   102	  find $GRIMOIRE/ -maxdepth 1 -type f|files| while read file ; do
   103	    cp $file $TO_SPELL_DIRECTORY/grimoire
   104	  done
   105	  mkdir -p $TO_SPELL_DIRECTORY/section
   106	  find $SECTION_DIRECTORY/ -maxdepth 1 -type f | while read file ; do
   107	    cp $file $TO_SPELL_DIRECTORY/section
   108	  done
   109	
   110	  scribe reindex $4
   111	}
   112	
   113	
   114	function validate_and_parse_args() {
   115	  ##  This script needed some sanity checking...
   116	  if [ "${1}" == "" ]; then
   117	    usage
   118	    exit 1
   119	  fi
   120	
   121	  if [ "${1}" == "add" ] && [ "${4}" == "" ]; then
   122	    usage
   123	    exit 1
   124	  fi
   125	
   126	  if [ "${2}" == "" ]; then
   127	    usage
   128	    exit 1
   129	  fi
   130	
   131	  [[ $1 == add-grimoire ]] && return
   132	
   133	  if [[ -z $3 ]]; then
   134	    usage
   135	    exit 1
   136	  fi
   137	
   138	  if [ "${3}" == "${4}" ] && [ "${5}" == "" ]; then
   139	    message "${PROBLEM_COLOR}The from and to grimoires cannot be" \
   140	            "the same name unless you specify a to-section.${DEFAULT_COLOR}"
   141	    usage
   142	    exit 1
   143	  fi
   144	
   145	
   146	  ##  Set and validate the from-grimoire name
   147	  if ! FROM_GRIMOIRE_DIRECTORY=$(codex_find_grimoire $3); then
   148	     message "${PROBLEM_COLOR}The grimoire you specified" \
   149	             "(${SPELL_COLOR}${3}${PROBLEM_COLOR}) can not be" \
   150	             "found.${DEFAULT_COLOR}"
   151	     exit 1
   152	  fi
   153	
   154	
   155	  ##  Set and validate the spell name
   156	  SPELL_DIRECTORY=$(codex_cache_spell_lookup $2 $FROM_GRIMOIRE_DIRECTORY)
   157	  if [[ -z $SPELL_DIRECTORY ]]; then
   158	    message "${PROBLEM_COLOR}The spell you specified" \
   159	            "(${SPELL_COLOR}${2}${PROBLEM_COLOR}) is not a" \
   160	            "valid spell of the specified grimoire" \
   161	            "(${SPELL_COLOR}${3}${PROBLEM_COLOR}).${DEFAULT_COLOR}"
   162	    exit 1
   163	  fi
   164	}
   165	
   166	function main">main">main">main">main() {
   167	  mk_tmp_dirs scribbler
   168	
   169	  ##  Get to the root of the issue
   170	  case $1 in
   171	             add) scribbler_add "$@" ;;
   172	    add-grimoire) scribbler_add_grimoire "$2" ;;
   173	          remove) scribbler_remove "$@" ;;
   174	               *) usage ;;
   175	  esac
   176	  cleanup_tmp_dir $TMP_DIR
   177	  exit 0
   178	}
   179	
   180	
   181	validate_and_parse_args $@
   182	
   183	if [ "${UID}" != 0 ]; then
   184	  echo "Enter the root password, please:"
   185	  PARAMS=$(consolidate_params "$@")
   186	  exec su -c "scribbler $PARAMS" root
   187	fi
   188	
   189	main">main">main">main">main "$@"
   190	#---------------------------------------------------------------------
   191	##
   192	## @License
   193	## This software is free software; you can redistribute it and/or modify
   194	## it under the terms of the GNU General Public License as published by
   195	## the Free Software Foundation; either version 2 of the License, or
   196	## (at your option) any later version.
   197	##
   198	## This software is distributed in the hope that it will be useful,
   199	## but WITHOUT ANY WARRANTY; without even the implied warranty of
   200	## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   201	## GNU General Public License for more details.
   202	##
   203	## You should have received a copy of the GNU General Public License
   204	## along with this software; if not, write to the Free Software
   205	## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   206	##
   207	#---------------------------------------------------------------------