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