tazpkg view modules/getenv @ rev 955

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