#!/bin/bash
export QEMU_AUDIO_DRV=alsa

#-device virtio-serial -chardev spicevmc,id=vdagent,debug=0,name=vdagent \
#-device virtserialport,chardev=vdagent,name=com.redhat.spice.0 \
#-spice port=7100,disable-ticketing \

# kvm is deprecated, use qemu-

export SPICE_DEBUG_ALLOW_MC=1

usage()
{
cat << EOF
usage: $0 options

#ToDo Write some useful description

OPTIONS:
   -h      Show this message
   -a      ISO architecture, should be 'i386' or 'amd64'
   -f      'cinnamon','gnome','lxde','kde','nox','rqt','xfce','xorg'
   -r      release: '', 'current','next','xperimental'
   -s      smp - 1..8
   -p      Server port for spice
EOF
}


if [ -f ./liso.conf ]; then
    . ./liso.conf
elif [ -f ./etc/liso.conf ]; then
    . /etc/liso.conf
else
    Webuser=webdevel
    Webhost=test.siduction.org
    IsoPath=/srv/iso
    PublishingPath=/srv/iso-publisher
    ServerPath=/var/www/virtual/webdevelopment/iso/siduction

    # hold the three newes builds, delete older ones
    DeleteOldPublishedIsos=4

    CurrentReleaseName=firestarter
    NextReleaseName=paintitblack
    XperimenxtalReleaseName=thunderstruck

    IsoFlavour=nox
    IsoArch=amd64
    SpicePort=6666
    ReleaseCode=next
    SMP=2
fi

while getopts “f:a:p:r:s:h” OPTION; do
     case ${OPTION} in
         f)
             IsoFlavour=${OPTARG}
             ;;
         a)
             IsoArch=${OPTARG}
             ;;
         p)
             OptSpicePort=${OPTARG}
             ;;
         r)
             ReleaseCode=${OPTARG}
             ;;
         s)
             SMP=${OPTARG}
             ;;
         h)
             usage
             exit
             ;;
     esac
done

case ${ReleaseCode} in
    current)
        IsoRelease=${CurrentReleaseName}
        ;;
    next)
        IsoRelease=${NextReleaseName}
        ;;
    xperimental)
        IsoRelease=${XperimentalReleaseName}
        ;;
    *)
        echo "ReleaseCode nicht bekannt ${ReleaseCode}"
        exit 1
        ;;
esac

if [ -z ${IsoFlavour} ] || [ -z ${IsoArch} ]; then
     usage
     exit 1
fi

SearchIso="${IsoPath}/${IsoFlavour}/${IsoArch}/*${IsoRelease}*iso"
IsoFile=$(ls ${SearchIso} | tail -n1)

if [ "x${IsoFile}" == "x" ]; then
    echo ""
    echo "================================================"
    echo "Isofile with Searchpattern ${SearchIso} not found!"
    echo "eventually you would like one of these images:  "
    echo ""
    ls ${IsoPath}/${IsoFlavour}/${IsoArch}/*iso
    echo ""
    echo "Quitting now"
    exit 0
fi

IsoName=$(basename ${IsoFile%.*})
IsoVersion=$(echo ${IsoName} | awk -F '-' '{ print  $2 }')  # 13.1.1
IsoRelease=$(echo ${IsoName} | awk -F '-' '{ print  $3 }')  # paintitblack
IsoDateTime=$(echo ${IsoName} | awk -F '-' '{ print  $(NF) }' | sed "s/\([0-9]\{4\}\)\([0-9]\{2\}\)\([0-9]\{2\}\)\([0-9]\{2\}\)/\1-\2-\3_\4-/1" ) # datetime

case ${IsoFlavour} in
    cinnamon)
         SpicePort=7210
         ;;
    gnome)
         SpicePort=7220
         ;;
    lxde)
         SpicePort=7230
         ;;
    nox)
         SpicePort=7240
         ;;
    kde)
         SpicePort=7250
         ;;
    rqt)
         SpicePort=7260
         ;;
    xfce)
        SpicePort=7270
        ;;
    xorg)
        SpicePort=7280
        ;;
    *)
        SpicePort=6666;
        ;;
esac

## OptSpicePort ToDo ##
## spice-vdagebt ToDo ##

echo ""
echo ""
echo "=============================="
echo "KVM   Spice :                 "
echo "=============================="
echo "Iso-Version:  ${IsoVersion}"
echo "Iso-Flavour:  ${IsoFlavour}"
echo "Architecture: ${IsoArch}"
echo "Release:      ${IsoRelease}"
echo "SMP:          ${SMP}"
echo "Date/Time:    ${IsoDateTime}"
echo "Spice-Port:   ${SpicePort}"
echo ""
echo ""

IsoFile=/srv/iso-archive/iso-archive/13.1-firestarter/kde/siduction-13.1.0-firestarter-kde-amd64-201305211844.iso



qemu-system-x86_64 \
    -name siduction-kde \
    -drive if=virtio,file=qcow2/kde.qcow2,media=disk,cache=unsafe \
    -boot order=d \
    -enable-kvm \
    -m 4096 \
    -usbdevice tablet \
    -vga qxl \
    -spice port=6666,disable-ticketing \
    -net nic,model=virtio \
    -net user \
    -smp 2 \
    -cdrom ${IsoFile} \
    -device virtio-serial-pci \
    -device virtserialport,chardev=spicechannel0,name=org.siduction.spice.0 \
    -chardev spicevmc,id=spicechannel0,name=vdagent \

# Nach Beenden der KVM, Interface stoppen und entfernen
#sudo /sbin/ifconfig ${INTERFACE} down
#sudo /usr/sbin/tunctl -d ${INTERFACE} &> /dev/null
#reset


