]> CyberLeo.Net >> Repos - CDN/Mosi.git/blob - script/genuzip
script/gentree: sensible default for root source tree, given base
[CDN/Mosi.git] / script / genuzip
1 #!/bin/sh
2
3 # Boilerplate
4 _root="$(dirname "${0}")"; . "${_root}/lib/env.sh"
5
6 # Load needed modules
7 want ansi log progress
8 printf "${a_normal}"
9
10 src="${1:-root.iso}"
11 out="${2:-root.img}"
12
13 rm -f "${out}"
14
15 [ -r "${src}" ] || err "${src}: no such file or directory"
16 [ ! -f "${out}" ] || err "${out}: file exists"
17
18 command="/usr/bin/mkuzip -s 65536 -v -o '${out}' '${src}' 2>&1"
19
20 time_start="$(date +%s)"
21
22 eval ${command} | sed -l -e '/^compressed /s/^.*$/$finished=yes/' -e '/^cluster /s/^cluster #\([0-9]*\), in \([0-9]*\) bytes, out \([0-9]*\) bytes$/$clust=\1 in=\2 out=\3/' -e '/^data /s/^.* number of clusters \([0-9]*\), .*$/$count=\1/' -e '/^[^$]/d; s/^\$//' | while read line
23 do
24   # initialization
25   clust="${clust:-0}"
26   count="${count:-0}"
27   all_in="${all_in:-0}"
28   all_out="${all_out:-0}"
29   in=0
30   out=0
31
32   # Read in values
33   eval ${line}
34
35   # Compute aggregates
36   iter="$(( ${iter:-0} + 1 ))"
37   all_in="$(( ${all_in} + ${in} ))"
38   all_out="$(( ${all_out} + ${out} ))"
39   
40   if [ -n "${finished}" -o "$(( ${iter} % ${limit_break:-16} ))" -eq 0 ] && \
41     [ -n "${finished}" -o "$(date +%s)" != "${time_last}" ]
42   then
43     time="$(date +%s)"
44     
45     clust_diff="$(( ${clust:-0} - ${clust_last:-0} ))"
46     time_diff="$(( ${time} - ${time_last:-0} ))"
47     
48     # Adjust last iteration length as a rolling average of half the last per-second cluster count
49     [ -z "${finished}" ] && limit_break="$(( ( ${limit_break:-16} + ( ( ${clust_diff} / ${time_diff} ) / 2 ) ) / 2 ))"
50     [ "${limit_break}" -eq 0 ] && limit_break=1 # Avoid divide-by-zero errors
51
52     clust_last="${clust:-0}"
53     time_last="${time}"
54
55     [ -n "${finished}" ] && clust="${count}"
56
57     # Compute current ratio
58     ratio="$(printf "%u / %u\n" "${all_out:-0}" "${all_in:-0}" | bc -l 2>/dev/null)"
59     
60     printf "\r %s %0.4f \033[K" "$(progress "${clust}" "${count}")" "${ratio:-0}"
61
62     [ -n "${finished}" ] && printf "\n"
63   fi
64 done