/usr/sbin/summon

     1	#!/bin/bash
     2	#---------------------------------------------------------------------
     3	##
     4	##=head1 SYNOPSIS
     5	##
     6	## summon is a script for downloading spell source files
     7	##
     8	##=head1 DESCRIPTION
     9	##
    10	## ...
    11	##
    12	##=head1 COPYRIGHT
    13	##
    14	## Original version Copyright 2001 by Kyle Sallee
    15	## Some parts copyright 2002 by Anders Bruun Olsen et al
    16	## Other additions/corrections Copyright 2002 by the Source Mage Team
    17	##
    18	##=head1 FUNCTIONS
    19	##
    20	##=over 4
    21	##
    22	#---------------------------------------------------------------------
    23	
    24	
    25	help">help">help() {
    26	
    27	  cat  <<  EOF
    28	
    29	Summon downloads single or multiple spell source files.
    30	
    31	Example:        summon  nano hdparm sudo
    32	Usage:          summon  [parameters] [spells]
    33	
    34	Optional Parameters:
    35	
    36	--from          <directory>     Specify an alternate for $SOURCE_CACHE
    37	
    38	-d | --download                 Force download of source, regardless of
    39	                                 of whether it exists locally or not.
    40	
    41	-g | --grimoire <grimoire>      Searches only the given grimoire when
    42	                                 searching for spells to summon.
    43	                                 <grimoire> can be absolute or relative to
    44	                                 $CODEX_ROOT
    45	                                 Note that spells or the --all parameter
    46	                                 still needs to be specified.
    47	
    48	-p | --print    <type>          Instead of downloading, print out
    49	                                 information in some form according
    50	                                 to the value of <type> for each spell:
    51	                                  source   - print out the filenames
    52	                                  one_url  - print the first url
    53	                                  all_urls - print out all the urls
    54	                                  raw      - print out all the file
    55	                                  and urls, this format is meant to
    56	                                  be re-read by summon in raw mode.
    57	
    58	-r | --raw                      Read in urls in "raw" form, eg:
    59	                                 file1 url1-1 url1-2 ...
    60	                                 file2 url2-1 url2-2 ...
    61	                                 ...
    62	
    63	--queue                         Download all spells in the install queue.
    64	--all                           Download all spells in the grimoires.
    65	                                (note: this is several gigabytes)
    66	EOF
    67	
    68	  exit  1
    69	
    70	}
    71	
    72	
    73	process_parameters">process_parameters">process_parameters()  {
    74	
    75	if [ ! -n "$1" ]; then
    76	  help">help">help;
    77	fi
    78	
    79	  while  [  -n  "$1"  ];  do
    80	
    81	    if echo "" $1 | grep  -q "^ -"; then
    82	
    83	      case $1 in
    84	         --from) SOURCE_CACHE=$2;         shift 2 ;;
    85	         --help) help">help">help;                    exit  1 ;;
    86	  -d|--download) FORCE_DOWNLOAD=on;        shift 1 ;;
    87	  -g|--grimoire) codex_set_grimoires $2;  shift 2 ;;
    88	     -p|--print) set_print_type $2;       shift 2 ;;
    89	        --queue) QUEUE=1;                 shift 1 ;;
    90	          --all) SUMMON_ALL=1;            shift 1 ;;
    91	       -r|--raw) RAW=1;                   shift 1 ;;
    92	              *) help">help">help;                            ;;
    93	      esac
    94	
    95	    else  shift 1
    96	    fi
    97	
    98	  done
    99	
   100	}
   101	
   102	
   103	strip_parameters">strip_parameters()  {
   104	
   105	  while  [  -n  "$1"  ];  do
   106	
   107	    if  echo  "" $1  |  grep  -q  "^ -";  then
   108	
   109	      case  $1  in
   110	        --from)  shift 2  ;;
   111	        --help)  shift 1  ;;
   112	 -d|--download)  shift 1  ;;
   113	 -g|--grimoire)  shift 2  ;;
   114	    -p|--print)  shift 2  ;;
   115	       --queue)  shift 1  ;;
   116	         --all)  shift 1  ;;
   117	         --raw)  shift 1  ;;
   118	             *)  shift 1  ;;
   119	      esac
   120	
   121	    else  echo  $1
   122	          shift 1
   123	    fi
   124	
   125	  done
   126	
   127	}
   128	
   129	#---------------------------------------------------------------------
   130	## @param A list of spells
   131	## @global PRINT_TYPE
   132	## This is mainly for summon, but might be useful for gaze as its
   133	## an information query function.
   134	#---------------------------------------------------------------------
   135	function summon_print_spells() {
   136	  local SPELLS="$*"
   137	  [ -z "$SPELLS" ] && return 1
   138	
   139	  for  SPELL  in  $SPELLS;  do
   140	    # run in a subshell rather than try to unset_details
   141	    # its a fork either way, and this way leaves us less open for surprises
   142	    ( run_details  &&
   143	      get_spell_files_and_urls|while read -a dl_array; do
   144	        if [ "$PRINT_TYPE" == source ]; then
   145	          echo "${dl_array[0]}"
   146	        elif [ "$PRINT_TYPE" == one_url ]; then
   147	          [ -n "${dl_array[1]}" ] && echo "${dl_array[1]}"
   148	        elif [ "$PRINT_TYPE" == all_urls ]; then
   149	          unset dl_array[0]
   150	          [ -n "${dl_array[*]}" ] && echo "${dl_array[*]}"
   151	        elif [ "$PRINT_TYPE" == raw ]; then
   152	          echo "${dl_array[*]}"
   153	        else
   154	          echo "unknown value of PRINT_TYPE $PRINT_TYPE we shouldn't get here"
   155	          echo "please file a bug if you see this, thanks."
   156	          exit 1
   157	        fi
   158	      done
   159	    )
   160	  done
   161	}
   162	
   163	#---------------------------------------------------------------------
   164	## @param A list of spells
   165	## Will call summon_spell for each spell found in "$*"
   166	##
   167	#---------------------------------------------------------------------
   168	function summon_spells() {
   169	  local SPELLS="$*"
   170	  local SPELL
   171	  [ -z "$SPELLS" ] && return 1
   172	  for  SPELL  in  $SPELLS;  do
   173	    summon_spell $SPELL
   174	  done
   175	}
   176	
   177	
   178	set_print_type() {
   179	  PRINT_TYPE="$1"
   180	  case $PRINT_TYPE in
   181	    source|one_url|all_urls|raw) true ;;
   182	                              *) help">help">help ;;
   183	  esac
   184	}
   185	
   186	main">main">main">main">main() {
   187	
   188	  local spells_to_get
   189	  cd  /tmp
   190	  process_parameters">process_parameters">process_parameters $*
   191	
   192	  if [ -n "$RAW" ] ; then
   193	    local line target rc
   194	    while read line; do
   195	      # eval is needed to preserve quoting
   196	      eval set -a $line
   197	      target="${line%% *}"
   198	      is_downloaded "$target" && continue
   199	      download_src_args "$@" || rc=$?
   200	    done
   201	    return $rc
   202	  fi
   203	
   204	  if  [  -n  "$SUMMON_ALL"  ];  then
   205	    spells_to_get=`codex_get_all_spells|get_basenames`
   206	  elif [  -n  "$QUEUE"  ]; then
   207	
   208	    # Since sometimes we'll be running in raw output mode and possibly
   209	    # redirecting that output somewhere, we dont want to be prompting
   210	    # the "user" about the install queue, but if stdout is a character
   211	    # device (such as a terminal) we may as well try. Im not expecting
   212	    # the user to redirect to a character device too often...but that
   213	    # may be an issue at some point.
   214	    if test -c /dev/stdout ; then
   215	      message -n "${MESSAGE_COLOR}Summoning all spells listed in the queue..."
   216	      message "${DEFAULT_COLOR}"
   217	      list_install_queue
   218	    fi
   219	    spells_to_get=$(< $INSTALL_QUEUE)
   220	  else
   221	    spells_to_get=`strip_parameters">strip_parameters  $*`
   222	  fi
   223	
   224	  if [ -n "$PRINT_TYPE" ]; then
   225	    summon_print_spells $spells_to_get
   226	  else
   227	    summon_spells $spells_to_get
   228	  fi
   229	}
   230	
   231	
   232	. /etc/sorcery/config
   233	if    [  "$UID"  ==  0  ] ; then
   234	  if  [[ $NICE != "0" ]] ; then
   235	    renice $NICE -p $$  >/dev/null
   236	  fi
   237	  mk_tmp_dirs summon
   238	  main">main">main">main">main  "$@"
   239	  rc=$?
   240	  cleanup_tmp_dir $TMP_DIR
   241	  exit $rc
   242	elif  [[  $1 == -h  ]]  ||  [[  $1 == --help  ]] ; then help">help">help
   243	else
   244	  # validate the parameters before su-ing, since we may still drop out
   245	  process_parameters">process_parameters">process_parameters $*
   246	
   247	  echo  "Enter the root password, please."  1>&2
   248	  PARAMS=$(consolidate_params "$@")
   249	  exec su -c "summon $PARAMS" root
   250	fi
   251	
   252	
   253	#---------------------------------------------------------------------
   254	##=back
   255	##
   256	##=head1 LICENSE
   257	##
   258	## This software is free software; you can redistribute it and/or modify
   259	## it under the terms of the GNU General Public License as published by
   260	## the Free Software Foundation; either version 2 of the License, or
   261	## (at your option) any later version.
   262	##
   263	## This software is distributed in the hope that it will be useful,
   264	## but WITHOUT ANY WARRANTY; without even the implied warranty of
   265	## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   266	## GNU General Public License for more details.
   267	##
   268	## You should have received a copy of the GNU General Public License
   269	## along with this software; if not, write to the Free Software
   270	## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   271	##
   272	#---------------------------------------------------------------------