Pascal Dreissen
2016-07-08 a2401ddb28154829f254378b72e081ac2d4af289
commit | author | age
1bb512 1 #!/bin/bash
W 2 # Copyright (c) 2009, Scott Barr <gsbarr@gmail.com>
3 # All rights reserved.
4 #
5 # Redistribution and use in source and binary forms, with or without
6 # modification, are permitted provided that the following conditions are met:
7 #    * Redistributions of source code must retain the above copyright
8 #      notice, this list of conditions and the following disclaimer.
9 #    * Redistributions in binary form must reproduce the above copyright
10 #      notice, this list of conditions and the following disclaimer in the
11 #      documentation and/or other materials provided with the distribution.
12 #    * Neither the name of the <organization> nor the
13 #      names of its contributors may be used to endorse or promote products
14 #      derived from this software without specific prior written permission.
15 #
16 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ''AS IS''
17 # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 # DISCLAIMED. IN NO EVENT SHALL <copyright holder> BE LIABLE FOR ANY
20 # DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 # (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23 # ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26
27 function countdown
28 {
29     local OLD_IFS="${IFS}"
30     IFS=":"
31     local ARR=( $1 ) ; shift
32     IFS="${OLD_IFS}"
33     local PREFIX="$*" ; [ -n "${PREFIX}" ] && PREFIX="${PREFIX}"
34     local SECONDS=$((  (ARR[0] * 60 * 60) + (ARR[1] * 60) + ARR[2]  ))
35     local START=$(date +%s)
36     local END=$((START + SECONDS))
37     local CUR=$START
38
39     while [[ $CUR -lt $END ]]
40     do
41             CUR=$(date +%s)
42             LEFT=$((END-CUR))
43
44             printf "\r${PREFIX} %02d" \
45                     $((LEFT%60))
46
47             sleep 1
48     done
49
50     echo "        "
51 }
52
53 function outputVar()
54 {
55   y=\$"$1"
56   x=`eval "expr \"$y\" "`
57   eval "$1=\"$2\""
58 }
59
60 function valid_input()
61 {
62   local correct="no"
63   Return_Val=""
64
65   if [ -z "$2" ]
66   then
67     local opts=( yes no )
68     local opt_list="yes/no"
69   else
70     local opts=( `echo $2 | tr "/" " "` )
71     local opt_list=$2
72   fi
73   
74   while [ "$correct" != "yes" ]
75   do
76         if [ -n "$3" ]
77         then
78             line_prompt="$1 [default: $3] "
79         else
80             line_prompt="$1 [$opt_list] "
81         fi
82         
83         echo -en "$line_prompt"
84         read answer
85         
86         if [ -n "$3" ]
87         then
88             answer=$3
89         else
90             ret=`echo "${opts[@]}" | grep -w "$answer"`
91         fi
92         
93         if [ $? -eq 0 ]
94         then
95             correct="yes"
96             Return_Val=$answer
97         fi
98   done
99 }
100
101 function start_spinner()
102 {
103   parent_pid=$$
104   (SP_STRING="/-\\|"; while [ -d /proc/$1 ] && [ -d /proc/$parent_pid ]; do printf "\e[1;37m\e7[ %1.1s ]  \e8\e[0m" "$SP_STRING"; sleep .2; SP_STRING=${SP_STRING#"${SP_STRING%?}"}${SP_STRING%?}; done) &
105   disown
106   spinner_pid=$!
107 }
108
109 function stop_spinner()
110 {
111    if [ $spinner_pid -gt 0 ]
112    then
113     kill -HUP $spinner_pid 2>/dev/null
114    fi
115 }
116
117 function exec_command()
118 {
119    printf "%-40s" "$2"
120    
121    (eval $1 >/dev/null 2>&1) &
122    pid=$!
123    
124    start_spinner
125    wait $pid
126    status=$?
127    stop_spinner
128    
129    if [ $status -eq 0 ];
130    then
131        echo -e "\e[1;37m[ \e[0m\e[1;32mok\e[0m\e[1;37m ]\e[0m"
132    else
133        echo -e "\e[1;37m[ \e[0m\e[1;31mfailed\e[0m\e[1;37m ]\e[0m"
134        echo -e "The following command did not complete successfully:"
135        echo -e "$1"
136        exit 1
137    fi
138 }