#!/bin/sh
#
# Script that tries to create app_info.xml for 'anonymous' platform
# seti cruncher.
#
# It also includes claimed support for official cruncher version
# (currently 4.02).
#
#######################
#
# Some settings
#
DEBUG=${DEBUG:-false}
#
SETIAPP=setiathome
SETI_OFFICIAL_VERSION=402
SETI_OFFICIAL_BINARY="setiathome_4.02_i686-pc-linux-gnu"
#
# We'll assume that seti binary starts with the same prefix
#
SETIBIN_LIST=$( find . -name ${SETIAPP}\* -maxdepth 1 -print )
${DEBUG} && echo "SETIBIN_LIST=\"${SETIBIN_LIST}\"" >&2
SETIBIN_NUM=$( echo "${SETIBIN_LIST}" | wc -w )
if [ ${SETIBIN_NUM} -eq 0 ]; then
    echo "Error: can not find setiathome binary in current directory." >&2
    echo "Check if you have it here and has name starting with ${SETIAPP}." >&2
    exit 1
fi
if [ ${SETIBIN_NUM} -gt 1 ]; then
    echo "Error: you have several files which names start with ${SETIAPP}." >&2
    echo "I can't decide which is setiathome binary." >&2
    exit 1
fi
#
# Get the information from binary
#
SETIAPP_OUT=$( ${SETIBIN_LIST} -version 2>/dev/null )
if [ -z "${SETIAPP_OUT}" ]; then
    echo "Error: Can not get any useful info from apparent setiathome binary" >&2
    echo "(${SETIBIN_LIST})." >&2
    exit 1
fi
SETIHDR=$( echo "${SETIAPP_OUT}" | grep "^SETI@home client" )
if [ -z "${SETIHDR}" ]; then
    echo "Error: ${SETIBIN_LIST} doesn't seem to be setiathome binary." >&2
    exit 1
fi
SETIBIN=$( basename ${SETIBIN_LIST} )
SETI_VER=$( echo "${SETIAPP_OUT}" | grep "^Version:" | awk '{ print $2 }' )
${DEBUG} && echo "Application version is ${SETI_VER}" >&2
SETIAPP_VER=$( echo "${SETI_VER} * 100" | bc |cut -d. -f1 )
#
# Now we have all needed information. Let's dump data out.
#
cat <<EOF
<app_info>
    <app>
        <name>${SETIAPP}</name>
    </app>
    <file_info>
        <name>${SETIBIN}</name>
        <executable/>
    </file_info>
    <app_version>
        <app_name>${SETIAPP}</app_name>
        <version_num>${SETIAPP_VER}</version_num>
        <file_ref>
            <file_name>${SETIBIN}</file_name>
            <main_program/>
        </file_ref>
    </app_version>
    <app>
	<name>${SETIAPP}</name>
    </app>
    <file_info>
        <name>${SETI_OFFICIAL_BINARY}</name>
        <executable/>
    </file_info>
    <app_version>
	<app_name>${SETIAPP}</app_name>
	<version_num>${SETI_OFFICIAL_VERSION}</version_num>
	<file_ref>
	    <file_name>${SETIBIN}</file_name>
	    <main_program/>
	</file_ref>
    </app_version>
</app_info>
EOF
