
/*
 * See the files COPYING and README for copyright information and how to reach 
 * the author.
 *
 *  $Id: common.c,v 1.3 2004/12/04 13:09:33 lordjaxom Exp $
 */

#include <dirent.h>
#include <glob.h>
#include <iconv.h>
#include "common.h"
#include "setup.h"
#include <vdr/recording.h>
#include <vdr/i18n.h>
#include <sys/types.h>

const char *BurnStates[__STATE_COUNT__] = { "List", "Status" };
const char *DiskTypes[__TYPE_COUNT__] = {
    "DVD with menus",
    "DVD without menus",
    "Archive disk"
};
const char *StoreModes[__STORE_COUNT__] = {
    "Author only",
    "Create ISO only",
    "Write to Disc",
    "Create ISO and write"
};
const char *BurnModes[__MODE_COUNT__] = { "Shrink", "Distribute" };
const char *ChaptersModes[__CHAPTERS_COUNT__] = {
    "No chapters",
    "Use editing marks",
    "Every 5 minutes",
    "Every 10 minutes",
    "Every 15 minutes",
    "Every 30 minutes",
    "Every hour"
};

const char *TitleChars = "abcdefghijklmnopqrstuvwxyz"
    "0123456789äöüß:.-;,#% ";

uint64_t ScanRecordingSize(const char *FileName, eDiskType dt)
{
    uint64_t size = 0;
    char *buf;
    FILE *f;

    if (dt == dtVDR) {
        asprintf(&buf, "%s/size.vdr", FileName);
    } else {
        asprintf(&buf, "%s/size_cut.vdr", FileName);
    }

    if ((f = fopen(buf, "r")) != NULL) {
        fscanf(f, "%lld", &size);
        fclose(f);
    }
    if (size == 0) {
        esyslog("BURN: No datafiles in %s (this shouldn't happen!)",
                FileName);
    }
    return size;
}

int ScanPageCount(const char *Path)
{
    glob_t globbuf;
    char *path;
    int res;

    asprintf(&path, "%s/menu-bg-*.png", Path);
    if (glob(path, 0, NULL, &globbuf) != 0) {
        esyslog("BURN: Error while scanning %s: %m", path);
        free(path);
        return -1;
    }

    res = globbuf.gl_pathc;
    globfree(&globbuf);
    free(path);
    return res;
}

uint64_t FileSize(const char *FileName)
{
    int res;
    struct stat64 buf;
    if ((res = stat64(FileName, &buf)) == 0) {
        return buf.st_size;
    }
    return 0;
}

const char *FileType(const char *FileName)
{
    int len;
    return (len = strlen(FileName)) > 3 ? FileName + len - 3 : "";
}

const char *ProgressBar(double Current, double Total, int Length)
{
    static char *buffer = NULL;
    int l;
    free(buffer);
    if (Length < 2) {
        return "";
    }
    buffer = MALLOC(char, Length + 1);
    buffer[0] = '[';
    if (Current > Total) {
        Current = Total;
    }
    l = (int)(Current * (Length - 2) / Total);
    memset(buffer + 1, '|', l);
    memset(buffer + l + 1, ' ', Length - 2 - l);
    buffer[Length - 1] = ']';
    buffer[Length] = '\0';
    return buffer;
}

