source: transgenic/trunk/fuentes/install/usr/bin/tg @ 389

Last change on this file since 389 was 389, checked in by mabarracus, 3 years ago

Copy trusty code

  • Property svn:executable set to *
File size: 26.7 KB
Line 
1#!/bin/bash
2# -------
3# File:        tg (transgenic tool)
4# Description: tool to manage (genetically) enhanced seeds
5# Author:      (c) 2009 Luis Garcia Gisbert <garcia_luigis@gva.es>
6#
7# This program is free software; you can redistribute it and/or modify
8# it under the terms of the GNU General Public License as published by
9# the Free Software Foundation; either version 2 of the License, or
10# (at your option) any later version.
11#
12# This program is distributed in the hope that it will be useful,
13# but WITHOUT ANY WARRANTY; without even the implied warranty of
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15# GNU General Public License for more details.
16#
17# You should have received a copy of the GNU General Public License along
18# with this program; if not, write to the Free Software Foundation, Inc.,
19# 51 Franklin St, Fifth Floor, Boston MA 02110-1301 USA
20# --------
21
22set -e
23
24PATH="/usr/sbin:/usr/bin:/sbin:/bin"
25
26# variables
27ACTION=""
28CONF_FILE="transgenic.conf"
29TG_INCLUDE_DIRS="include inc"
30TG_FINAL_SEEDS="final_seeds"
31#TG_OUTPUT_SEEDS_LIST="output_seeds.list"
32TG_SEED_DIRS="seeds"
33TG_MAINSEED="main"
34TG_INCLSEED="incl"
35TG_STRUCTURE="STRUCTURE"
36TG_EXCLUDE_SEEDS_LIST="^${TG_STRUCTURE}$ ^${TG_STRUCTURE}\..*$"
37TG_SEED_DEFAULT_DIR=""
38TG_BASE_DIR=""
39SHORT_OUTPUT="y"
40ASSUME_YES=""
41TG_EDITOR=""
42TG_PAGER=""
43TG_COWSAY="/usr/games/cowsay -f eyes"
44TG_DEFAULT_SECTION="Other"
45TG_VERSION_FILE="/usr/share/transgenic/version"
46declare -a SEEDS_NAME SEEDS_PATH SEEDS_DIRS SEEDS_TYPE
47XARG_REGEXP="^\([-[:alnum:]_]/\)?"
48SIMPLE_NAME_REGEXP="^[-[:alnum:]_\.+]\+$"
49RECOMMENDS_REGEXP="^([-[:alnum:]_\.]\+)$"
50NAME_END_REGEXP="\([^-[:alnum:]_\.+]\|$\)"
51GPP_CMD="gpp --nostdinc"
52
53PKG_LISTS_DIR="/var/lib/apt/lists/"
54#parameters management (Dir,Seed,Section,Pkgs)
55DARG=""
56SARG=""
57SECARG=""
58declare -a PARG
59
60# to store non-opt arguments
61declare -a ARGV
62# Bash arrays are 0-based, but we like ARGC to be equal to the number of non-option arguments.
63# To solve this issue, we use a "C-like" ARGV vector by setting ARGV[0] with the name of script
64# ('basename' of script for arbitrary cosmetic reasons), so real arguments begin with index 1
65# the array will be ARGC+1 elements
66ARGC=0
67ARGV[0]="$(basename "$0")"
68
69#functions
70
71get_confirmation(){
72   a="$ASSUME_YES"
73   while ! echo " y n " |grep -q " $a " ; do
74      $TG_COWSAY "$1 (y/n)"
75      read a
76   done
77   [ "$a" = "y" ] || return 1
78   return 0
79}
80
81do_conf_update(){
82   # read configuration file
83   if [ "$CONF_FILE" -a -r "$CONF_FILE" ] ; then
84      . $CONF_FILE
85   fi
86   # some safe defaults
87   [ "$TG_SEED_DIRS" ] || TG_SEED_DIRS="./"
88   [ "$TG_SEED_DEFAULT_DIR" ] || TG_SEED_DEFAULT_DIR="${TG_SEED_DIRS%% *}"
89   [ "$TG_BASE_DIR" ] || TG_BASE_DIR="$(dirname "$TG_SEED_DEFAULT_DIR")"
90   [ "$TG_EDITOR" ] || TG_EDITOR="editor"
91   [ "$TG_PAGER" ] || TG_PAGER="pager"
92   # prepare GPP command
93   GPP_CMD="gpp --nostdinc"
94   # we can include files from seed an include dirs ...
95   for d in $TG_SEED_DIRS $TG_INCLUDE_DIRS ; do
96      GPP_CMD="$GPP_CMD -I${d}"
97   done
98   add_seeds_from_dirs
99   return 0
100}
101
102find_seed_realpath(){
103   NUM_FILES=0
104   FULL_PATH=""
105   for d in $TG_SEED_DIRS $TG_INCLUDE_DIRS ; do
106      FULL_PATH="$d/$1"
107      if [ -r "$FULL_PATH" ] ; then
108         echo "$FULL_PATH"
109         NUM_FILES=$(($NUM_FILES + 1))
110      fi
111   done
112   [ $NUM_FILES -eq 1 ] || return 1
113   return 0
114}
115
116do_find_seeds(){
117   [ -d "$1" ] || return 0
118   find "$1" -xtype f -printf "%P\n" |sed -e "/\/\{0,1\}\./d"
119}
120
121show_quick_list(){
122   for d in $TG_SEED_DIRS $TG_INCLUDE_DIRS ; do
123      do_find_seeds "$d"
124   done
125   return 0
126}
127
128add_seeds_from_dirs(){
129   i=0
130   SEEDS_NAME=()
131   SEEDS_PATH=()
132   SEEDS_TYPE=()
133   for d in $TG_SEED_DIRS $TG_INCLUDE_DIRS ; do
134      #exclude hidden dirs and files
135      for f in $(do_find_seeds "$d") ; do
136         rc=0
137         f_path="$(find_seed_realpath "$f")" || rc=$?
138         [ $rc -eq 0 ] || die "Duplicated seed file: $f\n$f_path"
139         # fill the arrays
140         SEEDS_NAME[$i]="$f"
141         SEEDS_PATH[$i]="$f_path"
142         if echo " $TG_SEED_DIRS " |grep -q " $(dirname "$f_path") " ; then
143            SEEDS_TYPE[$i]="$TG_MAINSEED"
144         else
145            SEEDS_TYPE[$i]="$TG_INCLSEED"
146         fi
147         i=$(($i + 1))
148      done
149   done
150   return 0
151}
152
153get_seed_index(){
154   i=0
155   ret=""
156   while [ $i -lt ${#SEEDS_NAME[@]} ] ; do
157      if [ "${SEEDS_NAME[$i]}" = "$1" ] ; then
158         ret=$i
159         break
160      fi
161      i=$(($i + 1))
162   done
163   [ "$ret" ] || return 1
164   echo $ret
165   return 0
166}
167
168get_seed_file(){
169   f_path=""
170   i=$(get_seed_index "$1") || return 1
171   echo "${SEEDS_PATH[$i]}"
172   return 0
173}
174
175is_regular_seed(){
176   rc=0
177   for r in $TG_EXCLUDE_SEEDS_LIST ; do
178      if echo "$1" |grep -q "$r" ; then
179         # the seed matches an excluded regexp/value
180         rc=1
181         break
182      fi
183   done
184   return $rc
185}
186
187list_seeds(){
188   i=0
189   while [ $i -lt ${#SEEDS_NAME[@]} ] ; do
190      s="${SEEDS_NAME[$i]}"
191      if is_regular_seed "$s" ; then
192         if [ "$SHORT_OUTPUT" ] ; then
193            echo "$s"
194         else
195            echo "TGSEED:$s FILE:${SEEDS_PATH[$i]}"
196         fi
197      fi
198      i=$(($i + 1))
199   done
200   return 0
201}
202
203seed_name_error(){
204   die "Error, unknown seed name \"$1\""
205}
206
207expand_seed(){
208   SEED_FILE="$(get_seed_file "$1")" || seed_name_error "$1"
209   rc=0
210   $GPP_CMD "$SEED_FILE" || rc=$?
211   [ $rc -eq 0 ] || die "Error expanding file \"$SEED_FILE\""
212   return 0
213}
214
215say_line(){
216   STR="=="
217   n=1
218   while [ $n -le $1 ] ; do
219      STR="$STR="
220      n=$(($n + 1))
221   done
222   SEED_FILE=" FILE: $(get_seed_file "$2")" || SEED_FILE=""
223   if [ "$SHORT_OUTPUT" ] ; then
224      echo -n "$STR$STR" |tr "=" " "
225      echo "$2"
226   else
227      echo "# $STR TGSEED: $2$SEED_FILE $STR"
228   fi
229   return 0
230}
231
232show_depends(){
233   TMP_FILE="$(tempfile)"
234   SEED_FILE="$(get_seed_file "$1")" || seed_name_error "$1"
235   $GPP_CMD --includemarker "===LINE:%===FILE: % ===MODE: %" "$SEED_FILE" |sed -ne "/===LINE:.*===MODE: .$/{s%^.*===FILE:[[:blank:]]*%%;s%[[:blank:]]===MODE:[[:blank:]]% %;p}" > "$TMP_FILE"
236   INC_LEVEL=0
237   say_line $INC_LEVEL "$1"
238   cat "$TMP_FILE" | while read f m; do
239      case $m in
240         1)
241            INC_LEVEL=$(($INC_LEVEL + 1))
242            say_line $INC_LEVEL $f
243            ;;
244         2)
245            INC_LEVEL=$(($INC_LEVEL - 1))
246            ;;
247         *)
248            say_line $INC_LEVEL $f
249            ;;
250      esac
251   done
252   rm -f "$TMP_FILE"
253   return 0
254}
255
256clear_final_seeds(){
257   if [ -d "$TG_FINAL_SEEDS" ] ; then
258      find "$TG_FINAL_SEEDS" -maxdepth 1 -xtype f -exec rm {} \;
259   fi
260   return 0
261}
262
263write_final_seed(){
264   TG="$1"
265   n=$(get_seed_index "$TG") || die "Invalid seed \"$TG\" in output seeds list"
266   if [ "${SEEDS_TYPE[$n]}" = "$TG_MAINSEED" ] ; then
267      printf "."
268      if [ "$2" ] ; then
269         OUT_FILE="$2"
270      else
271         OUT_FILE="$TG_FINAL_SEEDS/${SEEDS_NAME[$n]}"
272      fi
273      mkdir -p "$(dirname ${OUT_FILE})"
274      expand_seed "${SEEDS_NAME[$n]}" > "$OUT_FILE"
275   fi
276   return 0
277}
278
279do_final_structure_file(){
280   TMP_FILE="$1"
281   TMP2_FILE="$(tempfile)"
282   # get "non-seed" options
283   sed -e "/^.*:/d" "$TMP_FILE" > "$TMP2_FILE"
284   # add missing STRUCTURE seeds
285   SHORT_OUTPUT="y"
286   for s in $(list_seeds) ; do
287      if ! grep -q "^${s}:" "$TMP_FILE" ; then
288         echo "${s}:" >> "$TMP2_FILE"
289      fi
290   done
291   # add the seed options
292   sed -ne "/^.*:/p" "$TMP_FILE" >> "$TMP2_FILE"
293   OUT_FILE="$TG_FINAL_SEEDS/$TG_STRUCTURE"
294   mkdir -p "$(dirname ${OUT_FILE})"
295   cat "$TMP2_FILE" > "${OUT_FILE}"
296   rm -f  "$TMP2_FILE"
297   return 0
298}
299
300open3(){
301   # asociate file descriptor 3 with TG_OUTPUT_SEEDS_LIST
302   exec 3< "$TG_OUTPUT_SEEDS_LIST"
303   return 0
304}
305
306close3(){
307   exec 3>&-
308   return 0
309}
310
311show_pkg_regexp(){
312   echo "^[[:blank:]]\*[[:blank:]]\+(\?${1}${NAME_END_REGEXP}"
313   return 0
314}
315
316show_pkg_line(){
317   echo " * $1"
318   return 0
319}
320
321get_pkg_seed_files(){
322   rc=0
323   rgrep --exclude-dir=".svn"  "$(show_pkg_regexp "${1}")" $TG_SEED_DIRS $TG_INCLUDE_DIRS |sed -e "s%^.*/%%;s% \* %%" || rc=$?
324   return $rc
325}
326
327test_pkg_in_file(){
328   grep -q "$(show_pkg_regexp "${1}")" "$2" || return 1
329   return 0
330}
331
332show_pkgs(){
333   if seedfile="$(test_seed_file)" ; then
334      if [ "$SHORT_OUTPUT" ] ; then
335         echo "${SARG}:"
336         sed -ne "/$(show_pkg_regexp '.*')/{s%^ \* %  %;p}" "$seedfile"
337      else
338         echo -n "${SARG}:"
339         sed -ne "/$(show_pkg_regexp '.*')/{s%^ \* %'%;s%$%'%;p}" "$seedfile" |tr "\n" "," |sed -e "s%,$%%"
340         echo ""
341      fi
342   fi
343   return 0
344}
345
346test_pkg_name(){
347   psearch="${1%)}"
348   psearch="${psearch#(}"
349   ! grep -q "^Package: $psearch$" $PKG_LISTS_FILES || return 0
350   show_warning "Unable to locate package \"$psearch\". Use at your own risk"
351   return 0
352}
353
354do_clear_arg(){
355   DARG=""
356   SARG=""
357   PARG=()
358   return 0
359}
360
361do_split_parg(){
362   do_split_arg "$1" "parg"
363   return 0
364}
365
366do_split_sarg(){
367   do_split_arg "$1"
368   return 0
369}
370
371do_split_darg(){
372   do_clear_arg
373   DARG="$1"
374   test_arg
375}
376
377is_simple_name(){
378   echo "$1" |grep -q "$SIMPLE_NAME_REGEXP" || return 1
379   return 0
380}
381
382is_recommends(){
383   echo "$1" |grep -q "$RECOMMENDS_REGEXP" || return 1
384   return 0
385}
386
387test_arg(){
388   if [ "$DARG" ] ; then
389      # trim any "/" or space at begin or end
390      DARG="$(echo "$DARG" |sed -e "s%^[[:blank:]]*/\?[[:blank:]]*%%;s%[[:blank:]]*/\?[[:blank:]]*$%%")"
391      is_simple_name "$DARG" || die "Invalid seed directory name: \"$DARG\""
392   fi
393   if [ "$SARG" ] ; then
394      is_simple_name "$SARG" || die "Invalid seed name: \"$DARG\""
395   fi
396   if [ ${#PARG[@]} -gt 0 ] ; then
397      i=0
398      while [ $i -lt ${#PARG[@]} ] ; do
399         is_simple_name "${PARG[$i]}" || is_recommends "${PARG[$i]}" || die "Invalid pkg name: \"${PARG[$i]}\""
400         test_pkg_name "${PARG[$i]}"
401         i=$(($i + 1))
402      done
403   fi
404   return 0
405}
406
407do_split_arg(){
408   BULK_ARG="$1"
409   do_clear_arg
410   # set value for DARG ONLY if there is a "/" in the seed name
411   if echo "$BULK_ARG" |grep -q "/" ; then
412      DARG="${BULK_ARG%%/*}"
413      BULK_ARG="${BULK_ARG#*/}"
414   fi
415   # set value for SARG if there is a ":"
416   if echo "$BULK_ARG" |grep -q ":" ; then
417      SARG="${BULK_ARG%%:*}"
418      BULK_ARG="${BULK_ARG#*:}"
419   elif [ -z "$2" ] && ! echo "$BULK_ARG" |grep -q "," ; then
420      # there is not a ":", the argument is not a comma separated list
421      # and we are called in "default" (seed) mode , not in "pkg" mode
422      # so the argument is trated as a seed
423      SARG="$BULK_ARG"
424      BULK_ARG=""
425   fi
426   # extract SECARG if SARG contains a % simbol
427   if echo "$SARG" |grep -q "%" ; then
428      SECARG="${SARG#*%}"
429      SARG="${SARG%%%*}"
430   else
431      SECARG="$TG_DEFAULT_SECTION"
432   fi
433
434   # ok, if BULK_ARG still contans data ... it will be considered as al list of pkgs
435   # and expanded as PARG array (commas are translated to spaces to get this)
436   PARG=( $(echo "${BULK_ARG}" |tr "," " ") )
437   test_arg
438   return 0
439}
440
441is_empty_seed_dir(){
442   test_seed_dir
443   [ -z "$(do_find_seeds "${TG_BASE_DIR}/${DARG}")" ] || return 1
444   return 0
445}
446
447test_seed_dir(){
448   tdir="${TG_BASE_DIR}/${DARG}"
449   [ -d "$tdir" ] || die "Invalid seed directory \"$tdir\""
450   return 0
451}
452
453test_seed_file(){
454   local rc
455   rc=0
456   # first, search for the seed name
457   sfile="$(get_seed_file "${SARG}")" || rc=1
458   if [ $rc -eq 1 -a "$DARG" ] ; then
459      sfile="${TG_BASE_DIR}/${DARG}/${SARG}"
460      rc=0
461   fi
462   if [ $rc -eq 0 -a -e "$sfile" ] ; then
463      echo "$sfile"
464      return 0
465   fi
466   return 1
467}
468
469show_section_regexp(){
470   SEC_NAME="$1"
471   [ "$SEC_NAME" ] || SEC_NAME=".*"
472   echo "^=\+[[:blank:]]$SEC_NAME[[:blank:]]="
473}
474
475show_section_header(){
476   echo "= $1 ="
477}
478
479do_add_pkg_to_file(){
480#   sed -i -e "\$a$(show_pkg_line "$1")" "$2"
481   if [ "$SECARG" ] ; then
482      # test if section exists
483      if [ -f "$2" ] && grep -q "$(show_section_regexp "$SECARG")" "$2" ; then
484         # ok, section exists in the seed file, so we need to insert the package in that section
485         TMP_FILE="$(tempfile)"
486         # show previous part of file:
487         sed -ne "1,/$(show_section_regexp "$SECARG")/{/$(show_section_regexp "$SECARG")/d;p}" "$2" > "$TMP_FILE"
488         # show section:
489         sed -ne "/$(show_section_regexp "$SECARG")/,/$(show_section_regexp)/{/$(show_section_regexp "$SECARG")/p;/$(show_section_regexp)/d;p}" "$2" >> "$TMP_FILE"
490         # print package
491         echo "$(show_pkg_line "$1")" >> "$TMP_FILE"
492         # show next section header (if any): sed -ne "/$(show_section_regexp "$SECARG")/,/$(show_section_regexp)/{/$(show_section_regexp "$SECARG")/d;/$(show_section_regexp)/p}" /tmp/kk
493   # show rest of file: sed -ne "/$(show_section_regexp "$SECARG")/,\${/$(show_section_regexp "$SECARG")/,/$(show_section_regexp)/d;p}"
494         # alternative, show rest of file including next header:
495         sed -ne "/$(show_section_regexp "$SECARG")/,\${/$(show_section_regexp "$SECARG")/,/$(show_section_regexp)/{/$(show_section_regexp "$SECARG")/d;/$(show_section_regexp)/p;d};p}" "$2" >> "$TMP_FILE"
496         cp "$TMP_FILE" "$2"
497         rm -f "$TMP_FILE"
498      else
499         show_section_header "$SECARG" >> "$2"
500         echo "$(show_pkg_line "$1")" >> "$2"
501      fi
502   else
503      echo "$(show_pkg_line "$1")" >> "$2"
504   fi
505   return 0
506}
507
508do_rm_pkg_from_file(){
509#   sed -i -e "\$a$(show_pkg_line "$1")" "$2"
510   sed -i -e "/$(show_pkg_regexp "$1")/d" "$2"
511   return 0
512}
513
514
515do_add_tdir(){
516   [ -e "$CONF_FILE" ] || return 0
517   tdir="${TG_BASE_DIR}/${1}"
518   sed -i -e "/^TG_SEED_DIRS=/s%^.*$%TG_SEED_DIRS=\"$TG_SEED_DIRS $tdir\"%" "$CONF_FILE"
519   if ! grep -q "^TG_SEED_DIRS=" "$CONF_FILE" ; then
520      echo "TG_SEED_DIRS=\"$TG_SEED_DIRS $tdir\"" >> "$CONF_FILE"
521   fi
522   return 0
523}
524
525do_rm_tdir(){
526   [ -e "$CONF_FILE" ] || return 0
527   tdir="${TG_BASE_DIR}/${1}"
528   TG_SEED_DIRS="$(echo "$TG_SEED_DIRS" |sed -e "s%$tdir%%g")"
529   sed -i -e "/^TG_SEED_DIRS=/s%^.*$%TG_SEED_DIRS=\"$TG_SEED_DIRS\"%" "$CONF_FILE"
530   if ! grep -q "^TG_SEED_DIRS=" "$CONF_FILE" ; then
531      echo "TG_SEED_DIRS=\"$TG_SEED_DIRS\"" >> "$CONF_FILE"
532   fi
533   return 0
534}
535
536do_rm_dir(){
537   [ "$DARG" ] || return 0
538   target="${TG_BASE_DIR}/${DARG}"
539   if [ -d "$target" ] ; then
540      rmdir "$target"
541      do_rm_tdir "$DARG"
542      do_conf_update
543      show_info "Deleted seed directory (${TG_BASE_DIR}/) ${DARG}"
544   fi
545   return 0
546}
547
548do_add_dir(){
549   [ "$DARG" ] || return 0
550   target="${TG_BASE_DIR}/${DARG}"
551   if [ ! -d "$target" ] ; then
552      mkdir -p "$target"
553      do_add_tdir "$DARG"
554      do_conf_update
555      show_info "Created new seed directory (${TG_BASE_DIR}/) ${DARG}"
556   fi
557   return 0
558}
559
560do_add_seed(){
561   [ "$SARG" ] || return 0
562   if ! seedfile="$(test_seed_file)" ; then
563      [ "$DARG" ] || DARG="$(basename "$TG_SEED_DEFAULT_DIR")"
564      tdir="${TG_BASE_DIR}/${DARG}"
565      target="$tdir/${SARG}"
566      test_seed_dir
567      touch "$target"
568      do_conf_update
569      show_info "Created new seed file (${TG_BASE_DIR}/${DARG}/) ${SARG}"
570   elif [ "$DARG" ] ; then
571      # verify supplied DARG
572      target="${TG_BASE_DIR}/${DARG}/${SARG}"
573      [ "$target" = "$seedfile" ] || die "There is a seed with the same name in other directory ($seedfile)"
574   fi
575   return 0
576}
577
578do_clear_seed(){
579   [ "$SARG" ] || return 0
580   seedfile="$(test_seed_file)" || return 0
581   if get_confirmation "Do you want to remove ALL pkgs from \"$SARG\"?" ; then
582      sed -i -e "/$(show_pkg_regexp '.*')/d" "$seedfile" 
583      show_info "Removed ALL pkgs from seed ${SARG}"
584   fi
585   return 0
586}
587
588is_empty_file(){
589   grep -q  "[^[:blank:]]" "$1" || return 0
590   return 1
591}
592
593do_rm_seed(){
594   [ "$SARG" ] || return 0
595   seedfile="$(test_seed_file)" || return 0
596   if ! is_empty_file "$seedfile" ; then
597      show_warning "$SARG is not empty and will be preserved"
598      return 0
599   fi
600   if get_confirmation "Do you want to ERASE seed \"$SARG\"?" ; then
601      rm -f "$seedfile"
602      show_info "Erased seed ${SARG}"
603      do_conf_update
604   fi
605   return 0
606}
607
608do_rm_pkgs(){
609   [ ${#PARG[@]} -gt 0 ] || return 0
610   [ "$SARG" ] || return 0
611   i=0
612   while [ $i -lt ${#PARG[@]} ] ; do
613      sfile="$(test_seed_file)" || die "Invalid seed file \"$sfile\""
614      if test_pkg_in_file "${PARG[$i]}" "$sfile" ; then
615         do_rm_pkg_from_file "${PARG[$i]}" "$sfile"
616         show_info "Removed pkg \"${PARG[$i]}\" from seed \"$SARG\""
617      fi
618      i=$(($i + 1))
619   done
620   return 0
621}
622
623do_mv_pkgs(){
624   [ ${#PARG[@]} -gt 0 ] || return 0
625   [ "$SARG" ] || return 0
626   i=0
627   tfile="$1"
628   TMP_FILE="$(tempfile)"
629   while [ $i -lt ${#PARG[@]} ] ; do
630      sfile="$(test_seed_file)" || die "Invalid seed file \"$sfile\""
631      if grep "$(show_pkg_regexp "${PARG[$i]}")" "$sfile" > "$TMP_FILE" ; then
632         do_rm_pkg_from_file "${PARG[$i]}" "$tfile"
633         cat "$TMP_FILE" >> "$tfile"
634         do_rm_pkg_from_file "${PARG[$i]}" "$sfile"
635         show_info "Moved pkg \"${PARG[$i]}\" from seed $SARG to seed $(basename $tfile)"
636      fi
637      i=$(($i + 1))
638   done
639   return 0
640
641}
642
643do_add_pkgs(){
644   [ ${#PARG[@]} -gt 0 ] || return 0
645   [ "$SARG" ] || return 0
646   i=0
647   while [ $i -lt ${#PARG[@]} ] ; do
648      sfile="$(test_seed_file)" || die "Invalid seed file \"$sfile\""
649      if ! test_pkg_in_file "${PARG[$i]}" "$sfile" ; then
650         do_add_pkg_to_file "${PARG[$i]}" "$sfile"
651         show_info "Added pkg \"${PARG[$i]}\" to seed \"$SARG\""
652      fi
653      i=$(($i + 1))
654   done
655   return 0
656}
657
658show_include_line(){
659   echo "#include $1"
660}
661
662do_include_seed_in_file(){
663   INCLUDE_REGEXP="^#include\b"
664   test_seed_file &>/dev/null || show_warning "Ignoring invalid seed name $SARG"
665   if ! grep -q "^$(show_include_line "$SARG")$" "$1" ; then
666      TMP_FILE="$(tempfile)"
667      { show_include_line "$SARG" ; cat "$1" ; } > "$TMP_FILE"
668      cp "$TMP_FILE" "$1"
669      rm -f "$TMP_FILE"
670   fi
671   return 0
672}
673
674do_uninclude_seed_in_file(){
675   sed -i -e "/^$(show_include_line "$SARG")$/d" "$1"
676   return 0
677}
678
679die(){
680   echo -e "$1" >&2
681   close3
682   exit 1
683}
684
685show_warning(){
686   while [ "$1" ] ; do
687      echo "tg Warning: $1" >&2
688      shift
689   done
690   return 0
691}
692
693show_info(){
694   while [ "$1" ] ; do
695      echo "tg: $1"
696      shift
697   done
698   return 0
699}
700
701expand_packages(){
702#               expand_seed "$SARG" |sed -ne "/$(show_pkg_regexp '.*')/{s%^ \* %%;s%(%%;s%)%%;s%[[:blank:]].*$%%;p}"
703   expand_seed "$SARG" |sed -ne "/$(show_pkg_regexp '[^\$]*')/{s%^ \* %%;s%[[:blank:]].*$%%;p}"
704}
705
706usage() {
707  CMD_NAME="$(basename "$0")"
708  die \
709"Usage: $CMD_NAME [options] {expand-all(sow-all)|list|clean|test|quick-list|version}\n\
710       $CMD_NAME [options] {expand(sow)|expand-packages|depends(tree)|add|del|rm|test|readlink|erase|plist} SEED_ID1 [SEED_ID2 ... SED_IDn]\n\
711       $CMD_NAME [options] mv SEED_ID1 SEED_ID2\n\
712       $CMD_NAME [options] {include|uninclude} SEED_ID1 [SEED_ID2 ... SEED_IDn] DEST_SEED_ID\n\
713       $CMD_NAME [options] {edit|vi|vim|pager|less|more|nano} SEED_ID\n\
714       $CMD_NAME [options] {scite} SEED_ID\n\
715       $CMD_NAME [options] exec CMD SEED_ID\n\
716       $CMD_NAME [options] {mkdir|rmdir} SEED_DIR1 [SEED_DIR2 ... SEED_DIRn]\n\
717       $CMD_NAME [options] pgrep PKG_REGEXP\n\
718       $CMD_NAME [options] {pdel|prm|padd} PKG_ID1 [PKG_ID2 ... PKG_IDn]\n\
719       options: -c CONFFILE -s (short listing) -l (long listing) -y (assume yes)\n\
720       PKG_ID: PKG_NAME or SEED:PKG_NAME or SEED:PKG_NAME1,PKG_NAME2,...PKG_NAMEn"
721}
722
723
724# ----
725# main
726# ----
727
728# read commandline
729case "$1" in
730   quick-list)
731      do_conf_update
732      show_quick_list
733      exit 0
734      ;;
735   version)
736      [ ! -r "$TG_VERSION_FILE" ] || cat "$TG_VERSION_FILE"
737      exit 0
738      ;;
739esac
740
741while [ $# -ge 1 ] ; do
742   case "$1" in
743      expand|sow|expand-all|sow-all|list|tree|depends|clean|pgrep|add|del|rm|mv|edit|mkdir|rmdir|pdel|prm|padd|edit|vi|vim|readlink|pager|less|more|erase|plist|include|uninclude|ptest-dup|searchandshow|expand-packages|test|nano|scite)
744         if [ -z "$ACTION" ] ; then
745            ACTION="$1"
746         else
747            # just an argument that equals to a action name
748            ARGC=$(( $ARGC + 1 ))
749            ARGV[$ARGC]="$1"
750         fi
751         ;;
752      exec)
753         if [ -z "$ACTION" ] ; then
754            ACTION="$1"
755            shift
756            CMD="$1"
757            if [ -z "$CMD" ] ; then
758               $TG_COWSAY 'Please, supply a command to execute !!!'
759               exit 1
760            fi
761         else
762            # just an argument that equals to a action name
763            ARGC=$(( $ARGC + 1 ))
764            ARGV[$ARGC]="$1"
765         fi
766         ;;
767      -c)
768         shift
769         [ "$1" ] || usage
770         CONF_FILE="$1"
771         ;;
772      -s)
773         SHORT_OUTPUT="Y"
774         ;;
775      -l)
776         SHORT_OUTPUT=""
777         ;;
778      -y)
779         ASSUME_YES="y"
780         ;;
781      -*)
782         # an invalid option -* or --* or a single -
783         usage
784         ;;
785        *)
786         ARGC=$(( $ARGC + 1 ))
787         ARGV[$ARGC]="$1"
788         ;;
789     esac
790     shift
791done
792
793do_conf_update
794
795# some checks
796[ "$ACTION" ] || usage
797#[ $ARGC -le 1 ] || usage
798for d in $TG_SEED_DIRS $TG_INCLUDE_DIRS ; do
799   [ -d "$d" ] || die "Error: seed directory \"$d\" not found"
800done
801#[ "$TG_OUTPUT_SEEDS_LIST" -a -f "$TG_OUTPUT_SEEDS_LIST" ] || die "Invalid value in TG_OUTPUT_SEEDS_LIST variable: TG_OUTPUT_SEEDS_LIST=\"$TG_OUTPUT_SEEDS_LIST\""
802#[ -s "$TG_OUTPUT_SEEDS_LIST" ] || die "No output seeds in \"$TG_OUTPUT_SEEDS_LIST\""
803
804#open3
805#while read TG <&3 ; do
806#   n=$(get_seed_index "$TG") || die "Invalid seed \"$TG\" in output seeds list"
807#   [ "${SEEDS_TYPE[$n]}" = "$TG_MAINSEED" ] || die "Output seed \"$TG\" is defined as an include file (${SEEDS_PATH[$n]})"
808#done
809#close3
810PKG_LISTS_FILES="/var/lib/dpkg/available $(find "$PKG_LISTS_DIR" -xtype f -name "*_Packages" |tr "\n" " ")"
811
812rc=0
813
814do_split_sarg "${ARGV[1]}"
815#SEED_NAME="${ARGV[1]}"
816case "$ACTION" in
817   expand|sow|expand-packages|test)
818      [ $ARGC -gt 0 ] || usage
819      j=1
820      while [ "$SARG" ] ; do
821         if is_regular_seed "$SARG" ; then
822            case "$ACTION" in
823               expand-packages)
824                  expand_packages
825                  ;;
826               test)
827                  for p in $(expand_packages) ; do
828                      test_pkg_name "$p"
829                  done
830                  ;;
831               *)
832                  expand_seed "$SARG" || rc=$?
833                  ;;
834            esac
835         fi
836         j=$(($j + 1))
837         do_split_sarg "${ARGV[$j]}"
838      done
839      ;;
840   expand-all|sow-all)
841      clear_final_seeds
842#     open3
843#      while read TG <&3 ; do
844      SHORT_OUTPUT="y"
845      for TG in $(list_seeds) ; do
846         write_final_seed "$TG"
847      done
848      TMP_FILE="$(tempfile)"
849      write_final_seed "$TG_STRUCTURE" "$TMP_FILE"
850      # check STRUCTURE coherency ...
851      do_final_structure_file "$TMP_FILE"
852      rm -f "$TMP_FILE"
853      echo ""
854#      close3
855      ;;
856   list)
857      list_seeds | sort | column || rc=$?
858      ;;
859   depends|tree)
860      [ $ARGC -gt 0 ] || usage
861      j=1
862      while [ "$SARG" ] ; do
863         show_depends "$SARG"
864         j=$(($j + 1))
865         do_split_sarg "${ARGV[$j]}"
866      done
867      ;;
868   plist)
869      j=1
870      while [ "$SARG" ] ; do
871         show_pkgs
872         j=$(($j + 1))
873         do_split_sarg "${ARGV[$j]}"
874      done
875      ;;
876   clean)
877      clear_final_seeds
878      ;;
879   mkdir)
880      j=1
881      do_split_darg "${ARGV[1]}"
882      while [ "$DARG" ] ; do
883         do_add_dir
884         j=$(($j + 1))
885         do_split_darg "${ARGV[$j]}"
886      done
887      ;;
888   rmdir)
889      j=1
890      do_split_darg "${ARGV[1]}"
891      while [ "$DARG" ] ; do
892         if is_empty_seed_dir ; then
893            do_rm_dir
894         else
895            show_warning "The directory \"$DARG\" is not empty and will be preserved"
896         fi
897         j=$(($j + 1))
898         do_split_darg "${ARGV[$j]}"
899      done
900      ;;
901   add)
902      j=1
903      while [ "$SARG" ] ; do
904         do_add_dir
905         do_add_seed
906         do_add_pkgs
907         j=$(($j + 1))
908         do_split_sarg "${ARGV[$j]}"
909      done
910      ;;
911   padd)
912      j=1
913      do_split_parg "${ARGV[1]}"
914      while [ ${#PARG[@]} -gt 0 ] ; do
915         [ "$SARG" ] || die "Invalid seed name"
916         do_add_pkgs
917         j=$(($j + 1))
918         do_split_parg "${ARGV[$j]}"
919      done
920      ;;
921   rm)
922      j=1
923      while [ "$SARG" ] ; do
924         if [ ${#PARG[@]} -gt 0 ] ; then
925            do_rm_pkgs
926         else
927            do_clear_seed
928         fi
929         j=$(($j + 1))
930         do_split_sarg "${ARGV[$j]}"
931      done
932      ;;
933   prm)
934      j=1
935      while [ ${#PARG[@]} -gt 0 ] ; do
936         do_rm_pkgs
937         j=$(($j + 1))
938         do_split_sarg "${ARGV[$j]}"
939      done
940      ;;
941   about)
942      # uhmm ... at this point, all the tests has been passed so ...
943      # print a nice message and let $rc variable to do this work
944      echo -e "OK folks, everything is gonna be all right, but don't forget that ...\n\
945all your seeds are belong to us"
946      ;;
947   pgrep)
948      [ $ARGC -gt 0 ] || usage
949      get_pkg_seed_files "${ARGV[1]}" || rc=$?
950      ;;
951   edit|vi|vim|nano|scite)
952      [ "$ACTION" = "edit" ] || TG_EDITOR="$ACTION"
953      if sfile="$(test_seed_file)" ; then
954         $TG_EDITOR "$sfile"
955      fi
956      ;;
957   pager|less|more)
958      [ "$ACTION" = "pager" ] || TG_PAGER="$ACTION"
959      j=1
960      while [ "$SARG" ] ; do
961         if sfile="$(test_seed_file)" ; then
962            $TG_PAGER "$sfile"
963         fi
964         j=$(($j + 1))
965         do_split_sarg "${ARGV[$j]}"
966      done
967      ;;
968   erase)
969      j=1
970      while [ "$SARG" ] ; do
971         do_rm_seed
972         j=$(($j + 1))
973         do_split_sarg "${ARGV[$j]}"
974      done
975      ;;
976   readlink)
977      j=1
978      while [ "$SARG" ] ; do
979         if sfile="$(test_seed_file)" ; then
980            readlink -f "$sfile"
981         fi
982         j=$(($j + 1))
983         do_split_sarg "${ARGV[$j]}"
984      done
985      ;;
986   mv)
987      do_split_sarg "${ARGV[$ARGC]}"
988      DEST_SEED="$SARG"
989      target_file="$(test_seed_file)" || rc=$?
990      j=1
991      do_split_sarg "${ARGV[1]}"
992      while [ "$target_file" -a ${#PARG[@]} -gt 0 -a "$SARG" ] ; do
993         do_mv_pkgs "$target_file"
994         j=$(($j + 1))
995         do_split_sarg "${ARGV[$j]}"
996      done
997      ;;
998   exec)
999      if target_file="$(test_seed_file)" ; then
1000         exec $CMD "$target_file"
1001      fi
1002      ;;
1003   include)
1004      [ $ARGC -gt 1 ] || usage
1005      do_split_sarg "${ARGV[$ARGC]}"
1006      DEST_SEED="$SARG"
1007      target_file="$(test_seed_file)" || die "Invalid destination seed: $SARG"
1008      j=1
1009      do_split_sarg "${ARGV[1]}"
1010      while [ "$SARG" -a $j -lt $ARGC ] ; do
1011         do_include_seed_in_file "$target_file"
1012         j=$(($j + 1))
1013         do_split_sarg "${ARGV[$j]}"
1014      done
1015      echo "Seeded included in $SARG"
1016      ;;
1017   uninclude)
1018      [ $ARGC -gt 1 ] || usage
1019      do_split_sarg "${ARGV[$ARGC]}"
1020      DEST_SEED="$SARG"
1021      target_file="$(test_seed_file)" || die "Invalid destination seed: $SARG"
1022      j=1
1023      do_split_sarg "${ARGV[1]}"
1024      while [ "$SARG" -a $j -lt $ARGC ] ; do
1025         do_uninclude_seed_in_file "$target_file"
1026         j=$(($j + 1))
1027         do_split_sarg "${ARGV[$j]}"
1028      done
1029      echo "Seeded removed at $SARG"
1030      ;;
1031      searchandshow)
1032      for f in $(tg sow "$1" | grep " \* " | sed -e "s%(%%g" | sed -e "s%)%%g" | cut -d " " -f3) ; do if [ $( apt-cache search $f | wc -l ) -eq 0 ] ; then  echo "Not present: $f" ; fi ; done
1033      ;;
1034   *)
1035      usage
1036      ;;
1037esac
1038
1039
1040exit $rc
Note: See TracBrowser for help on using the repository browser.