tazpkg view 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
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 to 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 # Report error and finish work
21 die() { longline "$(_ "$@")" >&2; exit 1; }
23 # Show debug messages
24 debug() { if [ -n "$debug" ]; then colorize 036 "$@" >&2; fi; }
26 debug "\n========\n$0 '$1' '$2' '$3' '$4'"
28 # Check and re-create files and folders (if permissions enough)
29 missing_file() {
30 if [ ! -f "$1" ]; then
31 case $(id -u) in
32 0) mkdir -p "$(dirname "$1")"; touch "$1"
33 [ -n "$2" ] && cp -a "$2" "$(dirname "$1")"
34 ;;
35 *) _ 'Missing: %s' "$1" >&2; die 'Please run tazpkg as root.';;
36 esac
37 fi
38 }
39 missing_dir() {
40 if [ ! -d "$1" ]; then
41 case $(id -u) in
42 0) mkdir -p "$1";;
43 *) _ 'Missing: %s' "$1" >&2; die 'Please run tazpkg as root.';;
44 esac
45 fi
46 }
48 # Fill empty file with value
49 fill() {
50 if [ ! -s "$1" ]; then
51 case $(id -u) in
52 0) echo "$2" > "$1";;
53 *) _ 'File "%s" empty.' "$1" >&2; die 'Please run tazpkg as root.';;
54 esac
55 fi
56 }
61 # Normalize $root
62 root="${root%/}"
63 debug "root = '$root'"
65 # Setup main config files
66 missing_dir "$root/etc/slitaz/"
67 missing_file "$root/etc/slitaz/slitaz.conf" '/etc/slitaz/slitaz.conf'
68 missing_file "$root/etc/slitaz/tazpkg.conf" '/etc/slitaz/tazpkg.conf'
69 missing_file "$root/etc/slitaz-release"; fill "$root/etc/slitaz-release" 'cooking'
71 # Read configuration
72 if [ -n "$root" ]; then
73 # Patch external conf files to correctly handle --root value
74 slitaz_conf=$(mktemp); cp "$root/etc/slitaz/slitaz.conf" "$slitaz_conf"
75 tazpkg_conf=$(mktemp); cp "$root/etc/slitaz/tazpkg.conf" "$tazpkg_conf"
76 sed -i "s| /| $root/|g; s|\"/|\"$root/|g" "$slitaz_conf" "$tazpkg_conf"
77 . "$slitaz_conf"; . "$tazpkg_conf"
78 rm "$slitaz_conf" "$tazpkg_conf"
79 else
80 . /etc/slitaz/slitaz.conf; . /etc/slitaz/tazpkg.conf
81 fi
83 debug "PKGS_DB = '$PKGS_DB'"
84 debug "INSTALLED = '$INSTALLED'"
85 debug "SLITAZ_LOGS = '$SLITAZ_LOGS'"
86 debug "LOG = '$LOG'"
88 BLOCKED="$PKGS_DB/blocked-packages.list"
89 debug "BLOCKED = '$BLOCKED'"
90 UP_LIST="$PKGS_DB/packages.up"
91 debug "UP_LIST = '$UP_LIST'"
92 debug "CACHE_DIR = '$CACHE_DIR'"
93 SAVE_CACHE_DIR="$CACHE_DIR"
96 # Re-create TazPkg working folders and files
97 for dir in "$PKGS_DB" "$CACHE_DIR" "$INSTALLED" "$SLITAZ_LOGS"; do
98 missing_dir "$dir"
99 done
100 for file in "$BLOCKED" "$UP_LIST" "$LOG" "$PKGS_DB/packages.info" "$PKGS_DB/mirror"; do
101 missing_file "$file"
102 done
103 fill "$PKGS_DB/mirror" "${ONLINE_PKGS%/}/"
106 # Check for installed.info
107 info_path="$PKGS_DB/installed.info"
108 missing_file "$info_path"
109 if [ ! -s "$info_path" ]; then
110 # Empty installed.info
111 if [ -n "$(ls "$INSTALLED")" ]; then
112 # Some packages are installed
113 if [ "$(id -u)" -eq 0 ]; then
114 # Root can re-create installed.info
115 _ 'File "%s" generated. Please wait...' 'installed.info' >&2
116 for pkg in $(find "$INSTALLED" -name receipt); do
117 unset PACKAGE VERSION EXTRAVERSION CATEGORY SHORT_DESC WEB_SITE \
118 TAGS PACKED_SIZE UNPACKED_SIZE DEPENDS
119 . $pkg
120 SIZES=$(echo $PACKED_SIZE $UNPACKED_SIZE | sed 's|\.0||g')
121 # remove newlines from some receipts
122 DEPENDS=$(echo $DEPENDS)
123 MD5="$(fgrep " $PACKAGE-$VERSION$EXTRAVERSION.tazpkg" "$PKGS_DB/installed.md5" | awk '{print $1}')"
124 cat >> "$info_path" << EOT
125 $PACKAGE $VERSION$EXTRAVERSION $CATEGORY $SHORT_DESC $WEB_SITE $TAGS $SIZES $DEPENDS $MD5
126 EOT
127 done
128 else
129 # User can't re-create installed.info
130 fill "$info_path"
131 fi
132 fi
133 else
134 # Non-empty installed.info
136 # Check for md5 field (#9) in the installed.info: older version missed it
137 if [ -n "$(awk -F$'\t' 'BEGIN{ n = "" } { if(NF != 9){ n = "o"; } } END{ print n }' $info_path)" ]; then
138 if [ "$(id -u)" -eq 0 ]; then
139 # Root can re-create it
140 _n 'File "%s" generated. Please wait...' 'installed.info.new' >&2
141 awk -F$'\t' -vm="$PKGS_DB/installed.md5" 'BEGIN{OFS="\t"}
142 {
143 if (NF != 9) {
144 pkg = $1 "-" $2 ".tazpkg";
145 "fgrep " pkg " " m " | cut -c-32" | getline $9;
146 $9 = ($9 == "") ? "00000000000000000000000000000000" : $9;
147 }
148 print;
149 }' $info_path > $info_path.new
150 mv -f $info_path.new $info_path
151 status
152 else
153 # User can't re-create it
154 _ 'Old "%s".' 'installed.info' >&2; die 'Please run tazpkg as root.'
155 fi
156 fi
157 fi
160 # Check for packages.info
161 if [ ! -s "$PKGS_DB/packages.info" -a "$(id -u)" -eq 0 -a "$0" != '@@MODULES@@/recharge' ]; then
162 @@MODULES@@/recharge >&2
163 fi
167 # Get repositories priority using $PKGS_DB/priority.
168 # In this file undigest repos are called by their names and main mirror by 'main'
170 PRIORITY="$(
171 {
172 [ -s "$PKGS_DB/priority" ] && cat "$PKGS_DB/priority"
173 echo 'main'
174 [ -d "$PKGS_DB/undigest" ] && ls "$PKGS_DB/undigest"
175 } | awk -vv="$PKGS_DB/undigest/" '{
176 if(arr[$0] != 1) { arr[$0]=1; print v $0; }
177 }' | sed 's|/undigest/main||')"
178 debug "PRIORITY = '$PRIORITY'"
181 # TazPkg version
182 export VERSION=$(awk -F$'\t' '$1=="tazpkg"{print $2;exit}' "$PKGS_DB/installed.info")
183 # User Agent
184 export UA="TazPkg-${VERSION:-Unknown}"
185 debug "UA = '$UA'"
187 CUR_DIR="$(pwd)"
189 debug '-- end getenv --'