
case $DEB_TARGET_GNU_SYSTEM in
  linux-gnu) DEB_TARGET_GNU_TYPE="$DEB_TARGET_GNU_CPU-linux" ;;
  *) DEB_TARGET_GNU_TYPE="$DEB_TARGET_GNU_CPU-$DEB_TARGET_GNU_SYSTEM" ;;
esac
DEB_TARGET_ARCH=`dpkg-architecture -t$DEB_TARGET_GNU_TYPE -qDEB_HOST_ARCH`

apt_options="--option Dir::Etc::SourceList=$TARGET/etc/apt/sources.list --option Dir::State::Lists=$TARGET/var/cache/apt/lists --option Debug::Nolocking=true --option APT::Architecture=$DEB_TARGET_ARCH --option APT::Architectures=$DEB_TARGET_ARCH --option Dir::Cache::archives=$TARGET/var/cache/apt/archives --option APT::Get::Force-Yes=true --option APT::Get::Download-Only=true --option Dir::State::status=$TARGET/var/lib/dpkg/status --option Dir::Etc::Preferences=$TARGET/etc/apt/preferences --option debug::pkgproblemresolver=1 --option Dir::Etc::Sourceparts=/nonexistant --option APT::Default-Release=$RELEASE"

DEBFOR=apt_debfor

target_setup () {
  mkdir -p $TARGET/var/lib/dpkg
  : >"$TARGET/var/lib/dpkg/status"
  : >"$TARGET/var/lib/dpkg/available"

  mkdir -p "$TARGET/var/cache/apt/lists/partial"
  mkdir -p "$TARGET/var/cache/apt/archives/partial"

  rm "$TARGET/var/lib/dpkg/status"
  touch "$TARGET/var/lib/dpkg/status"

  mkdir -p "$TARGET/etc/apt"
  sed -e "s/unstable/$RELEASE/g" < /etc/crosshurd/sources.list/$DEB_TARGET_GNU_SYSTEM > "$TARGET/etc/apt/sources.list"
  sed -e "s/unstable/$RELEASE/g" < /etc/crosshurd/preferences > "$TARGET/etc/apt/preferences"
}

apt_setup () {

  apt-get $apt_options update
  retval=$?
  sync

}

apt_fetch_deb () {
  apt-get $apt_options --download-only -y install $@
  retval=$?
  sync
}

extract () { (
  cd "$TARGET"
  local p=0
  for pkg in $(debfor "$@"); do
    p="$(($p + 1))"
    progress "$p" "$#" "Extracting packages"
    info "Extracting $pkg..."
    dpkg-deb --extract ./$pkg .
  done
  sync
); }

debfor () {
    "$DEBFOR" "$@"
}
                                                                                
apt_debfor () {
  for p in "$@"; do (
    cd "$TARGET/var/cache/apt/archives"
    local chk=0
    for x in ${p}_*_*.deb; do
      if [ -e "$x" ]; then
        echo "/var/cache/apt/archives/$x"
        chk=1
      fi
    done
    if [ "$chk" = 0 ]; then return 1; fi
  ); done
}

x_feign_install () {
        local pkg="$1"
        local deb="$(debfor $pkg)"
        local ver="$(dpkg-deb --field "$TARGET/$deb" Version)"
 
        mkdir -p "$TARGET/var/lib/dpkg/info"
 
        echo \
"Package: $pkg
Version: $ver
Status: install ok installed
" >> "$TARGET/var/lib/dpkg/status"
                                                                                
        touch "$TARGET/var/lib/dpkg/info/${pkg}.list"
}
 
progress () {
  if [ "$USE_BOOTFLOPPIES_INTERACTION" ]; then
    PROGRESS_NOW="$1"
    PROGRESS_END="$2"
    PROGRESS_WHAT="$3"
    PROGRESS_NEXT=""
    echo "P: $1 $2 $3" >&3
  fi
}

info () {
  if [ "$USE_BOOTFLOPPIES_INTERACTION" ]; then
    echo "I: $1" >&3
  else
    echo "I: $1"
  fi
}

setup_etc () {
  mkdir -p "$TARGET/etc"

  echo 'nameserver 127.0.0.1' > $TARGET/etc/resolv.conf
  echo debian > $TARGET/etc/hostname

  mkdir -p "$TARGET/etc/apt/"
}

conditional_cp () {
  if [ ! -e "$2/$1" -a -e "$1" ]; then cp -a "$1" "$2/$1"; fi
}

x_core_install () {
  for pkg in $@; do
    smallyes '' | (cd "$TARGET" && /usr/share/crosshurd/dpkg-hurd --unpack "$TARGET"/$(debfor "$pkg"))
  done
}

