#!/bin/bash
#
# Created : Nov 01, 2004
#
# Updated : Oct 23, 2006
#
# CHANGE LOG
#    May 2005
#       Added menu to choose FTP
#    Jun 26, 2005
#        Added menu to choose cleaning or not
#        Download of setup.bz2 and bunzipping2 it
#    Feb 22, 2006
#        Bunzip2 only if the bz2 file exists
#        Reformatting
#    May 09, 2006
#        Adding ftp://bo.mirror.garr.it/mirrors/sourceware.org/cygwin/
#        from Italy
#
#    Oct 23, 2006
#        Adding ftp://sourceware.org/pub/cygwin/ from USA
#

# Mirror script for cygwin
# Uses Michael A. Chase "cleanup_setup.pl"
# in a modified version (V1.0303)
# and wget

# getcurr.sh and cleanup_setup.pl must reside in the same directory
# as setup.exe

# Current time
DATE=`date +%y%m%d_%H%M%S`

# Files to download
filelist="missing.lst"

# Choose your mirror with directory of setup.ini
#mirror="ftp://ftp.inf.tu-dresden.de/software/windows/cygwin32/";
# Cut directories                   1        2       3

while [ 1 ] ; do
  echo ""
  echo "Select the mirror to use or exit:"
  echo "1) ftp.plig.net --UK"
  echo "2) ftp.easynet.be --Belgium"
  echo "3) sourceware.mirrors.tds.net --Wisconsin(USA)"
  echo "4) ftp.ale.org --Atlanta(USA)"
  echo "5) sources-redhat.mirror.redwire.net --San Diego(USA)"
  echo "6) mirrors.kernel.org --Palo Alto(USA)"
  echo "7) bo.mirror.garr.it --ITALY"
  echo "8) sourceware.org --USA"
  echo ""
  echo "0) exit program"
  echo ""
  echo -n "Enter selection 0-8: "
  read selection
  echo ""

  case $selection in
    1 )
      mirror="ftp://ftp.plig.net/pub/cygwin/"
      cutdir=2
      break;;
    2 )
      mirror="ftp://ftp.easynet.be/cygwin/"
      cutdir=1
      break;;
    3 )
      mirror="ftp://sourceware.mirrors.tds.net/pub/sourceware.org/cygwin/"
      cutdir=3
      break;;
    4 )
      mirror="ftp://ftp.ale.org/pub/mirrors/cygwin/"
      cutdir=3
      break;;
    5 )
      mirror="ftp://sources-redhat.mirror.redwire.net/pub/sources.redhat.com/cygwin/"
      cutdir=3
      break;;
    6 )
      mirror="ftp://mirrors.kernel.org/sources.redhat.com/cygwin/"
      cutdir=2
      break;;
    7 )
      mirror="ftp://bo.mirror.garr.it/mirrors/sourceware.org/cygwin/"
      cutdir=3
      break;;
    8 )
      mirror="ftp://sourceware.org/pub/cygwin/"
      cutdir=2
      break;;

    0 )
      echo "OK! Exiting";
      exit 1;;
    * )
      echo "Please enter one of 0 to 8"
  esac
done

# Name your download directory
mirrordir="/home/$USER/Downloads/cygwin"

##echo $mirror
##echo $cutdir
##echo $mirrordir

if [ -e ./setup.ini ] ; then
  echo ""
  echo -n "Saving setup.ini to setup.ini.old"
  mv -f setup.ini setup.ini.old
  echo -n "...done"
  echo ""
fi

wget -r -N --no-host-directories --cut-dirs=$cutdir --passive-ftp -P $mirrordir ${mirror}setup.bz2

if [ -e ./setup.bz2 ] ; then
  echo ""
  echo -n "Bunzipping2 setup.bz2 and adding .ini to it..."
  bunzip2 setup.bz2
  mv -f setup setup.ini
  echo -n "...done"
  echo ""
else
  echo "BZ2 NOT FOUND! Exiting...";
  exit 2
fi

#rm -f $filelist

./clean_setup.pl -noprev -notest -missing $filelist
wget -r -N --no-host-directories --cut-dirs=$cutdir --passive-ftp -P $mirrordir -i $filelist -B $mirror

while [ 1 ] ; do
  echo ""
  echo -n "Cleaning (Y/N)? "
  read selection
  echo ""

  case $selection in
    Y | y )
      LOG=clean_setup_$DATE.log
      ./clean_setup.pl -DDir -DFile -DSetup -MFile 2>&1 | tee $LOG
      break;;
    N | n )
      break;;
  esac
done

##echo $LOG
##echo $selection

#shutdown [-x] 5
