]> git.sven.stormbind.net Git - sven/java-package.git/blob - lib/common.sh
Initial rework of (svn) java-package 0.42
[sven/java-package.git] / lib / common.sh
1 # read_yn <prompt>
2 function read_yn() {
3     local prompt="$1"
4     while true; do
5         read -e -n 1 -p "$prompt" reply
6         case "$reply" in
7             "" | "y" | "Y")
8                 return 0
9                 ;;
10             "N" | "n")
11                 return 1
12                 ;;
13         esac
14     done
15 }
16
17
18 # diskusage <path>: prints size in MB
19 function diskusage() {
20     local path="$1"
21     read size dummy < <( du -sm "$path" )
22     echo "$size"
23 }
24
25
26 # diskfree <minimum size in MB>
27 function diskfree() {
28     local size="$1"
29     echo -n "Checking free diskspace:"
30         (( free = `stat -f -c '%a / 2048 * ( %s / 512 )' $tmp ` ))
31
32     if [ "$free" -ge "$size" ]; then
33         echo " done."
34     else
35         cat >&2 << EOF
36
37
38 WARNING: Possibly not enough free disk space in "$tmp".
39
40 You need at least $size MB, but only $free MB seems free. Note: You
41 can specify an alternate directory by setting the environment variable
42
43 Press Ctrl+C to interrupt, or return to try to continue anyway.
44
45 TMPDIR.
46
47 EOF
48         read
49     fi
50 }
51
52
53 # extract_bin <file> <expected_min_size> <dest>
54 function extract_bin() {
55     local file="$1"
56     local expected_min_size="$2"
57     local dest="$3"
58     cat << EOF
59
60 In the next step, the binary file will be extracted. Probably a
61 license agreement will be displayed. Please read this agreement
62 carefully. If you do not agree to the displayed license terms, the
63 package will not be built.
64
65 EOF
66     read -e -p "Press [Return] to continue: " dummy
67     echo
68     local extract_dir="$tmp/extract"
69     mkdir "$extract_dir"
70     cd "$extract_dir"
71     echo
72
73     local extract_cmd
74     case "$archive_path" in
75         *.tar)
76             extract_cmd="tar xf";;
77         *.tar.bz2)
78             extract_cmd="tar --bzip2 -xf";;
79         *.tgz|*.tar.gz)
80             extract_cmd="tar xfz";;
81         *.zip)
82             extract_cmd="unzip -q";;
83         *)
84             extract_cmd=sh
85     esac
86
87     if ! $extract_cmd "$archive_path"; then
88         cat << EOF
89
90 WARNING: The package installation script exited with an error
91 value. Usually, this means, that the installation failed for some
92 reason. But in some cases there is no problem and you can continue
93 creating the Debian package.
94
95 Please check if there are any error messages. Press [Return] to
96 continue or Ctrl-C to abort.
97
98 EOF
99         read
100     fi
101     echo
102     echo -n "Testing extracted archive..."
103     local size="$( diskusage "$extract_dir" )"
104     if [ "$size" -lt "$expected_min_size" ]; then
105         cat << EOF
106
107 Invalid size ($size MB) of extracted archive. Probably you have not
108 enough free disc space in the temporary directory. Note: You can
109 specify an alternate directory by setting the environment variable
110 TMPDIR.
111
112 EOF
113         error_exit
114     else
115         cd "$extract_dir"
116         files=(*)
117         if [ "${#files[*]}" -ne 1 ]; then
118             cat << EOF
119
120 Expected one file, but found the following ${#files[*]} files:
121     ${files[*]}
122
123 EOF
124             error_exit
125         fi
126         mv "$files" "$dest"
127         echo -e " okay.\n"
128     fi
129 }
130
131
132 function read_maintainer_info() {
133     if [ -z "$maintainer_name" ]; then
134         if [ -n "$DEBFULLNAME" ]; then
135                 maintainer_name="$DEBFULLNAME"
136         elif [ -n "$DEBNAME" ]; then
137                 maintainer_name="$DEBNAME"
138         else
139                 default_name="$(getent passwd $(id -run) | cut -d: -f5| cut -d, -f1)"
140         
141         cat << EOF
142
143 Please enter your full name. This value will be used in the maintainer
144 field of the created package.
145
146 EOF
147
148         # gecos can be null
149         while [ -z "$maintainer_name" ]; do
150                 read -e -p "Full name [$default_name]:" maintainer_name
151                 if [ -z "$maintainer_name" ] && [ -n "$default_name" ]; then
152                         maintainer_name="$default_name"
153                 fi
154         done
155         fi
156     fi
157  
158     if [ -z "$maintainer_email" ]; then
159         local default_email=
160         if [ -n "$DEBEMAIL" ]; then
161             maintainer_email="$DEBEMAIL"
162         else 
163         if [ -r "/etc/mailname" ]; then
164             default_email="$( id -run )@$( cat /etc/mailname )"
165         else
166             default_email="$( id -run )@$( hostname --fqdn )"
167         fi
168         cat << EOF
169
170 Please enter a valid email address or press return to accept the
171 default value. This address will be used in the maintainer field of
172 the created package.
173
174 EOF
175         read -e -p "Email [$default_email]: " maintainer_email
176         if [ -z "$maintainer_email" ]; then
177             maintainer_email="$default_email"
178         fi
179         fi
180     fi
181 }
182
183 # provide the architecture for evaluation by plugins
184 function get_architecture() {
185     echo
186
187     export DEB_BUILD_ARCH=$(dpkg-architecture -qDEB_BUILD_ARCH)
188
189     export DEB_BUILD_GNU_TYPE=$(dpkg-architecture -qDEB_BUILD_GNU_TYPE)
190     
191     echo "Detected Debian build architecture: ${DEB_BUILD_ARCH:-N/A}"
192     
193     echo "Detected Debian GNU type: ${DEB_BUILD_GNU_TYPE:-N/A}"
194 }