tazpkg annotate modules/getenv @ rev 844

Finish modularization. Beta release: still have few FIXMEs and TODOs.
author Aleksej Bobylev <al.bobylev@gmail.com>
date Mon Oct 05 03:53:47 2015 +0300 (2015-10-05)
parents a02e36d44d06
children 8a73a58ed3cb
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@844 7 # Set up the aliases to guaranteed to work with Busybox applets rather with the GNU Coreutils ones
al@844 8 # due to some incompatibilities.
al@844 9 # Please don't hesitate to expand or shrink this list (with justification).
al@844 10 for i in awk basename bzcat cat chmod chroot clear cmp cp cpio cut date dd diff dirname dpkg-deb \
al@844 11 du egrep fgrep find grep gzip head httpd id ln ls lzcat md5sum mkdir mktemp mv readlink \
al@844 12 realpath rm rmdir rpm rpm2cpio sed sort stat su tac tail tar tee touch tr tty uniq unlzma wc \
al@844 13 wget which xzcat zcat; do
al@844 14 alias $i="busybox $i"
al@844 15 done
al@844 16
al@844 17
al@844 18 . /lib/libtaz.sh
al@844 19
al@844 20 # Report error and finish work
al@844 21 die() { longline "$(_ "$@")" >&2; exit 1; }
al@844 22
al@844 23 # Show debug messages
al@844 24 debug() { if [ -n "$debug" ]; then colorize 036 "$@" >&2; fi; }
al@844 25
al@844 26 debug "\n========\n$0 '$1' '$2' '$3' '$4'"
al@844 27
al@840 28 # Check and re-create files and folders (if permissions enough)
al@844 29 missing_file() {
al@844 30 if [ ! -f "$1" ]; then
al@844 31 case $(id -u) in
al@844 32 0) mkdir -p "$(dirname "$1")"; touch "$1"
al@844 33 [ -n "$2" ] && cp -a "$2" "$(dirname "$1")"
al@844 34 ;;
al@844 35 *) _ 'Missing: %s' "$1" >&2; die 'Please run tazpkg as root.';;
al@844 36 esac
al@844 37 fi
al@844 38 }
al@844 39 missing_dir() {
al@844 40 if [ ! -d "$1" ]; then
al@844 41 case $(id -u) in
al@844 42 0) mkdir -p "$1";;
al@844 43 *) _ 'Missing: %s' "$1" >&2; die 'Please run tazpkg as root.';;
al@844 44 esac
al@844 45 fi
al@840 46 }
al@840 47
al@840 48 # Fill empty file with value
al@840 49 fill() {
al@840 50 if [ ! -s "$1" ]; then
al@840 51 case $(id -u) in
al@840 52 0) echo "$2" > "$1";;
al@844 53 *) _ 'File "%s" empty.' "$1" >&2; die 'Please run tazpkg as root.';;
al@840 54 esac
al@840 55 fi
al@840 56 }
al@840 57
al@840 58
al@840 59
al@840 60
al@844 61 # Normalize $root
al@844 62 root="${root%/}"
al@844 63 debug "root = '$root'"
al@844 64
al@840 65 # Setup main config files
al@844 66 missing_dir "$root/etc/slitaz/"
al@844 67 missing_file "$root/etc/slitaz/slitaz.conf" '/etc/slitaz/slitaz.conf'
al@844 68 missing_file "$root/etc/slitaz/tazpkg.conf" '/etc/slitaz/tazpkg.conf'
al@844 69 missing_file "$root/etc/slitaz-release"; fill "$root/etc/slitaz-release" 'cooking'
al@840 70
al@840 71 # Read configuration
al@840 72 if [ -n "$root" ]; then
al@840 73 # Patch external conf files to correctly handle --root value
al@840 74 slitaz_conf=$(mktemp); cp "$root/etc/slitaz/slitaz.conf" "$slitaz_conf"
al@840 75 tazpkg_conf=$(mktemp); cp "$root/etc/slitaz/tazpkg.conf" "$tazpkg_conf"
al@840 76 sed -i "s| /| $root/|g; s|\"/|\"$root/|g" "$slitaz_conf" "$tazpkg_conf"
al@840 77 . "$slitaz_conf"; . "$tazpkg_conf"
al@840 78 rm "$slitaz_conf" "$tazpkg_conf"
al@840 79 else
al@840 80 . /etc/slitaz/slitaz.conf; . /etc/slitaz/tazpkg.conf
al@840 81 fi
al@840 82
al@844 83 debug "PKGS_DB = '$PKGS_DB'"
al@844 84 debug "INSTALLED = '$INSTALLED'"
al@844 85 debug "SLITAZ_LOGS = '$SLITAZ_LOGS'"
al@844 86 debug "LOG = '$LOG'"
al@844 87
al@840 88 BLOCKED="$PKGS_DB/blocked-packages.list"
al@844 89 debug "BLOCKED = '$BLOCKED'"
al@840 90 UP_LIST="$PKGS_DB/packages.up"
al@844 91 debug "UP_LIST = '$UP_LIST'"
al@844 92 debug "CACHE_DIR = '$CACHE_DIR'"
al@844 93 SAVE_CACHE_DIR="$CACHE_DIR"
al@844 94
al@840 95
al@840 96 # Re-create TazPkg working folders and files
al@840 97 for dir in "$PKGS_DB" "$CACHE_DIR" "$INSTALLED" "$SLITAZ_LOGS"; do
al@844 98 missing_dir "$dir"
al@840 99 done
al@840 100 for file in "$BLOCKED" "$UP_LIST" "$LOG" "$PKGS_DB/packages.info" "$PKGS_DB/mirror"; do
al@844 101 missing_file "$file"
al@840 102 done
al@840 103 fill "$PKGS_DB/mirror" "${ONLINE_PKGS%/}/"
al@840 104
al@844 105
al@840 106 # Check for installed.info
al@840 107 info_path="$PKGS_DB/installed.info"
al@844 108 missing_file "$info_path"
al@840 109 if [ ! -s "$info_path" ]; then
al@840 110 # Empty installed.info
al@844 111 if [ -n "$(ls "$INSTALLED")" ]; then
al@844 112 # Some packages are installed
al@844 113 if [ "$(id -u)" -eq 0 ]; then
al@844 114 # Root can re-create installed.info
al@844 115 _ 'File "%s" generated. Please wait...' 'installed.info' >&2
al@844 116 for pkg in $(find "$INSTALLED" -name receipt); do
al@844 117 unset PACKAGE VERSION EXTRAVERSION CATEGORY SHORT_DESC WEB_SITE \
al@844 118 TAGS PACKED_SIZE UNPACKED_SIZE DEPENDS
al@844 119 . $pkg
al@844 120 SIZES=$(echo $PACKED_SIZE $UNPACKED_SIZE | sed 's|\.0||g')
al@844 121 # remove newlines from some receipts
al@844 122 DEPENDS=$(echo $DEPENDS)
al@844 123 MD5="$(fgrep " $PACKAGE-$VERSION$EXTRAVERSION.tazpkg" "$PKGS_DB/installed.md5" | awk '{print $1}')"
al@844 124 cat >> "$info_path" << EOT
al@840 125 $PACKAGE $VERSION$EXTRAVERSION $CATEGORY $SHORT_DESC $WEB_SITE $TAGS $SIZES $DEPENDS $MD5
al@840 126 EOT
al@844 127 done
al@844 128 else
al@844 129 # User can't re-create installed.info
al@844 130 fill "$info_path"
al@844 131 fi
al@840 132 fi
al@840 133 else
al@840 134 # Non-empty installed.info
al@840 135
al@840 136 # Check for md5 field (#9) in the installed.info: older version missed it
al@840 137 if [ -n "$(awk -F$'\t' 'BEGIN{ n = "" } { if(NF != 9){ n = "o"; } } END{ print n }' $info_path)" ]; then
al@840 138 if [ "$(id -u)" -eq 0 ]; then
al@840 139 # Root can re-create it
al@844 140 _n 'File "%s" generated. Please wait...' 'installed.info.new' >&2
al@840 141 awk -F$'\t' -vm="$PKGS_DB/installed.md5" 'BEGIN{OFS="\t"}
al@840 142 {
al@840 143 if (NF != 9) {
al@840 144 pkg = $1 "-" $2 ".tazpkg";
al@840 145 "fgrep " pkg " " m " | cut -c-32" | getline $9;
al@840 146 $9 = ($9 == "") ? "00000000000000000000000000000000" : $9;
al@840 147 }
al@840 148 print;
al@840 149 }' $info_path > $info_path.new
al@840 150 mv -f $info_path.new $info_path
al@840 151 status
al@840 152 else
al@840 153 # User can't re-create it
al@844 154 _ 'Old "%s".' 'installed.info' >&2; die 'Please run tazpkg as root.'
al@840 155 fi
al@840 156 fi
al@840 157 fi
al@840 158
al@840 159
al@844 160 # Check for packages.info
al@844 161 if [ ! -s "$PKGS_DB/packages.info" -a "$(id -u)" -eq 0 -a "$0" != '@@MODULES@@/recharge' ]; then
al@844 162 @@MODULES@@/recharge >&2
al@840 163 fi
al@844 164
al@844 165
al@844 166
al@844 167 # Get repositories priority using $PKGS_DB/priority.
al@844 168 # In this file undigest repos are called by their names and main mirror by 'main'
al@844 169
al@844 170 PRIORITY="$(
al@844 171 {
al@844 172 [ -s "$PKGS_DB/priority" ] && cat "$PKGS_DB/priority"
al@844 173 echo 'main'
al@844 174 [ -d "$PKGS_DB/undigest" ] && ls "$PKGS_DB/undigest"
al@844 175 } | awk -vv="$PKGS_DB/undigest/" '{
al@844 176 if(arr[$0] != 1) { arr[$0]=1; print v $0; }
al@844 177 }' | sed 's|/undigest/main||')"
al@844 178 debug "PRIORITY = '$PRIORITY'"
al@844 179
al@844 180
al@844 181 # TazPkg version
al@844 182 export VERSION=$(awk -F$'\t' '$1=="tazpkg"{print $2;exit}' "$PKGS_DB/installed.info")
al@844 183 # User Agent
al@844 184 export UA="TazPkg-${VERSION:-Unknown}"
al@844 185 debug "UA = '$UA'"
al@844 186
al@844 187 CUR_DIR="$(pwd)"
al@844 188
al@844 189 debug '-- end getenv --'