tazpkg annotate modules/getenv @ rev 840
Add a bunch of modules with new-style support of 'root' (not all commands are modules yet); strip and compress resources.
author | Aleksej Bobylev <al.bobylev@gmail.com> |
---|---|
date | Fri Aug 28 16:10:34 2015 +0300 (2015-08-28) |
parents | |
children | d6cbd0c5f273 |
rev | line source |
---|---|
al@840 | 1 #!/bin/sh |
al@840 | 2 # TazPkg - Tiny autonomous zone packages manager, hg.slitaz.org/tazpkg |
al@840 | 3 # getenv - TazPkg module |
al@840 | 4 # Get TazPkg working environment |
al@840 | 5 |
al@840 | 6 |
al@840 | 7 # Check and re-create files and folders (if permissions enough) |
al@840 | 8 missing() { |
al@840 | 9 case $1 in |
al@840 | 10 file) |
al@840 | 11 if [ ! -f "$2" ]; then |
al@840 | 12 case $(id -u) in |
al@840 | 13 0) mkdir -p "$(dirname "$2")"; touch "$2" |
al@840 | 14 [ -n "$3" ] && cp -a "$3" "$(dirname "$2")" |
al@840 | 15 ;; |
al@840 | 16 *) _ 'Missing: %s' "$2"; _ 'Please run tazpkg as root.'; exit 1;; |
al@840 | 17 esac |
al@840 | 18 fi;; |
al@840 | 19 dir) |
al@840 | 20 if [ ! -d "$2" ]; then |
al@840 | 21 case $(id -u) in |
al@840 | 22 0) mkdir -p "$2";; |
al@840 | 23 *) _ 'Missing: %s' "$2"; _ 'Please run tazpkg as root.'; exit 1;; |
al@840 | 24 esac |
al@840 | 25 fi;; |
al@840 | 26 esac |
al@840 | 27 } |
al@840 | 28 |
al@840 | 29 # Fill empty file with value |
al@840 | 30 fill() { |
al@840 | 31 if [ ! -s "$1" ]; then |
al@840 | 32 case $(id -u) in |
al@840 | 33 0) echo "$2" > "$1";; |
al@840 | 34 *) _ 'File "%s" empty.' "$1"; _ 'Please run tazpkg as root.'; exit 1;; |
al@840 | 35 esac |
al@840 | 36 fi |
al@840 | 37 } |
al@840 | 38 |
al@840 | 39 |
al@840 | 40 |
al@840 | 41 |
al@840 | 42 # Setup main config files |
al@840 | 43 missing dir "$root/etc/slitaz/" |
al@840 | 44 missing file "$root/etc/slitaz/slitaz.conf" '/etc/slitaz/slitaz.conf' |
al@840 | 45 missing file "$root/etc/slitaz/tazpkg.conf" '/etc/slitaz/tazpkg.conf' |
al@840 | 46 missing file "$root/etc/slitaz-release"; fill "$root/etc/slitaz-release" 'cooking' |
al@840 | 47 |
al@840 | 48 # Read configuration |
al@840 | 49 if [ -n "$root" ]; then |
al@840 | 50 # Patch external conf files to correctly handle --root value |
al@840 | 51 slitaz_conf=$(mktemp); cp "$root/etc/slitaz/slitaz.conf" "$slitaz_conf" |
al@840 | 52 tazpkg_conf=$(mktemp); cp "$root/etc/slitaz/tazpkg.conf" "$tazpkg_conf" |
al@840 | 53 sed -i "s| /| $root/|g; s|\"/|\"$root/|g" "$slitaz_conf" "$tazpkg_conf" |
al@840 | 54 . "$slitaz_conf"; . "$tazpkg_conf" |
al@840 | 55 rm "$slitaz_conf" "$tazpkg_conf" |
al@840 | 56 else |
al@840 | 57 . /etc/slitaz/slitaz.conf; . /etc/slitaz/tazpkg.conf |
al@840 | 58 fi |
al@840 | 59 |
al@840 | 60 BLOCKED="$PKGS_DB/blocked-packages.list" |
al@840 | 61 UP_LIST="$PKGS_DB/packages.up" |
al@840 | 62 |
al@840 | 63 # Re-create TazPkg working folders and files |
al@840 | 64 for dir in "$PKGS_DB" "$CACHE_DIR" "$INSTALLED" "$SLITAZ_LOGS"; do |
al@840 | 65 missing dir "$dir" |
al@840 | 66 done |
al@840 | 67 for file in "$BLOCKED" "$UP_LIST" "$LOG" "$PKGS_DB/packages.info" "$PKGS_DB/mirror"; do |
al@840 | 68 missing file "$file" |
al@840 | 69 done |
al@840 | 70 fill "$PKGS_DB/mirror" "${ONLINE_PKGS%/}/" |
al@840 | 71 |
al@840 | 72 # Check for installed.info |
al@840 | 73 info_path="$PKGS_DB/installed.info" |
al@840 | 74 missing file "$info_path" |
al@840 | 75 if [ ! -s "$info_path" ]; then |
al@840 | 76 # Empty installed.info |
al@840 | 77 if [ "$(id -u)" -eq 0 ]; then |
al@840 | 78 # Root can re-create installed.info |
al@840 | 79 _ 'File "%s" generated. Please wait...' 'installed.info' |
al@840 | 80 for pkg in $(find "$INSTALLED" -name receipt); do |
al@840 | 81 unset PACKAGE VERSION EXTRAVERSION CATEGORY SHORT_DESC WEB_SITE \ |
al@840 | 82 TAGS PACKED_SIZE UNPACKED_SIZE DEPENDS |
al@840 | 83 . $pkg |
al@840 | 84 SIZES=$(echo $PACKED_SIZE $UNPACKED_SIZE | sed 's|\.0||g') |
al@840 | 85 # remove newlines from some receipts |
al@840 | 86 DEPENDS=$(echo $DEPENDS) |
al@840 | 87 MD5="$(fgrep " $PACKAGE-$VERSION$EXTRAVERSION.tazpkg" "$PKGS_DB/installed.md5" | awk '{print $1}')" |
al@840 | 88 cat >> "$info_path" << EOT |
al@840 | 89 $PACKAGE $VERSION$EXTRAVERSION $CATEGORY $SHORT_DESC $WEB_SITE $TAGS $SIZES $DEPENDS $MD5 |
al@840 | 90 EOT |
al@840 | 91 done |
al@840 | 92 else |
al@840 | 93 # User can't re-create installed.info |
al@840 | 94 fill "$info_path" |
al@840 | 95 fi |
al@840 | 96 else |
al@840 | 97 # Non-empty installed.info |
al@840 | 98 |
al@840 | 99 # Check for md5 field (#9) in the installed.info: older version missed it |
al@840 | 100 if [ -n "$(awk -F$'\t' 'BEGIN{ n = "" } { if(NF != 9){ n = "o"; } } END{ print n }' $info_path)" ]; then |
al@840 | 101 if [ "$(id -u)" -eq 0 ]; then |
al@840 | 102 # Root can re-create it |
al@840 | 103 _n 'File "%s" generated. Please wait...' 'installed.info.new' |
al@840 | 104 awk -F$'\t' -vm="$PKGS_DB/installed.md5" 'BEGIN{OFS="\t"} |
al@840 | 105 { |
al@840 | 106 if (NF != 9) { |
al@840 | 107 pkg = $1 "-" $2 ".tazpkg"; |
al@840 | 108 "fgrep " pkg " " m " | cut -c-32" | getline $9; |
al@840 | 109 $9 = ($9 == "") ? "00000000000000000000000000000000" : $9; |
al@840 | 110 } |
al@840 | 111 print; |
al@840 | 112 }' $info_path > $info_path.new |
al@840 | 113 mv -f $info_path.new $info_path |
al@840 | 114 status |
al@840 | 115 else |
al@840 | 116 # User can't re-create it |
al@840 | 117 _ 'Old "%s".' 'installed.info' |
al@840 | 118 _ 'Please run tazpkg as root.' |
al@840 | 119 exit 1 |
al@840 | 120 fi |
al@840 | 121 fi |
al@840 | 122 fi |
al@840 | 123 |
al@840 | 124 |
al@840 | 125 if [ -n "$debug" ]; then |
al@840 | 126 cat <<EOT |
al@840 | 127 root = "$root" |
al@840 | 128 PKGS_DB = "$PKGS_DB" |
al@840 | 129 CACHE_DIR = "$CACHE_DIR" |
al@840 | 130 INSTALLED = "$INSTALLED" |
al@840 | 131 BLOCKED = "$BLOCKED" |
al@840 | 132 UP_LIST = "$UP_LIST" |
al@840 | 133 SLITAZ_LOGS = "$SLITAZ_LOGS" |
al@840 | 134 LOG = "$LOG" |
al@840 | 135 EOT |
al@840 | 136 fi |