#!/bin/sh

#  mkcygwiniso.sh - script to generate Cygwin ISO images
#  Copyright (C) 2003 Lucas Nussbaum <lucas@lucas-nussbaum.net>
#  Based on script found on http://ns1.iocc.com/~joshua/cygwin/

#  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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

# changelog
# 1.0 - Joshua <http://ns1.iocc.com/~joshua/cygwin/>
# 2.0 - Lucas Nussbaum modification <http://www.lucas-nussbaum.net/>
# 3.0 - Kairo Araujo adaptation <http://www.kairo.eti.br>
#	   : Added the SETUPEXEURL for setup.exe	

# Mirror address. To be chosen from http://www.cygwin.com/mirrors.html
# You need to put ftp:// or http:// at the beginning. If you don't, the
# script won't work.
MIRRORURL=ftp://mirror.switch.ch/mirror/cygwin/

# URL address for get setup.exe
SETUPEXEURL=http://www.cygwin.com/

# directory to download files to
DOWNLOADDIR=cygwin_data/

# File in which we want to store the list of files to download
DOWNLOADLIST=cygwinlist

# name of the ISO file
ISO=cygwin.iso

# get the bz2 compressed setup.ini or the uncompressed one ?
GETBZ2=1

# clean up everything (remove $DOWNLOADDIR and $DOWNLOADLIST after generating ISO ?)
CLEANUP=0

# only download current version ? This saves a lot of space. If set to 0,
# versions tagged [prev] and [test] will be downloaded too.
ONLYCURRENT=1

# Wget options
WGETOPTS="-c -nv"

# Validate usage (minimally)
if [ $# -ne 0 ]; then
	echo "$0: Usage $0 (no parameters needed, config made inside the script)" >&2
	exit 1
fi

# Remove trailing / from MIRRORURL and DOWNLOADDIR
MIRRORURL="${MIRRORURL%*/}"
DOWNLOADDIR="${DOWNLOADDIR%*/}"
# computes number of directories to cut.
CUTDIR=$(($(echo $MIRRORURL | tr / "
" | wc -l)-3))

if [ ! -d $DOWNLOADDIR ]; then
	mkdir $DOWNLOADDIR
	if [ $? -ne 0 ]; then
		echo "$0: Error creating $DOWNLOADDIR" >&2
		exit 1;
	fi
fi

MIRRORNAME=$(echo "$MIRRORURL" |sed -e 's/:/%3a/g' -e 's|/|%2f|g')

if [ ! -d $DOWNLOADDIR/$MIRRORNAME ]; then
	mkdir $DOWNLOADDIR/$MIRRORNAME
	if [ $? -ne 0 ]; then
		echo "$0: Error creating $DOWNLOADDIR/$MIRRORNAME" >&2
		exit 1;
	fi
fi

# Retrieve the "setup.ini" file from the mirror
# Get the BZip2-compressed form or the normal form ?
if [ "$GETBZ2" -eq 1 ]; then
	SETUP=setup.bz2
else
	SETUP=setup.ini
fi

# get setup.{ini|bz2}
wget $WGETOPTS -O "$DOWNLOADDIR/$MIRRORNAME/$SETUP" "$MIRRORURL/$SETUP"

if [ $? -ne 0 ]; then
	echo "$0: Error retrieving \"$SETUP\" from \"$MIRRORURL/$SETUP\"" >&2
	exit 1;
fi

if [ "$GETBZ2" -eq 1 ]; then
	bzcat "$DOWNLOADDIR/$MIRRORNAME/$SETUP" >"$DOWNLOADDIR/$MIRRORNAME/setup.ini"
	rm "$DOWNLOADDIR/$MIRRORNAME/$SETUP"
fi

# Download the files


DOWNLOADLISTTMP=$(mktemp)
if [ $ONLYCURRENT -eq 1 ]; then
   # awk roxor
   DOWNLOADLISTTMP2=$(mktemp)
   awk 'BEGIN { seen=0 } /^$/ { seen=0 } /^\[/ { seen=1} { if (seen == 0) print '$*' }' $DOWNLOADDIR/$MIRRORNAME/setup.ini > $DOWNLOADLISTTMP2
   sed -n -e "/^install: / s|^install: \([^ ]*\) .*|$MIRRORURL/\1|p" $DOWNLOADLISTTMP2 > $DOWNLOADLISTTMP
   rm -f $DOWNLOADLISTTMP2
else
   sed -n -e "/^install: / s|^install: \([^ ]*\) .*|$MIRRORURL/\1|p" $DOWNLOADDIR/$MIRRORNAME/setup.ini > $DOWNLOADLISTTMP
fi
sort $DOWNLOADLISTTMP > $DOWNLOADLIST
rm $DOWNLOADLISTTMP

echo "You can now edit $DOWNLOADLIST, remove unwanted files (e.g XFree86)"
echo "Press Enter when ready to start download"
read test

echo "Downloading setup.exe"
wget $WGETOPTS -c -O "$DOWNLOADDIR/setup.exe" $SETUPEXEURL/setup.exe
if [ $? -ne 0 ]; then
	echo "$0: Error retrieving setup.exe !" >&2
	exit 1;
fi

cd $DOWNLOADDIR/$MIRRORNAME

for i in `cat ../../$DOWNLOADLIST | tr "\n" " "`
do
	wget $WGETOPTS -x -nH --cut-dirs=$CUTDIR $i
	if [ $? -ne 0 ]; then
		echo "$0: Error retrieving $i !" >&2
		exit 1;
	fi
done

# creating ISO

cd ../..

mkisofs -o $ISO -r -J $DOWNLOADDIR/*

echo "ISO file $ISO successfully generated !"
ls -lh $ISO

if [ $CLEANUP -eq 1 ]; then
	rm -rf $DOWNLOADDIR
	rm -f $DOWNLOADLIST
fi

exit 0

