#! /bin/sh
# Copyright (C) 2006  Benoit Sigoure <tsuna@lrde.epita.fr>.
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
# USA.

echo 'If you need to pass through a proxy to access the web, kill me'
echo "and restart me with: env http_proxy=<your_proxy> $0 ..."
set -ex

: ${MINGW_PATH=C:/mingw}

source=http://tsunanet.net/autotroll

umask 022
test -d ~/bin || mkdir ~/bin
cd ~/bin

for i in my_shell.pl win_wrapper.sh
do
  test -f $i || wget $source/$i
  chmod +x $i
done

if [ ! -d "$MINGW_PATH" ]; then
  echo "\
  @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  Warning: it looks like you did not install MinGW
  or at least not under
    $MINGW_PATH
  Re run me with env MINGW_PATH=C:/path/mingw $0
  @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  exit 1
fi

test -f ./win_wrapper.sh || {
  echo "$0: ./win_wrapper.sh must exist." >&2
  exit 1
}

rv=0
for i in i /cygdrive/c/mingw/bin/*; do
  if test -f "`basename $i`"; then
    echo 'skip (already exists)'
    continue
  fi
  if test ! -x $i; then 
    echo 'ignored (not executable)'
    continue
  fi
  if test -d $i; then
    echo 'ignored (directory)'
    continue
  fi
  case $i in
    *.dll) echo 'ignored (dll)'; continue;;
  esac
  echo
  ln -s -f win_wrapper.sh `basename $i` || {
    echo "error (ln -s -f returned $?)"
    rv=1
    continue
  }
  case $i in
    *.exe)
      ln -s -f win_wrapper.sh `basename $i | sed 's/\.exe$//'` || {
        echo "error (ln -s -f returned $?)"
        rv=1
      }
      ;;
  esac
done

ln -s -f win_wrapper.sh make
ln -s -f win_wrapper.sh make.exe

if [ x"`make --version | sed q`" = x'GNU Make 3.81' ]; then
  echo 'It looks like you have Cygwin or MinGW GNU Make 3.81
Please install a manually patched version of GNU Make 3.81
with the patches in http://tsunanet.net/autotroll/win32.html
and install it with the default prefix (/usr/local)'
fi

echo 'Do not forget to put ~/bin first in your PATH'
