#! /bin/sh

if [ $1 ]; then
    uname="$1"
else
    uname=`uname -m`
    echo "Defaulting to this machine's architecture, $uname, for linking."
    echo "This may be overridden on the command line (i386,x86_64,ia64)."
fi

C=$(cd $(dirname $0) && pwd)
R=${C%/*/*}

if [ -n "$XEN" -a -d "$XEN" ]; then
  XEN=$(cd $XEN && pwd)
else
  XEN=$R/xen
fi
echo "Xen tree: $XEN"

if [ -n "$XL" -a -d "$XL" ]; then
  XL=$(cd $XL && pwd)
else
  XL=$R/linux-2.6.18-xen.hg
fi
echo "Linux tree: $XL"

cd $C

for d in $(find ${XL}/drivers/xen/ -mindepth 1 -maxdepth 1 -type d); do
    test -d $(basename $d) || continue
    lndir $d $(basename $d) > /dev/null 2>&1
done

ln -sf ${XL}/drivers/xen/core/gnttab.c platform-pci
if [ -f ${XL}/drivers/xen/core/features.c ]; then
    ln -sf ${XL}/drivers/xen/core/features.c platform-pci
else
    ln -sf ${XL}/drivers/xen/features.c platform-pci
fi
ln -sf ${XL}/drivers/xen/core/xen_proc.c xenbus
ln -sf ${XL}/drivers/xen/core/reboot.c platform-pci

mkdir -p include/asm include/xen

lndir -silent ${XL}/include/xen include/xen
ln -nsf ${XEN}/include/public include/xen/interface

# Need to be quite careful here: we don't want the files we link in to
# risk overriding the native Linux ones (in particular, system.h must
# be native and not xenolinux).
case "$uname" in
i[34567]86|x86_64)
    if [ -d ${XL}/arch/x86/include/mach-xen ]; then
        ln -sf ${XL}/arch/x86/include/mach-xen/asm/hypervisor.h include/asm
        ln -sf ${XL}/arch/x86/include/mach-xen/asm/hypercall*.h include/asm
        ln -sf ${XL}/arch/x86/include/mach-xen/asm/synch_bitops*.h include/asm
        ln -sf ${XL}/arch/x86/include/mach-xen/asm/maddr*.h include/asm
        ln -sf ${XL}/arch/x86/include/mach-xen/asm/gnttab_dma.h include/asm
        ln -sf ${XL}/arch/x86/lib/scrub.c balloon
    elif [ -d ${XL}/include/asm-x86 ]; then
        ln -sf ${XL}/include/asm-x86/mach-xen/asm/hypervisor.h include/asm
        ln -sf ${XL}/include/asm-x86/mach-xen/asm/hypercall*.h include/asm
        ln -sf ${XL}/include/asm-x86/mach-xen/asm/synch_bitops*.h include/asm
        ln -sf ${XL}/include/asm-x86/mach-xen/asm/maddr*.h include/asm
        ln -sf ${XL}/include/asm-x86/mach-xen/asm/gnttab_dma.h include/asm
        ln -sf ${XL}/arch/x86/lib/scrub.c balloon
    else
        if [ $uname = x86_64 ]; then
            mkdir -p include/asm-i386
            lndir -silent ${XL}/include/asm-i386 include/asm-i386
        else
            uname=i386
        fi
        ln -sf ${XL}/include/asm-$uname/mach-xen/asm/hypervisor.h include/asm
        ln -sf ${XL}/include/asm-$uname/mach-xen/asm/hypercall.h include/asm
        ln -sf ${XL}/include/asm-$uname/mach-xen/asm/synch_bitops.h include/asm
        ln -sf ${XL}/include/asm-$uname/mach-xen/asm/maddr.h include/asm
        ln -sf ${XL}/include/asm-$uname/mach-xen/asm/gnttab_dma.h include/asm
    fi
  ;;
"ia64")
    ln -sf ${XL}/include/asm-ia64/hypervisor.h include/asm
    ln -sf ${XL}/include/asm-ia64/hypercall.h include/asm
    ln -sf ${XL}/include/asm-ia64/synch_bitops.h include/asm
    ln -sf ${XL}/include/asm-ia64/maddr.h include/asm
    ln -sf ${XL}/include/asm-ia64/gnttab_dma.h include/asm
    mkdir -p include/asm/xen
    ln -sf ${XL}/include/asm-ia64/xen/xcom_hcall.h include/asm/xen
    ln -sf ${XL}/include/asm-ia64/xen/xencomm.h include/asm/xen
    ln -sf ${XL}/arch/ia64/xen/xcom_hcall.c platform-pci/
    ln -sf ${XL}/arch/ia64/xen/xcom_asm.S platform-pci/
    ln -sf ${XL}/arch/ia64/xen/xencomm.c platform-pci/xencomm_arch.c
    ln -sf ${XL}/drivers/xen/core/xencomm.c platform-pci
  ;;
*)
    echo unknown architecture $uname
    exit 1
  ;;
esac
