[doc_tool] Show leading zeros in percentage of install script.
[hsd_doku_tool.git] / scripts / gen_package_linux.sh
1 #!/bin/sh
2 #Script that creates self extracting executable script from tar.gz file. 
3 if [ $# -eq 0 ] || [ ! -f $1 ]; then
4         echo "This script creates self extractable executable"
5         echo Usage: $0 [/path/to/*.tar.gz]
6         exit;
7 fi
8 if [ $# -gt 0 ]; then
9         TAR_FILE=$1
10 fi
11 EXIT_COMMAND=exit
12 if [ $# -gt 1 ]; then
13         EXIT_COMMAND="exec $2"
14 fi
15 FILECOUNT=$(tar -tzf $TAR_FILE | wc -l)
16 SELF_EXTRACTABLE="$TAR_FILE.run"
17
18 cat > ${SELF_EXTRACTABLE} << EOL
19 #!/bin/bash
20 if [ -f '/usr/bin/xmessage' ] || [ -f '/usr/bin/gxmessage' ]; then
21     XMESSAGE=\$(which gxmessage) || XMESSAGE=xmessage
22     PWD=\$(pwd)
23     ans=\$(\$XMESSAGE -nearmouse -title 'Extract?' -buttons yes,no Extract to: [ \$PWD ] ? -print)
24 else
25     ans=yes
26 fi
27 if [ "\$ans" == "yes" ]; then
28     TOSKIP=\$(awk '/^#----- ARCHIVE STARTS HERE -----#/ {print NR + 1; exit 0; }' \$0)
29     COUNT=$FILECOUNT
30     echo "Installing Doc Tool: "& 
31     PID=\$!
32     LINE="                                                  "
33     LINENUM=0
34
35     # Extract
36     tail -n+\$TOSKIP \$0  | tar -xzvp 2>&1 | 
37     while read line; do
38         x=\$((x+1))
39
40         # calculate percentage
41         PERCENTAGE_NUM=\$(((x*100)/COUNT))
42         PERCENTAGE=\$(printf '%03d' \$PERCENTAGE_NUM)   
43
44         # create gauge line
45         if [ "\$PERCENTAGE_NUM" -ge "\$LINENUM" ]; then
46                 LINE=\${LINE/ />}
47
48                 if [ "\$PERCENTAGE_NUM" -ge "100" ]; then
49                         LINE=\${LINE// />}
50                 fi
51         fi
52
53         # display gauge line
54         if [ -z \${XMESSAGE+x} ]; then
55               echo -en "[\$LINE] \$x / \$COUNT extracted (\$PERCENTAGE%)\r"
56                 if [ "\$PERCENTAGE_NUM" -ge "\$LINENUM" ]; then
57                       LINENUM=\$((PERCENTAGE_NUM+2))
58                 fi
59         else
60                if [ "\$PERCENTAGE_NUM" -ge "\$LINENUM" ]; then
61                       LINENUM=\$((PERCENTAGE_NUM+2))
62                       if ps -p \$PID > /dev/null 
63                       then
64                             kill \$PID 2> /dev/null 
65                        fi
66                       \$XMESSAGE -center -title 'Progress' "[\$LINE] \$x / \$COUNT extracted! (\$PERCENTAGE%)" &
67                        PID=\$!
68                fi
69         fi
70     done
71
72     # check if untar was successful
73     if [ "\${PIPESTATUS[1]}" != "0" ]; then
74         if [ -z \${XMESSAGE+x} ]; then
75             echo 'Failed to extract package!'
76         else
77              \$XMESSAGE -nearmouse -title 'Error!' 'Failed to extract package!'
78         fi
79         exit 1
80     fi
81
82     # display success message
83     if [ -z \${XMESSAGE+x} ]; then
84         echo ''
85         echo 'Finished extracting!'
86     else
87         \$XMESSAGE -nearmouse -title 'Success' 'Finished extracting!'
88     fi
89    exit
90 else
91    exit
92 fi
93 #----- ARCHIVE STARTS HERE -----#
94 EOL
95
96 cat $TAR_FILE >> $SELF_EXTRACTABLE
97 chmod a+x $SELF_EXTRACTABLE