const tTrackInfoArray & FindTitleTracks(const char *Path)
{
    static tTrackInfoArray tracks;
    glob_t globbuf;
    char *path;

    tracks.Clear();
    asprintf(&path, "%s/*.mpv", Path);
    if (glob(path, 0, NULL, &globbuf) != 0 || globbuf.gl_pathc < 1) {
        globfree(&globbuf);
        free(path);
        asprintf(&path, "%s/*.m2v", Path);
        if (glob(path, 0, NULL, &globbuf) != 0 || globbuf.gl_pathc < 1) {
            globfree(&globbuf);
            free(path);
            tracks.Clear();
            return tracks;
        }
    }
    tracks.Add(globbuf.gl_pathv[0], ttVideo, Setup.OSDLanguage);

    globfree(&globbuf);
    free(path);

    asprintf(&path, "%s/*.ac3", Path);
    if (glob(path, 0, NULL, &globbuf) == 0) {
        for (int i = 0; i < (int)globbuf.gl_pathc; ++i) {
            tracks.Add(globbuf.gl_pathv[i], ttAudioAC3, Setup.OSDLanguage);
        }
    }
    globfree(&globbuf);
    free(path);

    asprintf(&path, "%s/*.mpa", Path);
    if (glob(path, 0, NULL, &globbuf) == 0) {
        for (int i = 0; i < (int)globbuf.gl_pathc; ++i) {
            tracks.Add(globbuf.gl_pathv[i], ttAudioMP2, Setup.OSDLanguage);
        }
    }
    globfree(&globbuf);
    free(path);

    return tracks;
}

char *EscapeLatin1toUTF8(const char *Text)
{
    int len = strlen(Text);
    uchar *in = (uchar *) Text;
    char *utf = MALLOC(char, len * 2 + 1);
    uchar *out = (uchar *) utf;

    for (; *in; ++in) {
        if (BurnSetup.UseIso2UTF && *in >= 0x80) {
            *out++ = ((*in >> 6) & 0x1F) | 0xC0;
            *out++ = (*in & 0x3F) | 0x80;
            /* } else if (*in == '-') {
             *out++ = 0xD6;
             *out++ = 0xBE; */
		} else if (*in == 0x09) { 
                             *out++ = ' '; 
                       } else {
                             *out++ = *in;
                              }
    }
    *out = 0;
    return utf;
}

char *Convert2UTF8(const char *Text)
{
    if (!Text) {
        return strdup("");
    }

    const char *f = tr("iso8859-15");
    char *from = NULL;

    if (!strcasecmp(f, "iso8859-1")) {
        from = "ISO_8859-1";
    } else if (!strcasecmp(f, "iso8859-2")) {
        from = "ISO_8859-2";
    } else if (!strcasecmp(f, "iso8859-3")) {
        from = "ISO_8859-3";
    } else if (!strcasecmp(f, "iso8859-4")) {
        from = "ISO_8859-4";
    } else if (!strcasecmp(f, "iso8859-5")) {
        from = "ISO_8859-5";
    } else if (!strcasecmp(f, "iso8859-6")) {
        from = "ISO_8859-6";
    } else if (!strcasecmp(f, "iso8859-7")) {
        from = "ISO_8859-7";
    } else if (!strcasecmp(f, "iso8859-8")) {
        from = "ISO_8859-8";
    } else if (!strcasecmp(f, "iso8859-9")) {
        from = "ISO_8859-9";
    } else if (!strcasecmp(f, "iso8859-10")) {
        from = "ISO_8859-10";
    } else if (!strcasecmp(f, "iso8859-11")) {
        from = "ISO_8859-11";
    } else if (!strcasecmp(f, "iso8859-12")) {
        from = "ISO_8859-12";
    } else if (!strcasecmp(f, "iso8859-13")) {
        from = "ISO_8859-13";
    } else if (!strcasecmp(f, "iso8859-14")) {
        from = "ISO_8859-14";
    } else if (!strcasecmp(f, "iso8859-15")) {
        from = "ISO_8859-15";
    } else {
        from = "ISO_8859-1";
    }

    iconv_t ic = iconv_open("UTF8", from);
    if (ic >= 0) {
        size_t inbytesleft = sizeof(Text);
        size_t outbytesleft;
        size_t ret;
        char *out = MALLOC(char, inbytesleft * 4 + 1);;
        if ((ret =
             iconv(ic, (char **)&Text, &inbytesleft, &out,
                   &outbytesleft)) >= 0) {
            iconv_close(ic);
            return out;
        }
        free(out);
    }
    return strdup(Text);
}
