# Packager: Phill Watkins <phill~dot~watkins~at~gmail~dot~com>

pkgname=qemu-kvm
pkgver=0.12.5
pkgrel=1pw
#arch=noarch
source=\
(
"http://downloads.sourceforge.net/project/kvm/qemu-kvm/$pkgver/qemu-kvm-$pkgver.tar.gz"
"http://people.salixos.org/pwatk/packages/xap/$pkgname/rc.kvm.gz"
"http://people.salixos.org/pwatk/packages/xap/$pkgname/kvm.txt.gz"
)
sourcetemplate=http://people.salixos.org/pwatk/packages/xap/$pkgname/$pkgver
docs=("Changelog" "CODING_STYLE" "COPYING*" "EXTERNAL_DEPENDENCIES" "KVM_VERSION" "LICENSE" "MAINTAINERS" "README" "TODO" "VERSION")
url=http://www.linux-kvm.org/page/Main_Page
options=('noautodotnew')

slackdesc=\
(
#|-----handy-ruler------------------------------------------------------|
"qemu-kvm (Modified QEMU for KVM full virtualization)"
"KVM (for Kernel-based Virtual Machine) is a full virtualization"
"solution for Linux on x86 hardware containing virtualization"
"extensions (Intel VT or AMD-V). It consists of a loadable kernel"
"module, kvm.ko, that provides the core virtualization infrastructure"
"and a processor specific module, kvm-intel.ko or kvm-amd.ko. KVM also"
"requires a modified QEMU although work is underway to get the required"
"changes upstream."
""
"The KVM kernel component is included in mainline Linux, as of 2.6.20."
)

doinst() {
    groupadd kvm > /dev/null 2>&1

    if [ "$(grep GenuineIntel /proc/cpuinfo)" ]; then
      kvmmod=intel
    elif [ "$(grep AuthenticAMD /proc/cpuinfo)" ]; then
      kvmmod=amd
    fi
    
    depmod -a > /dev/null 2>&1
    
    if [ -f $(find lib/modules/$(uname -r) -name kvm-$kvmmod.ko) ]; then
      if [ ! "$(lsmod | grep "^kvm_$kvmmod ")" ]; then
        modprobe kvm-$kvmmod
      fi
    fi
    
    udevadm control --reload-rules > /dev/null 2>&1
    udevadm trigger --action=change > /dev/null 2>&1
}

build() {
    set -e
    
    # There are a number of targets for full system and/or user mode emulation. This script
    # allows you to customise the target list as you see fit using the `target_list' variable below.
    #
    # To compile only the targets specified by the configure script set the variable to `default' 
    #
    # To compile all available targets set the variable to `all'
    #
    # To compile specific targets set the variable to a comma separated list from the available
    # targets below.
    #
    # Full system emulation: i386-softmmu,x86_64-softmmu,arm-softmmu,cris-softmmu,m68k-softmmu,
    #                        microblaze-softmmu,mips-softmmu,mipsel-softmmu,mips64-softmmu,
    #                        mips64el-softmmu,ppc-softmmu,ppcemb-softmmu,ppc64-softmmu,sh4-softmmu,
    #                        sh4eb-softmmu,sparc-softmmu,sparc64-softmmu
    # User mode emulation:   i386-linux-user,x86_64-linux-user,alpha-linux-user,arm-linux-user,
    #                        armeb-linux-user,cris-linux-user,m68k-linux-user,microblaze-linux-user,
    #                        mips-linux-user,mipsel-linux-user,ppc-linux-user,ppc64-linux-user,
    #                        ppc64abi32-linux-user,sh4-linux-user,sh4eb-linux-user,sparc-linux-user,
    #                        sparc64-linux-user,sparc32plus-linux-user
    target_list="all"
    
    if [ "$target_list" = "default" ]; then
      target_list=""
    elif [ "$target_list" = "all" ]; then
      target_list="--target-list="
    elif [ -n "$target_list" ]; then
      target_list="--target-list=$target_list"
    else 
      target_list=""
    fi

    cd $startdir/src/$pkgname-$pkgver

    ./configure \
      --prefix=/usr \
      --enable-system \
      --enable-linux-user \
      --enable-mixemu \
      $target_list \
      --audio-drv-list=oss,alsa,sdl,esd \
      --audio-card-list=ac97,es1370,sb16,cs4231a,adlib,gus \

    make
    make install \
      DESTDIR=$startdir/pkg \
      docdir=/usr/doc/$pkgname-$pkgver \
      mandir=/usr/man
    install -m 0755 kvm/kvm_stat $startdir/pkg/usr/bin/kvm_stat
    install -Dm 0644 kvm/scripts/65-kvm.rules $startdir/pkg/lib/udev/rules.d/65-kvm.rules
    sed -i 's/NAME="%k", //' $startdir/pkg/lib/udev/rules.d/65-kvm.rules
    
    install -Dm 0755 $startdir/src/rc.kvm $startdir/pkg/etc/rc.d/rc.kvm
    install -Dm 0644 $startdir/src/kvm.txt $startdir/pkg/etc/rc.d/desc.d/kvm.txt

    set +e
}