#!/bin/sh
# Copyright (C) 2006, 2007 Etersoft, Vitaly Lipatov <lav@etersoft.ru>
# Redistribution of this file is permitted under the terms of the
# GNU Public License (GPL)

# Create log output for WINE@Etersoft supporting purposes
# $Id: winelog,v 1.24 2007/12/15 22:47:23 lav Exp $

# Use ~/.wine by default.
[ -z "$WINEPREFIX" ] && export WINEPREFIX=${HOME}/.wine

# Create wine dir in anyway
mkdir -p "$WINEPREFIX" || exit 1

# Filter out your registration number
filter()
{
	sed -e "s|[0-9A-F]\{4\}-\([0-9A-F]\{4\}\)|XXXX-\1|"
}

wstart()
{
	echo
	echo
	echo "==============================================================================="
	echo "WINEPREFIX=$WINEPREFIX"
	echo "Current date:"
	date
	uname -a
	which TEST-PATH-COMMAND
	echo "------"
	locale
	echo "------"
	XCOM=X
	which $XCOM >/dev/null && $XCOM -version
	echo
	wine --version | filter
	echo
	case `uname` in
		"Linux")
			for i in /etc/*[-_]release /etc/*[-_]version ; do
				test -f $i && echo "$i: `cat $i`"
			done
			echo
			echo "------"
			/lib/libc-*
			echo
			echo "------"
			#if [ -f /etc/slackware-version ] || [ -f /etc/debian_version ] ; then
			#	echo "Non rpm-based system"
			#	# TODO: for Slackware/Debian
			#else
			#	rpm -qi wine
			#	echo "------"
			#	rpm -q fontconfig
			#	rpm -qa | grep freetype
			#	rpm -V wine libwine wine-etersoft
			#fi
			;;
		"FreeBSD")
			;;
	esac
	echo
	echo "------"
	WINEDIAG=winediag
	which $WINEDIAG >/dev/null && $WINEDIAG | filter
	echo
	echo "------"
	ls -l $WINEPREFIX/dosdevices/c:/windows/system32
	echo "MD5SUM:"
	md5sum $WINEPREFIX/dosdevices/c:/windows/system32/*
	echo "------"
	echo "Start WINE program with '$@' params"
	test -z "$WINEDEBUG" && export WINEDEBUG=+loaddll
	wine "$@"
}

export WINELOGFILO=$WINEPREFIX/wine.log

if [ "$1" = "-h" -o "$1" = "--help" -o -z "$1" ] ; then
	echo "Usage: winelog [-c] program.exe"
	echo "	-c - stdoutput without any prompt (for script using)"
	echo "Writes all wine output to $WINELOGFILO file. Please gzip it and send to your support contact."
	exit 0
fi

if ! touch "$WINELOGFILO" ; then
	echo "Please check permission on `dirname $WINELOGFILO` directory"
	exit 1
fi

if [ ! -r "$WINELOGFILO" ] ; then
	echo "Please check permission on $WINELOGFILO file"
	exit 1
fi

if [ "$1" = "-c" ] ; then
	shift
	wstart "$@" </dev/null
else
	#test -f $WINELOGFILO && mv -f $FILO $FILO.old
	echo "Append output to $WINELOGFILO... Please gzip it before send."
	wstart "$@" >>$WINELOGFILO 2>&1 </dev/null
fi
