#include <stdlib.h>
#include <stdio.h>
#include "swis.h"

#include "osmodule.h"
#include "toolbox.h"

#include "rc5wimp.h"
#include "config.h"

#include "sound.h"

#define PlayIt_Version 0x4D140
#define PlayIt_Config 0x4D141
#define PlayIt_LoadDriver 0x4D142
#define PlayIt_DriverInfo 0x4D143
#define PlayIt_Open 0x4D147
#define PlayIt_Play 0x4D14A

os_error *moo(int sound)
{
    os_error *e;
    int v;
    char filename[1024];

    if (!sound) return NULL;

    if (sound == -1)
    	sound = rand() % 5 + 1;

    e = (os_error *)_swix(PlayIt_Version, _OUT(0), &v);
    if (e || v < 133)
    {
        sprintf(filename, "%s.PlayIt.PlayIt", our_dir);
        e = xosmodule_load(filename);
        if (e) return e;
    }

    e = (os_error *)_swix(PlayIt_DriverInfo, 0);
    if (e)
    {
        sprintf(filename, "%s.PlayIt.", our_dir);
        e = (os_error *)_swix(PlayIt_LoadDriver, _IN(0), filename);
        if (e) return e;
    }

    /* Set non-looping, scale to system volume */
    e = (os_error *)_swix(PlayIt_Config, _INR(0,2), 0, 0xC, 0x4);
    if (e) return e;

    sprintf(filename, "%s.Moos.Moo%d", our_dir, sound);

    e = (os_error *)_swix(PlayIt_Open, _INR(0,1), filename, 0);
    if (e) return e;

    return (os_error *)_swix(PlayIt_Play, 0);
}
