am-ft: make the environment available earlier
[platform/upstream/automake.git] / maintainer / am-ft
1 #!/usr/bin/env bash
2 # Remote testing of Automake tarballs made easy.
3 # This script requires Bash 4.x or later.
4
5 # Copyright (C) 2013 Free Software Foundation, Inc.
6
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 2, or (at your option)
10 # any later version.
11
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 # GNU General Public License for more details.
16
17 # You should have received a copy of the GNU General Public License
18 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
20 # TODO: some documentation would be nice ...
21
22 set -u
23 me=${0##*/}
24
25 fatal () { echo "$me: $*" >&2; exit 1; }
26
27 cmd='
28   test_script=$HOME/.am-test/run
29   if test -f "$test_script" && test -x "$test_script"; then
30     "$test_script" "$@"
31   else
32     nice -n19 ./configure && nice -n19 make -j10 check
33   fi
34 '
35
36 remote=
37 interactive=1
38 while test $# -gt 0; do
39   case $1 in
40    -b|--batch) interactive=0;;
41    -c|--command) cmd=${2-}; shift;;
42    -*) fatal "'$1': invalid option";;
43     *) remote=$1; shift; break;;
44   esac
45   shift
46 done
47 [[ -n $remote ]] || fatal "no remote given"
48
49 if ((interactive)); then
50   do_on_error='{
51     AM_TESTSUITE_FAILED=yes
52     export AM_TESTSUITE_FAILED
53     # We should not modify the environment with which the failed
54     # tests have run, hence do not read ".profile", ".bashrc", and
55     # company.
56     exec bash --noprofile --norc -i
57   }'
58 else
59   do_on_error='exit $?'
60 fi
61
62 tarball=$(echo automake*.tar.xz)
63
64 case $tarball in
65   *' '*) fatal "too many automake tarballs: $tarball";;
66 esac
67
68 test -f $tarball || fatal "no automake tarball found"
69
70 distdir=${tarball%%.tar.xz}
71
72 env='PATH=$HOME/bin:$PATH'
73 if test -t 1; then
74   env+=" TERM='$TERM' AM_COLOR_TESTS=always"
75 fi
76
77 # This is tempting:
78 #   $ ssh "command" arg-1 ... arg-2
79 # but doesn't work as expected.  So we need the following hack
80 # to propagate the command line arguments to the remote shell.
81 quoted_args=--
82 while (($# > 0)); do
83   case $1 in
84     *\'*) quoted_args+=" "$(printf '%s\n' "$1" | sed "s/'/'\\''/g");;
85        *) quoted_args+=" '$1'";;
86   esac
87   shift
88 done
89
90 set -e
91 set -x
92
93 scp $tarball $remote:tmp/
94
95 # Multiple '-t' to force tty allocation.
96 ssh -t -t $remote "
97   set -x; set -e; set -u;
98   set $quoted_args
99   cd tmp
100   if test -e $distdir; then
101     # Use 'perl', not only 'rm -rf', to correctly handle read-only
102     # files or directory.  Fall back to 'rm' if something goes awry.
103     perl -e 'use File::Path qw/rmtree/; rmtree(\"$distdir\")' \
104       || rm -rf $distdir || exit 1
105     test ! -e $distdir
106   fi
107   export $env
108   "'
109   am_extra_acdir=$HOME/.am-test/extra-aclocal
110   am_extra_bindir=$HOME/.am-test/extra-bin
111   am_extra_setup=$HOME/.am-test/extra-setup.sh
112   if test -d "$am_extra_acdir"; then
113     export ACLOCAL_PATH=$am_extra_acdir${ACLOCAL_PATH+":$ACLOCAL_PATH"}
114   fi
115   if test -d "$am_extra_bindir"; then
116     export PATH=$am_extra_bindir:$PATH
117   fi
118   '"
119   xz -dc $tarball | tar xf -
120   cd $distdir
121   if test -f \"\$am_extra_setup\"; then
122     . \"\$am_extra_setup\"
123   fi
124   ($cmd) || $do_on_error
125 "