qi: determine whether to preserve debugging shell flags (for the purpose)
[dragora.git] / qi / src / qi.in
blob33e6e3f066a3de09f72bc3730e2147a4b81af1d9
1 #! /bin/sh -
2 # Copyright (C) 2016-2018 Matias Fonzo <selk@dragora.org>
4 # This program is free software: you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation, either version 3 of the License, or
7 # (at your option) any later version.
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
14 # You should have received a copy of the GNU General Public License
15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
17 # EXIT STATUS
18 # 0 = Successful completion
19 # 1 = Minor common errors (e.g: help usage, support not available)
20 # 2 = Command execution error
21 # 3 = Integrity check error for compressed files
22 # 4 = File empty, not regular, or expected
23 # 5 = Empty or not defined variable
24 # 6 = Package already installed
25 # 10 = Network manager error
27 #### Functions
29 usage()
31 printf "%s\n" \
32 "Qi - A practical and user-friendly package manager." \
33 "" \
34 "Usage: $PROGRAM [OPTION...] [FILE]..." \
35 "" \
36 "Operation mode:" \
37 " -b Build packages using recipes" \
38 " -c Create .tlz package from directory" \
39 " -d Delete packages" \
40 " -i Install packages" \
41 " -o Resolve build order through .order files" \
42 " -u Update packages (implies -i, -d with -p)" \
43 " -x Extract packages for debugging purposes" \
44 "" \
45 "Common options:" \
46 " -L Print and exit default directory locations" \
47 " -N Don't read the configuration file" \
48 " -P <DIR> Package directory for (de)installations;" \
49 " only valid for -i, -d, or -u options" \
50 " -t <DIR> Target directory for symbolic links;" \
51 " only valid for -i, -d, or -u options" \
52 " -k Keep (don't delete) srcdir or destdir" \
53 " Keep (don't delete) package directory" \
54 " this is for -b, -d or -u options" \
55 " -p Prune conflicts on package (de)installation" \
56 " -r Use the named directory as the root directory" \
57 " for installing, deleting, or upgrading packages." \
58 " The target directory, package directory will be" \
59 " relative to this specific directory" \
60 " -v Be verbose (a 2nd -v gives more)" \
61 "" \
62 "Options for 'build' mode (-b):" \
63 " -O <DIR> Where the produced packages are written" \
64 " -W <DIR> Where archives, patches, and recipes are expected" \
65 " -Z <DIR> Where (compressed) sources will be found" \
66 " -a Architecture to use [detected]" \
67 " -f Force proceeding with a recipe" \
68 " -j Parallel jobs for the compiler" \
69 " -1 Increment release number (release + 1)" \
70 " -3 Resume skipping completed recipes option" \
71 " -n Don't create a .tlz package" \
72 "" \
73 "Other options:" \
74 " -h Display this help and exit" \
75 " -V Output version information" \
76 "" \
77 "Some influential environment variables:" \
78 " TMPDIR Temporary directory for sources" \
79 " QICFLAGS C compiler flags" \
80 " QICXXFLAGS C++ compiler flags" \
81 " QILDFLAGS Linker flags" \
82 "" \
83 "When FILE is -, read standard input." \
87 version()
89 printf "%s\n" \
90 "$PROGRAM @VERSION@" \
91 "Copyright (C) 2016-2018 Matias Andres Fonzo." \
92 "License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>" \
93 "This is free software: you are free to change and redistribute it." \
94 "There is NO WARRANTY, to the extent permitted by law."
97 warn()
99 printf "%s\n" "$@" 1>&2
102 is_readable()
104 if test -e "$1"
105 then
106 if ! test -r "$1"
107 then
108 echo "${PROGRAM}: cannot read ${1}: Permission denied" 1>&2
109 return 1
111 else
112 echo "${PROGRAM}: cannot access ${1}: No such file or directory" 1>&2
113 return 1
117 # Portable alternative to the file operator -nt (among shells)
118 is_newer()
120 if test -n "$(find $1 -prune -newer $2 -print)"
121 then
122 return 0
124 return 1
127 # Determine whether $2 matches pattern $1
128 fnmatch()
130 case $2 in
132 return 0
135 return 1
137 esac
140 chkstatus_or_exit()
142 status=$?
144 if test $status -ne 0
145 then
146 echo "Return status = $status" 1>&2
147 exit ${1-2}; # If not given, defaults to 2
150 unset status
153 readconfig()
155 if test $RC = RC
156 then
157 is_readable "$HOME/.qirc" 2> /dev/null && RCFILE="$HOME/.qirc";
159 echo "Processing \`${RCFILE}' ..."
161 test -f "$RCFILE" || {
162 warn "${RCFILE} is not a regular file."
163 return 1
166 # Parse config file
167 while IFS='=' read -r variable value
169 case $variable in
170 \#* | "") # Ignore commented or blank lines
171 continue
173 esac
175 # Set variable name avoiding code execution
176 eval "$variable=\${value}"
177 done < "$RCFILE"
181 #### Mode functions
183 build_mode()
185 recipe=$1
187 # A recipe is any valid regular file, the current working directory
188 # has priority over the working tree (or where the recipes reside).
189 # The 'worktree' is the second place where to find a recipe. Also,
190 # we complete the possibility of using the directory name to invoke
191 # a recipe if it contains "recipe" as valid file name.
193 if test ! -f "$recipe"
194 then
195 if test -f "${recipe}/recipe"
196 then
197 recipe="${recipe}/recipe"
198 elif test -f "${worktree}/recipes/${recipe}/recipe"
199 then
200 recipe="${worktree}/recipes/${recipe}/recipe"
204 test -f "$recipe" || {
205 warn "\`${recipe}' is not a regular file."
206 return 4
209 # Complain if the file name is not "recipe"
211 if test "${recipe##*/}" = recipe
212 then
213 true
214 else
215 warn "\`${recipe}' is not a valid recipe name."
216 return 4
219 # Start preparations to import the recipe
221 # Separate the directory name from the file name,
222 # getting its absolute path and base name
224 CWD=$(CDPATH= cd -P -- $(dirname -- "$recipe") && printf "$PWD")
225 recipe=$(basename -- "$recipe")
227 # Check readability for load the recipe on success
229 is_readable "${CWD}/$recipe" || exit 4
231 # Find target architecture if 'arch' is not set
232 if test -z "$arch"
233 then
234 arch=$(${CC:-cc} -dumpmachine 2> /dev/null) || arch=unknown
235 arch="${arch%%-*}" # Get the rid of target triplet.
238 # Re-create external directories
239 mkdir -p -- "${worktree}/archive" \
240 "${worktree}/patches" \
241 "${worktree}/recipes" \
242 "$tardir"
244 # Variables treatment for the current and next recipe.
246 # Unset special variables that can only be predefined on
247 # the recipe and not coming from ${sysconfdir}/qirc
249 unset srcdir destdir pkgname pkgversion program version release \
250 fetch description homepage license replace full_pkgname \
251 CFLAGS CXXFLAGS LDFLAGS
253 # The following variables must be saved and restored
254 save_arch="${save_arch:=$arch}"
255 save_jobs="${save_jobs:=$jobs}"
256 save_outdir="${save_outdir:=$outdir}"
257 save_opt_nopkg="${save_opt_nopkg:=$opt_nopkg}"
259 # Reset variable values in case of return
260 arch=$save_arch
261 jobs=$save_jobs
262 outdir=$save_outdir
263 opt_nopkg=$save_opt_nopkg
265 # The following variables cannot be redefined on the recipe
266 readonly worktree netget rsync
268 # Import the recipe
270 echo "{@} Building from ${CWD}/$recipe ..."
271 . "${CWD}/$recipe"
273 # Check for required variables
274 if test -z "$program"
275 then
276 warn "${recipe}: The variable 'program' is not defined."
277 exit 5
279 if test -z "$version"
280 then
281 warn "${recipe}: The variable 'version' is not defined."
282 exit 5
284 if test -z "$release"
285 then
286 warn "${recipe}: The variable 'release' is not defined."
287 exit 5
290 # Pre-settings before to start building
292 # Increment the release number if the option was given
293 if test "$opt_incr_release" = opt_incr_release
294 then
295 release=$(( release + 1 ))
298 # Allow the dot as definition for 'tardir'
299 if test "$tardir" = .
300 then
301 tardir="$CWD"
304 # Set default values for the following special variables
306 pkgname="${pkgname:=$program}"
307 pkgversion="${pkgversion:=$version}"
308 srcdir="${srcdir:=${program}-$version}"
309 destdir="${destdir:=${TMPDIR}/package-$pkgname}"
311 # Complete package name adding 'pkgversion-arch+release'
312 full_pkgname="${full_pkgname:=$pkgname-${pkgversion}-${arch}+${release}}"
314 # If a package is going to be created, the existence of a
315 # previous build will be detected and reported. Under normal
316 # conditions the recipe is built as long as it is newer than
317 # the produced package, if not, we warn to the user about it.
318 # Rebuilding the package is possible (through the force ;-)
320 if test "$opt_nopkg" != opt_nopkg && \
321 { test "$opt_force" != opt_force && \
322 test -r "${outdir}/${full_pkgname}.tlz" ; }
323 then
324 if is_newer "${CWD}/$recipe" "${outdir}/${full_pkgname}.tlz"
325 then
326 warn \
327 "" \
328 "The recipe is more RECENT than the detected package:" \
329 "" \
330 "$( stat -c "%y %n" "${CWD}/$recipe" )" \
331 "$( stat -c "%y %n" "${outdir}/${full_pkgname}.tlz" )" \
332 "" \
333 " This recipe will be processed ..." \
335 elif test -e "${CWD}/post-install" && \
336 is_newer "${CWD}/post-install" "${CWD}/$recipe"
337 then
338 warn \
339 "" \
340 "The post-install script is more RECENT than the recipe:" \
341 "" \
342 "$( stat -c "%y %n" "${CWD}/post-install" )" \
343 "$( stat -c "%y %n" "${CWD}/$recipe" )" \
344 "" \
345 " The recipe will be re-processed ..." \
347 touch "${CWD}/$recipe"
348 elif test "$opt_ignoreqsts" = opt_ignoreqsts
349 then
350 warn "Recipe for '${full_pkgname}.tlz': Ignored." ""
351 return 0
352 else
353 warn \
354 "" \
355 "This recipe ALREADY produced a package:" \
356 "$( stat -c "%y %n" "${outdir}/${full_pkgname}.tlz" )" \
357 "" \
358 "The recipe is still OLDER than the produced package:" \
359 "$( stat -c "%y %n" "${CWD}/$recipe" )" \
360 "" \
361 " Probably nothing has changed." \
364 # In non-interactive mode, the user is asked about
365 # rebuilding the package. In interactive mode,
366 # the user need to pass the option explicitly
367 if test ! -t 0
368 then
369 printf "%b" \
370 "Do you want to rebuild this package?\n" \
371 "1) Yes, built it\n" \
372 "2) No, skipt it [default]\n" \
373 "3) Resume, skipping completed recipes\n" \
374 "Choose an option number: " > /dev/tty
375 IFS= read -r ANSWER < /dev/tty || exit 2;
376 case $ANSWER in
378 echo "$ANSWER" > /dev/tty
379 unset ANSWER
382 unset ANSWER
383 echo "Building UNPROCESSED/MODIFIED recipes ..." > /dev/tty
384 echo ""
385 opt_ignoreqsts=opt_ignoreqsts
386 readonly opt_ignoreqsts
387 return 0
390 unset ANSWER
391 echo "Recipe for '${full_pkgname}.tlz': Cancelled." > /dev/tty
392 echo ""
393 return 0
395 esac
396 else
397 warn "Use the -f option to re-process ${CWD}/$recipe." ""
398 return 6;
403 # Fetch remote sources
405 echo "Fetching remote sources if needed ..."
406 if test -n "$fetch"
407 then
408 for origin in $fetch
410 _source="${origin##*/}"
412 echo "Looking for \"$_source\" ..."
414 echo "Verifying checksum file \"${_source}.sha256\" from '${tardir}'"
415 if test -e "${tardir}/${_source}.sha256"
416 then
417 ( cd -- "$tardir" && sha256sum - ) < "${tardir}/${_source}.sha256"
418 chkstatus_or_exit
419 continue;
420 else
421 warn "${_source}.sha256: Checksum file does not exist, yet."
424 # Download source or resume if allowed
426 if test ! -e "${tardir}/$_source"
427 then
428 warn " Can't find $_source in ${tardir};" \
429 "attempting to get it from ${origin%/*} ..."
432 case $origin in
433 rsync://*)
435 cd -- "$tardir" && $rsync $origin || exit $?
436 sha256sum $_source > ${_source}.sha256
437 ); chkstatus_or_exit 10
439 *://*)
441 cd -- "$tardir" && $netget $origin || exit $?
442 sha256sum $_source > ${_source}.sha256
443 ); chkstatus_or_exit 10
446 warn "${PROGRAM}: Unrecognized protocol for ${origin}."
447 exit 4
448 esac
449 done
450 unset origin _source
451 else
452 warn "The variable 'fetch' is empty."
455 # Prepare special directories for build the source,
456 # the destination and the output of the package
458 echo "Preparing directories ..."
460 if test -d "${TMPDIR}/$srcdir" && test -z "$keep_srcdir"
461 then
462 rm -rf -- "${TMPDIR}/$srcdir" || chkstatus_or_exit
463 echo "removed directory: '${TMPDIR}/$srcdir'"
466 if test -d "$destdir"
467 then
468 rm -rf -- "$destdir" || chkstatus_or_exit
469 echo "removed directory: '$destdir'"
471 mkdir -p -- "$destdir" || chkstatus_or_exit
472 echo "mkdir: created directory '$destdir'"
474 if test ! -d "$outdir"
475 then
476 mkdir -p -- "$outdir" || chkstatus_or_exit
477 echo "mkdir: created directory '$outdir'"
480 echo "Entering to 'TMPDIR': $TMPDIR ..."
481 cd -- "$TMPDIR" || chkstatus_or_exit
483 # Set trap before to run the build() function in order
484 # to catch the return status, exit code 2 if fails
486 trap 'chkstatus_or_exit 2' EXIT HUP INT QUIT ABRT TERM
488 # Determine if the debugging indicators of the shell should be
489 # retained, assuming that it has been previously passed
490 case $- in *x*)
491 _xtrace_flag_is_set=xtrace_flag_is_set ;;
492 esac
494 echo "Running build() ..."
495 build
496 unset build
498 # Turn off possible shell flags coming from the recipe
500 set +e
501 if test "$_xtrace_flag_is_set" != xtrace_flag_is_set
502 then
503 set +x
506 # Reset given signals
507 trap - EXIT HUP INT QUIT ABRT TERM
509 # If 'destdir' is empty, the package won't be created
510 if rmdir -- "$destdir" 2> /dev/null
511 then
512 warn "The package \"${full_pkgname}.tlz\" won't be created. 'destdir' is empty."
513 opt_nopkg=opt_nopkg
516 # Create (make) the package
518 if test "$opt_nopkg" != opt_nopkg
519 then
520 # Edit the recipe when 'release' is incremented
521 if test "$opt_incr_release" = opt_incr_release
522 then
523 echo ",s/^\(release\)=.*/\1=${release}/"$'\nw' | \
524 ed "${CWD}/$recipe" || chkstatus_or_exit
527 mkdir -p -- "${destdir}/var/lib/qi" || chkstatus_or_exit
529 # Include a recipe copy into the package
530 cp -p "${CWD}/$recipe" \
531 "${destdir}/var/lib/qi/${full_pkgname}.recipe" && \
532 chmod 644 "${destdir}/var/lib/qi/${full_pkgname}.recipe" || chkstatus_or_exit
534 # Detect post-install script for inclusion
536 if test -f "${CWD}/post-install"
537 then
538 echo "${CWD}/post-install: Detected."
539 cp -p "${CWD}/post-install" \
540 "${destdir}/var/lib/qi/${full_pkgname}.sh" && \
541 chmod 644 "${destdir}/var/lib/qi/${full_pkgname}.sh" || chkstatus_or_exit
544 # Detect declared package names for later replacement
546 if test -n "$replace"
547 then
548 warn \
549 "The following package names has been declared for replacement:" \
550 " $replace"
552 rm -f "${destdir}/var/lib/qi/${full_pkgname}.replace"
553 for item in $replace
555 echo "$replace" >> "${destdir}/var/lib/qi/${full_pkgname}.replace"
556 done
557 unset item
560 # Create meta file for the package information
561 echo "Creating meta file ${full_pkgname}.tlz.txt ..."
562 do_meta > "${outdir}/${full_pkgname}.tlz.txt" || chkstatus_or_exit
564 # Make a copy of it for the database
565 cp -p "${outdir}/${full_pkgname}.tlz.txt" \
566 "${destdir}/var/lib/qi/${full_pkgname}.txt" || chkstatus_or_exit
568 # Produce the package
569 cd -- "$destdir" && create_mode "${outdir}/${full_pkgname}.tlz"
572 # Back to the current working directory
573 cd -- "$CWD" || chkstatus_or_exit
575 # Delete 'srcdir' or 'destdir' if -k is not given
576 if test "$opt_keep" != opt_keep
577 then
578 echo "Deleting temporary directories ..."
580 srcdir="${srcdir%%/*}" # Directory name without parents.
582 if test -r "${TMPDIR}/$srcdir"
583 then
584 if test -z "$keep_srcdir"
585 then
586 rm -rf -- "${TMPDIR}/$srcdir" || chkstatus_or_exit
587 echo "removed directory: '${TMPDIR}/${srcdir}'"
588 else
589 warn "The variable 'keep_srcdir' is set:" \
590 "'${TMPDIR}/${srcdir}' will not be deleted."
594 if test -r "$destdir"
595 then
596 rm -rf -- "$destdir" || chkstatus_or_exit
597 echo "removed directory: '$destdir'"
601 # Install or update the package if -i or -u was passed
602 if test "$opt_nopkg" != opt_nopkg
603 then
604 if test "$opt_install" = opt_install
605 then
606 install_mode "${outdir}/${full_pkgname}.tlz"
607 elif test "$opt_update" = opt_update
608 then
609 upgrade_mode "${outdir}/${full_pkgname}.tlz"
613 echo ""
614 echo "All done for ${CWD}/${recipe}."
615 echo ""
618 create_mode()
620 directory=$(dirname -- "$1")
621 name=$(basename -- "$1")
623 # Perform sanity checks
625 is_readable "$directory" || exit 4
627 # Check again to find out if it is a valid directory
628 test -d "$directory" || {
629 warn "Package directory '$name' does not exist."
630 exit 4
633 test "$directory" = . && {
634 warn "Cannot create package on current working directory."
635 exit 4
638 test "$name" = "${name%.tlz}" && {
639 warn \
640 "Package format '$name' not supported." \
641 "It should be \`name-version-architecture+release.tlz'"
642 exit 4
645 echo "{#} Creating package $name at $directory ..."
647 ( umask 022 ; tar cvf - ./* | lzip -9cvv ) > "${directory}/$name"
648 chkstatus_or_exit 3
650 echo "Package created \"${directory}/${name}\"."
652 echo "${directory}/$name: Creating SHA256 checksum ..."
653 ( cd -- "$directory" && sha256sum $name > ${name}.sha256 )
655 # Remove used variables
656 unset directory name
659 delete_mode()
661 expunge="${1%%.tlz}"
663 echo "<<< Deleting package $rootdir${expunge} from $packagedir ..."
665 # If package is not a fully qualified directory,
666 # prepend it with the value of packagedir.
668 if ! fnmatch "${packagedir}*" "$expunge"
669 then
670 expunge="${packagedir}/$expunge"
673 # Complain if the package directory does not exist
675 test -d "$rootdir${expunge}" || {
676 warn "Package directory '$rootdir${expunge}' does not exist."
677 return 4
680 # Complain if the package directory cannot be well-read
682 is_readable "$rootdir${expunge}" || exit 4
684 # Remove package from Graft control
686 # Scan for possible conflicts, stop if arise
687 if test "$opt_prune" != opt_prune
688 then
689 echo "Checking for possible conflicts ..."
690 if graft -d -n $graft_r -t "$targetdir" "$expunge" 2>&1 | \
691 grep ^CONFLICT
692 then
693 warn "" \
694 " A conflict occurred during uninstallation;" \
695 "Unless the -p option is given, this package will be PRESERVED."
696 return 6;
700 # Ignore some signals up to completing the deinstallation
701 trap "" HUP INT QUIT ABRT TERM
703 # Remove objects (files, links or directories) from the target
704 # directory that are in conflict with the package directory
706 echo "Pruning any conflict ..."
707 graft -p -D -u $graft_r -t "$targetdir" "$expunge"
708 chkstatus_or_exit 2
710 echo "Disabling links ..."
711 graft -d -D -u $graft_v $graft_r -t "$targetdir" "$expunge"
712 chkstatus_or_exit 2
714 # Delete package directory
715 if test "$opt_keep" != opt_keep
716 then
717 echo "Deleting package directory, if present ..."
719 if test -d "${rootdir}$expunge"
720 then
721 rm -rf -- "${rootdir}$expunge" || chkstatus_or_exit
722 echo "removed directory: '${rootdir}$expunge'"
726 # Reset given signals
727 trap - HUP INT QUIT ABRT TERM
729 # Remove used variables
730 unset expunge
733 install_mode()
735 # Complain if the package cannot be well-read
737 is_readable "$1" || exit 4
739 # Complain if the package does not end in .tlz
741 if ! fnmatch '*.tlz' "$1"
742 then
743 warn "\`${1}' does not end in .tlz"
744 return 4
747 # Make preparations to install the package
749 echo ">>> Installing package $1 ..."
751 # Get the filename
752 name=$(basename -- "$1" .tlz)
754 echo "Checking tarball integrity ..."
755 tar -tf "$1" > /dev/null
756 chkstatus_or_exit 3
758 # Create package directory using 'name'
759 if ! test -d "$rootdir${packagedir}/$name"
760 then
761 mkdir -p -- "$rootdir${packagedir}/$name" || chkstatus_or_exit
762 echo "mkdir: created directory '$rootdir${packagedir}/$name'"
765 # Scan for possible conflicts, stop if arise
766 if test "$opt_prune" != opt_prune
767 then
768 echo "Checking for possible conflicts ..."
769 if graft -i -n $graft_r -t "$targetdir" "${packagedir}/$name" 2>&1 | \
770 grep ^CONFLICT
771 then
772 warn "" \
773 " A conflict occurred during installation;" \
774 "Unless the -p option is given, this package won't be LINKED."
775 return 6;
779 # Ignore some signals up to completing the installation
780 trap "" HUP INT QUIT ABRT TERM
782 echo "Decompressing $1 ..."
783 ( cd -- "$rootdir${packagedir}/$name" && lzip -cd - | tar xpf - ) < "$1"
784 chkstatus_or_exit 3
786 # Transite package to Graft control
788 # Remove objects (files, links or directories) from the target
789 # directory that are in conflict with the package directory
790 echo "Pruning any conflict ..."
791 graft -p -D -u $graft_r -t "$targetdir" "${packagedir}/$name"
792 chkstatus_or_exit 2
794 echo "Enabling symbolic links ..."
795 graft -i -P $graft_v $graft_r -t "$targetdir" "${packagedir}/$name"
796 chkstatus_or_exit 2
798 # Avoid unnecessary runs coming from the upgrade_mode(),
799 # this is when the incoming package is **pre-installed**
801 if test "$_isUpgrade" != _isUpgrade.on
802 then
803 # Show package description
804 if test -r "$rootdir${packagedir}/${name}/var/lib/qi/${name}.txt"
805 then
806 grep '^#' "$rootdir${packagedir}/${name}/var/lib/qi/${name}.txt"
807 elif test -r "${1}.txt"
808 then
809 # From external meta file (current directory)
810 grep '^#' "${1}.txt"
811 else
812 warn "Description file not found for '$name'."
815 # Check and run the post-install script if exist
816 if test -r "$rootdir${packagedir}/${name}/var/lib/qi/${name}.sh"
817 then
818 echo "Running post-install script for \`${name}' ..."
820 # Rely on 'targetdir' if 'rootdir' is empty
821 cd -- "${rootdir:-$targetdir}"/ && \
822 . "$rootdir${packagedir}/${name}/var/lib/qi/${name}.sh"
826 # Check if there are declared packages for replacement
827 if test -r "$rootdir${packagedir}/${name}/var/lib/qi/${name}.replace"
828 then
829 while read -r line
831 for replace in "$rootdir${packagedir}/$(pkgbase $line)"-*
833 if ! test -e "$replace"
834 then
835 warn "${replace}: Declared package does not exist. (ignored)"
836 continue;
839 replace="${replace##*/}"
841 # The search for the package to be replaced cannot
842 # be the same to the incoming package, even to the
843 # temporary location coming from the upgrade_mode()
844 if test "$replace" = "$name" || test "$replace" = "${PRVLOC##*/}"
845 then
846 continue;
849 warn "WARNING: Replacing package \`${replace}' ..."
851 # Since the links belongs to the new package, only
852 # those which are not in conflict can be deleted.
853 # To complete, we will remove the package directory
855 graft -d -D -u $graft_r \
856 -t "$targetdir" "$replace" > /dev/null 2>&1
858 rm -rf -- "$rootdir${packagedir}/$replace"
859 done
860 done < "$rootdir${packagedir}/${name}/var/lib/qi/${name}.replace"
861 unset line
865 # Reset given signals
866 trap - HUP INT QUIT ABRT TERM
868 # Remove used variables
869 unset name
872 resolve_mode() {
873 # Complain if the file cannot be well-read
875 is_readable "$1" || exit 4
877 # Complain if the file does not end in .order
879 if ! fnmatch '*.order' "$1"
880 then
881 warn "\`${1}' does not end in .order"
882 return 4
885 # Get a clean list of the file while prints its content in reverse order,
886 # lines containing: colons, comments, parentheses, end of line, and blank
887 # lines, are removed. The parentheses are used to insert a reference.
888 # The last `awk' in the pipe: removes nonconsecutive lines, duplicate.
889 awk \
890 '{ gsub( /:|^#(.*)$|\([^)]*)|^$/,"" ); for( i=NF; i > 0; i-- ) print $i }' \
891 "$1" | awk '!s[$0]++'
894 upgrade_mode()
896 # Complain if the package is not a regular file
898 test -f "$1" || {
899 warn "\`${1}' is not a regular file."
900 return 4
903 # Get the filename
904 incoming=$(basename -- "$1" .tlz)
906 # Check for packages declared in the black list only for installation
908 for item in $blacklist
910 case $item in
911 ${incoming}*)
912 warn \
913 "*" \
914 "* The incoming package will be installed instead of being updated." \
915 "* This may be crucial for the correct functioning of \`${PROGRAM}'." \
917 opt_prune=opt_prune install_mode "$1"
918 return 0;;
919 esac
920 done
921 unset item
923 # Prepare the package to install it in a temporary location
925 # Set random directory using packagedir as prefix and 'incoming' as suffix
926 PRVLOC=$(mktemp -dp "$rootdir${packagedir}" ${incoming}.XXXXXXXXXXXX) || exit 2
928 # Pre-install the package in the custom 'packagedir'
930 save_packagedir="$packagedir"
931 packagedir="$PRVLOC"
933 echo "Pre-installing package using temporary location ..."
934 opt_prune=opt_prune # Turn on prune operation.
935 _isUpgrade=_isUpgrade.on install_mode "$1" > /dev/null
936 _isUpgrade=_isUpgrade.off
938 # Restore variable before looking for old packages
939 packagedir=$save_packagedir
940 unset save_packagedir
942 echo "Looking for installations under the same name ..."
943 for long_name in "$rootdir${packagedir}/$(pkgbase $incoming)"*
945 found="${long_name##*/}"
947 # The search for the package to be deleted
948 # cannot be the same to the temporary location
949 test "$long_name" = "$PRVLOC" && continue;
951 fnmatch "$(pkgbase $found)*" "$incoming" || continue;
952 echo "${long_name}: Detected."
954 # A package directory is preserved if -k is given
955 delete_mode "$found" > /dev/null
956 done
957 unset long_name found
959 # Re-install the package removing the temporary location
961 install_mode "$1"
962 opt_prune=opt_prune.off # Turn off prune operation.
964 echo "Deleting temporary location ..."
965 rm -rf -- "$PRVLOC" || chkstatus_or_exit
966 echo "removed directory: '$PRVLOC'"
968 echo ""
969 echo "Successful upgrade to '${incoming}'."
971 # Remove remaining variables
972 unset incoming PRVLOC
975 extract_mode()
977 # Perform sanity checks before package extraction
979 is_readable "$1" || exit 4
981 test -f "$1" || {
982 warn "\`${1}' is not a regular file."
983 return 4
986 # Preparations to extract the package
988 name=$(basename -- "$1" .tlz)
990 # Set random directory using 'name' as prefix
991 PRVDIR="${TMPDIR}/${name}.${RANDOM-0}$$"
993 # Trap to remove 'PRVDIR' on disruptions
994 trap "rm -rf -- $PRVDIR" HUP INT ABRT TERM
996 # Create 'PRVDIR' removing access for all but user
997 ( umask 077 ; mkdir -- $PRVDIR )
998 mkdir -p -m 700 -- $PRVDIR
1000 echo "Extracting package $name ..."
1001 ( umask 000 ; cd -- $PRVDIR && lzip -cd - | tar xpvf - ) < "$1"
1002 if test $? -ne 0
1003 then
1004 # Try to remove (empty) 'PRVDIR' on failure
1005 rmdir -- $PRVDIR
1006 exit 3;
1008 echo "$name has been extracted on $PRVDIR"
1010 # Reset given signals
1011 trap - HUP INT ABRT TERM
1013 # Remove used variables
1014 unset name PRVDIR
1017 #### Extra functions used in the modes
1019 pkgbase()
1021 string=$(basename -- "$1" .tlz)
1023 # Match cases to print the package name.
1025 # We will take into account the four segments removing
1026 # the last two to print the package (long) name
1027 case $string in
1028 *-*-*+*)
1029 echo "${string%-*-*}"
1032 echo "$string"
1034 esac
1036 unset string
1039 unpack()
1041 for file in "$@"
1043 case $file in
1044 *.tar)
1045 tar tf "$file" > /dev/null && \
1046 tar xpf "$file"
1047 chkstatus_or_exit 3
1049 *.tar.gz | *.tgz | *.tar.Z )
1050 gzip -cd "$file" | tar tf - > /dev/null && \
1051 gzip -cd "$file" | tar xpf -
1052 chkstatus_or_exit 3
1054 *.tar.bz2 | *.tbz2 | *.tbz )
1055 bzip2 -cd "$file" | tar tf - > /dev/null && \
1056 bzip2 -cd "$file" | tar xpf -
1057 chkstatus_or_exit 3
1059 *.tar.lz | *.tlz )
1060 lzip -cd "$file" | tar tf - > /dev/null && \
1061 lzip -cd "$file" | tar xpf -
1062 chkstatus_or_exit 3
1064 *.tar.xz | *.txz )
1065 xz -cd "$file" | tar tf - > /dev/null && \
1066 xz -cd "$file" | tar xpf -
1067 chkstatus_or_exit 3
1069 *.zip | *.ZIP )
1070 unzip -t "$file" > /dev/null && \
1071 unzip "$file" > /dev/null
1072 chkstatus_or_exit 3
1074 *.gz)
1075 gzip -t "$file" && \
1076 gzip -cd "$file" > "$(basename -- $file .gz)"
1077 chkstatus_or_exit 3
1079 *.Z)
1080 gzip -t "$file" && \
1081 gzip -cd "$file" > "$(basename -- $file .Z)"
1082 chkstatus_or_exit 3
1084 *.bz2)
1085 bzip2 -t "$file" && \
1086 bzip2 -cd "$file" > "$(basename -- $file .bz2)"
1087 chkstatus_or_exit 3
1089 *.lz)
1090 lzip -t "$file" && \
1091 lzip -cd "$file" > "$(basename -- $file .lz)"
1092 chkstatus_or_exit 3
1094 *.xz)
1095 xz -t "$file" && \
1096 xz -cd "$file" > "$(basename -- $file .xz)"
1097 chkstatus_or_exit 3
1100 warn "${PROGRAM}: cannot unpack ${file}: Unsupported extension"
1101 exit 1
1102 esac
1103 done
1104 unset file
1107 do_meta()
1109 # Extract information from the recipe to create the meta file.
1111 # The package description is pre-formatted in 78 columns,
1112 # the '#' character and a space is added as prefix to conform
1113 # 80 columns in total
1115 cat << EOF
1116 $(echo "$description" | fold -w 78 | awk '$0="# " $0')
1118 QICFLAGS="$QICFLAGS"
1119 QICXXFLAGS="$QICXXFLAGS"
1120 QILDFLAGS="$QILDFLAGS"
1121 program=$program
1122 version=$version
1123 release=$release
1124 blurb="$(echo "$description" | sed -e '/^$/d;2q')"
1125 homepage="$homepage"
1126 license="$license"
1127 fetch="$fetch"
1128 replace="$replace"
1133 #### Default values
1135 PROGRAM="${0##*/}"
1136 packagedir=@PACKAGEDIR@
1137 targetdir=@TARGETDIR@
1138 blacklist="perl graft musl glibc"
1139 RC=RC
1140 RCFILE=@SYSCONFDIR@/qirc
1141 opt_install=opt_install.off
1142 opt_update=opt_update.off
1143 opt_force=opt_force.off
1144 opt_keep=opt_keep.off
1145 opt_incr_release=opt_incr_release.off
1146 opt_ignoreqsts=opt_ignoreqsts.off
1147 opt_nopkg=opt_nopkg.off
1148 opt_prune=opt_prune.off
1149 rootdir=""
1150 jobs=1
1151 mode=""
1152 verbose=0
1153 graft_v=-v
1154 graft_r=""
1155 _isUpgrade=_isUpgrade.off
1156 TMPDIR="${TMPDIR:=/usr/src/qi/build}"
1157 QICFLAGS="${QICFLAGS:=-g0 -Os}"
1158 QICXXFLAGS="${QICXXFLAGS:=$QICFLAGS}"
1159 QILDFLAGS="${QILDFLAGS:=-s}"
1160 worktree=/usr/src/qi
1161 tardir=${worktree}/sources
1162 outdir=/var/cache/qi/packages
1163 netget="wget -c -w1 -t3 --no-check-certificate"
1164 rsync="rsync -v -a -L -z -i --progress"
1165 configure_args="--prefix=@PREFIX@ --libexecdir=@LIBEXECDIR@ --bindir=@BINDIR@ --sbindir=@SBINDIR@ --sysconfdir=@SYSCONFDIR@ --localstatedir=@LOCALSTATEDIR@"
1166 infodir=@INFODIR@
1167 mandir=@MANDIR@
1168 docdir=@DOCDIR@
1170 # Store default locations
1171 QI_TARGETDIR=$targetdir
1172 QI_PACKAGEDIR=$packagedir
1173 QI_WORKTREE=$worktree
1174 QI_TARDIR=$tardir
1175 QI_OUTDIR=$outdir
1177 #### Parse options
1179 while getopts :bcdiouxLNP:t:fkvO:W:Z:a:j:13npr:hV name
1181 case $name in
1183 if test -z "$mode"
1184 then
1185 readconfig
1186 mode=build_mode
1190 mode=create_mode
1193 readconfig
1194 mode=delete_mode
1197 if test -z "$mode"
1198 then
1199 readconfig
1200 mode=install_mode
1202 if test "$mode" = build_mode
1203 then
1204 opt_install=opt_install
1208 mode=resolve_mode
1211 if test -z "$mode"
1212 then
1213 readconfig
1214 mode=upgrade_mode
1216 if test "$mode" = build_mode
1217 then
1218 opt_update=opt_update
1222 mode=extract_mode
1225 printf "%s\n" \
1226 "QI_TARGETDIR=$QI_TARGETDIR" \
1227 "QI_PACKAGEDIR=$QI_PACKAGEDIR" \
1228 "QI_WORKTREE=$QI_WORKTREE" \
1229 "QI_TARDIR=$QI_TARDIR" \
1230 "QI_OUTDIR=$QI_OUTDIR"
1231 exit 0
1234 RC=RC.off
1237 packagedir="$OPTARG"
1240 targetdir="$OPTARG"
1243 opt_force=opt_force
1246 opt_keep=opt_keep
1249 verbose=$(( verbose + 1 ))
1252 outdir="$OPTARG"
1255 worktree="$OPTARG"
1258 tardir="$OPTARG"
1261 arch="$OPTARG"
1264 jobs="$OPTARG"
1267 opt_incr_release=opt_incr_release
1270 opt_ignoreqsts=opt_ignoreqsts
1273 opt_nopkg=opt_nopkg
1276 opt_prune=opt_prune
1279 rootdir="$OPTARG"
1282 usage
1283 exit 0
1286 version
1287 exit 0
1290 warn "Option '-${OPTARG}' requires an argument"
1291 usage
1292 exit 1
1295 warn "Illegal option -- '-${OPTARG}'"
1296 usage
1297 exit 1
1299 esac
1300 done
1301 shift $(( OPTIND - 1 ))
1303 if test $# -eq 0
1304 then
1305 usage
1306 exit 1
1309 # Program sanity check
1310 for need in awk basename bzip2 chmod cp dirname find fold graft grep \
1311 lzip mkdir mktemp rm rmdir sed sha256sum stat tar
1313 if ! type $need 1> /dev/null 2> /dev/null
1314 then
1315 warn "${PROGRAM}: cannot operate without ${need}(1): Check your PATH"
1316 exit 2
1318 done
1319 unset need
1321 # Determine verbosity level/flag
1323 if test "$verbose" -gt 1
1324 then
1325 graft_v=-V
1328 # Read standard input if FILE is -, or when FILE
1329 # is not connected to a terminal.
1331 if test "$1" = - || test ! -t 0
1332 then
1333 # Unset positional parameters setting $# to zero
1334 set --
1336 # Assign remaining arguments to the positional parameters
1337 while read -r input
1339 set -- "$@" "$input"
1340 done
1343 # We need at least one operating mode
1344 if test -z "$mode"
1345 then
1346 usage
1347 exit 4
1350 umask 022; # Remove write permission for group and other.
1352 # Validate 'rootdir' directory
1354 if test -n "$rootdir"
1355 then
1356 if test -d "$rootdir" && test "$rootdir" != /
1357 then
1358 # Remove slash from the end
1359 rootdir="${rootdir%/}"
1361 # A workaround for graft-2.13+. The specified directory is
1362 # relative to the log file, we prepend it inside the rootdir
1364 eval "$(graft -L)" ; GRAFT_LOGFILE="${GRAFT_LOGFILE:=/var/log/graft}"
1365 mkdir -p -- "$rootdir$(dirname -- $GRAFT_LOGFILE)" || chkstatus_or_exit
1367 # Compose 'rootdir' and log file option to be used with graft(1)
1368 graft_r="-r $rootdir -l $GRAFT_LOGFILE"
1370 # Unset variables coming from eval
1371 unset GRAFT_PERL GRAFT_LOGFILE GRAFT_TARGETDIR GRAFT_PACKAGEDIR
1372 else
1373 warn "${PROGRAM}: \`${rootdir}' is not a qualified root directory"
1374 exit 4
1376 export rootdir
1379 # Ensure 'TMPDIR' creation to prefix temporary files
1381 if test ! -d "$TMPDIR"
1382 then
1383 mkdir -p -- "$TMPDIR" || chkstatus_or_exit
1385 readonly TMPDIR
1387 # Process each package or recipe provided on the command-line
1389 for package in "$@"
1391 $mode $package
1392 done