#!/bin/sh -e

unset TMP TEMP TMPDIR || true

if [ "$DEBOOTSTRAP_DIR" = "" ]; then
    DEBOOTSTRAP_DIR=/usr/lib/debootstrap
fi

DEVICES_TARGZ=$DEBOOTSTRAP_DIR/devices.tar.gz

. $DEBOOTSTRAP_DIR/functions

export LANG=C
umask 022

usage()
{
    echo "Usage: ${0##*/} [OPTION]... <suite> <target> [<mirror> [<script>]]"
    echo "Bootstrap Debian base system."
    echo
    cat <<EOF
      --arch                 set the target architecture (use if no dpkg)
                               [ --arch powerpc ]
      --download-only        download packages, but don't perform installation
      --print-debs           print the packages to be installed, and exit
      --unpack-tarball       acquire .debs from a tarball instead of http
      --boot-floppies        used for internal purposes by boot-floppies
      --help                 display this help and exit
      --include=A,B,C        adds specified names to the list of base packages
      --exclude=A,B,C        removes specified packages from the list
      --verbose              don't turn off the output of wget
EOF
}

if [ $# != 0 ] ; then
  while true ; do
    case "$1" in
	--help)
	  usage
	  exit 0
	  ;;
	--boot-floppies)
	  if ! (echo -n "" >&3) 2>/dev/null; then
	    error 1 "If running debootstrap by hand, don't use --boot-floppies"
	  fi
	  USE_BOOTFLOPPIES_INTERACTION=yes
	  shift
	  ;;
	--print-debs)
	  JUST_PRINT_DEBS=yes
	  shift
	  ;;
	--download-only)
	  DOWNLOAD_ONLY=yes
	  shift
	  ;;
	--arch)
	  if [ -n "$2" ] ; then
	    ARCH="$2"
	    shift 2
	  else
	    error 1 "option requires an argument $1"
	  fi
	  ;;
	--unpack-tarball)
	  if [ -n "$2" ] ; then
	    if [ ! -f "$2" ] ; then
	      error 1 "$2: No such file or directory"
	    fi
	    UNPACK_TARBALL="$2"
	    shift 2
	  else
	    error 1 "option requires an argument $1"
	  fi
	  ;;
  --include*)
    additional="$(echo $1 | cut -f2 -d"="|tr , " ")"
    shift 1
    ;;
  --exclude*)
    exclude="$(echo $1 | cut -f2 -d"="|tr , " ")"
    shift 1
    ;;
  --verbose)
    export verbose=true
    shift 1
    ;;
	*)
	  break
	  ;;
    esac
  done
else
  info "usage: [OPTION]... <suite> <target> [<mirror> [<script>]]"
  info "Try \`${0##*/} --help for more information."
  error 1 "You must specify a suite and a target."
fi

if [ "$1" = "" -o \( "$2" = "" -a "$JUST_PRINT_DEBS" = "" \) ]; then
  info "usage: [OPTION]... <suite> <target> [<mirror> [<script>]]"
  info "Try \`${0##*/} --help for more information."
  error 1 "You must specify a suite and a target."
fi

SUITE="$1"
TARGET="$2"
if [ "${TARGET#/}" = "${TARGET}" ]; then
  if [ "${TARGET%/*}" = "$TARGET" ] ; then
    TARGET="$(echo `pwd`/$TARGET)"
  else
    TARGET="$(cd ${TARGET%/*}; echo `pwd`/${TARGET##*/})"
  fi
fi

MIRRORS="http://ftp.debian.org/debian"
SCRIPT="$DEBOOTSTRAP_DIR/scripts/$1"
if [ "$3" != "" ]; then
  MIRRORS="$3"
  if [ "$4" != "" ]; then
    SCRIPT="$4"
  fi
fi

# Remove trailing /'s
TARGET="${TARGET%/}"
MIRRORS="${MIRRORS%/}"

if [ "$ARCH" != "" ]; then
	true
elif [ -x /usr/bin/dpkg ] && /usr/bin/dpkg --print-architecture >/dev/null 2>&1
then
	ARCH=`/usr/bin/dpkg --print-architecture`
elif [ -e $DEBOOTSTRAP_DIR/arch ]; then
	ARCH=`cat $DEBOOTSTRAP_DIR/arch`
else
	error 1 "Couldn't work out current architecture"
fi

export MIRRORS ARCH SUITE TARGET

if [ "$JUST_PRINT_DEBS" = "" -a "$DOWNLOAD_ONLY" = "" -a -x /usr/bin/id ] && [ `id -u` -ne 0 ]; then
  error 1 "debootstrap can only run as root"
fi

if [ ! -e "$SCRIPT" ]; then
  error 1 "No such script: $SCRIPT"
fi

mkdir -p "$TARGET"

PKGDETAILS=$DEBOOTSTRAP_DIR/pkgdetails

if [ "$UNPACK_TARBALL" ]; then
  if [ "${UNPACK_TARBALL#/}" = "$UNPACK_TARBALL" ]; then
    error 1 "Tarball must be given a complete path"
  fi
  if [ "${UNPACK_TARBALL%.tar}" != "$UNPACK_TARBALL" ]; then
    (cd "$TARGET" && tar -xf "$UNPACK_TARBALL")
  elif [ "${UNPACK_TARBALL%.tgz}" != "$UNPACK_TARBALL" ]; then
    (cd "$TARGET" && zcat "$UNPACK_TARBALL" | tar -xf -)
  else
    error 1 "Unknown tarball: must be either .tar or .tgz"
  fi
fi

. "$SCRIPT"

work_out_debs

if [ "$JUST_PRINT_DEBS" ]; then
  echo "$all_debs"
  exit 0
fi

download $all_debs

if [ "$DOWNLOAD_ONLY" ]; then
  exit 0
fi

install_debs

if [ -e "$TARGET/etc/apt/sources.list" ]; then
  rm -f "$TARGET/etc/apt/sources.list"
fi

if [ -n "$USE_BOOTFLOPPIES_INTERACTION" ] ; then
  echo "I: debootstrap: Successfully completed" # goes to /dev/tty4
  sleep 1 # give the user a second to see the success notice.
fi