smallyes() {
  YES="${1-y}"
  while echo "$YES" ; do : ; done
}

gnu_set_server () {
  SERVER=$1
  OWNER=$2
  PERMS=$3
  VALUE=$4
  cd $TARGET/servers
  if [ ! -e $SERVER ] ; then
    touch $SERVER
    chown $OWNER:$OWNER $SERVER
    chmod $PERMS $SERVER
    setfattr -n gnu.translator -v "$VALUE" $SERVER
  fi
}
    
gnu_set_dev () {
  DEV_NODE=$1
  OWNER=$2
  PERMS=$3
  VALUE=$4 
  cd $TARGET/dev
  if [ ! -e $DEV_NODE ] ; then
    touch $DEV_NODE
    chown $OWNER:$OWNER $DEV_NODE
    chmod $PERMS $DEV_NODE
    setfattr -n gnu.translator -v "$VALUE" $DEV_NODE
    if [ $DEV_NODE = "fd" ] ; then
      ln -f -s fd/0 stdin
      ln -f -s fd/1 stdout
      ln -f -s fd/2 stderr
    fi
  fi  
}
  
gnu_setup_passive_translators () {
  if [ ! -e $TARGET/servers/exec ] ; then
    return 2
  else
    setfattr -n gnu.translator -v "/hurd/exec\0" $TARGET/servers/exec 2> /dev/null || return 1
  fi
  info "Setting up passive translators..."
  gnu_set_server socket/1 root 666 "/hurd/pflocal\0"
  gnu_set_server socket/2 root 666 "/hurd/pfinet\0-6\0/servers/socket/26\0"
  gnu_set_server socket/26 root 666 "/hurd/pfinet\0-4\0/servers/socket/2\0"
  gnu_set_server crash-suspend root 666 "/hurd/crash\0--suspend\0"
  gnu_set_server crash-dump-core root 666 "/hurd/crash\0--dump-core\0"
  gnu_set_server crash-kill root 666 "/hurd/crash\0--kill\0"
  gnu_set_server password root 666 "/hurd/password\0"
  gnu_set_server default-pager root 666 "/hurd/proxy-defpager\0"
  if [ ! -e $TARGET/servers/crash ] ; then
    ln -s crash-suspend $TARGET/servers/crash
  fi
  if [ ! -e $TARGET/servers/socket/local ] ; then
    ln -s 1 $TARGET/servers/socket/local
  fi
  if [ ! -e $TARGET/servers/socket/inet ] ; then
    ln -s 2 $TARGET/servers/socket/inet
  fi
  gnu_set_dev fd root 666 "/hurd/magic\0--directory\0fd\0"
  gnu_set_dev console root 600 "/hurd/term\0/dev/console\0device\0console\0"
  gnu_set_dev tty root 666 "/hurd/magic\0tty\0"
  gnu_set_dev null root 666 "/hurd/null\0"
  gnu_set_dev zero root 666 "/hurd/storeio\0-Tzero\0"
  gnu_set_dev full root 666 "/hurd/null\0--full\0"
  gnu_set_dev time root 644 "/hurd/storeio\0--no-cache\0time\0"
  gnu_set_dev mem root 660 "/hurd/storeio\0--no-cache\0mem\0"
  gnu_set_dev klog root 660 "/hurd/streamio\0kmsg\0"
  gnu_set_dev shm root 644 "/hurd/tmpfs\0--mode=1777\00050%\0"
  for J in 0 1 2 3 4 5 6 7 8 9 a b c d e f g h i j k l m n o p q r s t u v; do
    gnu_set_dev ptyp$J root 666 "/hurd/term\0/dev/ptyp$J\0pty-master\0/dev/ttyp$J\0"
    gnu_set_dev ttyp$J root 666 "/hurd/term\0/dev/ttyp$J\0pty-slave\0/dev/ptyp$J\0"
  done
  for J in 0 1 2 3 4 5 6 7 8 9 a b c d e f g h i j k l m n o p q r s t u v; do
    gnu_set_dev ptyq$J root 666 "/hurd/term\0/dev/ptyq$J\0pty-master\0/dev/ttyq$J\0"
    gnu_set_dev ttyq$J root 666 "/hurd/term\0/dev/ttyq$J\0pty-slave\0/dev/ptyq$J\0"
  done
  gnu_set_dev vcs root 600 "/hurd/console\0"
  for I in 1 2 3 4 5 6; do
    gnu_set_dev tty$I root 600 "/hurd/term\0/dev/tty$I\0hurdio\0/dev/vcs/$I/console\0"
  done
} 
