#!/bin/sh
# (c) Copyright 2001-2003. CodeWeavers, Inc.
#-----------------------------------------------------------------------------
#  buildrpm
#   This utility will create a custom RPM from a CrossOver installation
#  which makes it simpler for some sites to distribute CrossOver
#  over a wide range of RPM based machines.
#
#   Usage:
#       /opt/cxoffice/bin/buildrpm tag-name
#   where tag-name is a unique identifier you give to this build.
#
#   Creates:
#       i386/cxoffice-1.1-<tag-name>.rpm
#
#   Notes:
#       The intended usage of this tool is that you pick one machine,
#   and you install CrossOver as root, install all of the office apps you
#   wish to use, and you configure all of the applications and mime types
#   just the way you like them.
#       Then, when that's done, you can build an RPM and use your
#   own internal tools (or a third party management tool) to automatically
#   install this RPM on other (similarly configured) system within
#   your organization.
#-----------------------------------------------------------------------------


# Portable which(1) implementation
cxwhich()
{
    case "$1" in
    /*)
        if [ -x "$1" ]
        then
            echo "$1"
            return 0
        fi
        ;; 
    */*)
        if [ -x "`pwd`/$1" ]
        then
            echo "`pwd`/$1"
            return 0
        fi
        ;;
    *)
        saved_ifs="$IFS"
        IFS=":"
        for d in $PATH
        do
            IFS="$saved_ifs"
            if [ -n "$d" -a -x "$d/$1" ]
            then
                echo "$d/$1"
                return 0
            fi
        done
        ;;
    esac
    return 1
}

# Locate where CrossOver is installed by looking for the directory
# where this this script is located, unwinding symlinks on the way
cx_root=""
locate_cx_root()
{
    [ -n "$cx_root" ] && return 0
    cx_root="$CX_ROOT"
    if [ -z "$cx_root" ]
    then
        argv0=`cxwhich "$0"`
        [ -z "$argv0" ] && argv0="$0"
        echo "$argv0" | egrep "^/" >/dev/null
        if [ $? -ne 0 ]
        then
            argv0="`pwd`/$argv0"
        fi
        dir=`dirname "$argv0"`
        while [ ! -x "$dir/cxtcl" ]
        do
            [ ! -h "$argv0" ] && break
            argv0=`ls -l "$argv0" | sed -e 's/^.*-> //'`
            echo "$argv0" | egrep "^/" >/dev/null
            if [ $? -ne 0 ]
            then
                argv0="$dir/$argv0"
            fi
            dir=`dirname "$argv0"`
        done
        dir=`echo "$dir" | sed -e 's%\(/\.\)*$%%' -e 's%\(/\./\(\./\)*\)%/%'`
        cx_root=`dirname "$dir"`
    fi
    if [ ! -x "$cx_root/bin/cxtcl" ]
    then
        echo "`basename \"$0\"`:error: could not find CrossOver in \"$cx_root\"" >&2
        exit 1
    fi
    CX_ROOT="$cx_root"
    export CX_ROOT
    return 0
}

locate_cx_root


#-----------------------------------------------------------------------------
#  Validate arguments
#-----------------------------------------------------------------------------
if [ $# -ne 1 ] ; then
    echo "Error - Incorrect parameters"
    echo "Usage: `basename \"$0\"` tag-name"
    echo "(once you have installed CrossOver as root, and installed all applications"
    echo "to your liking)"
    exit 1
fi

#------------------------------------------------------------------------------
#   Find the template.spec file
#------------------------------------------------------------------------------
template="BOGUS"
if [ -f "$cx_root/support/template.spec" ] ; then
    template="$cx_root/support/template.spec"
    output_dir="$cx_root/support/dotwine"
fi
if [ "$template" = "BOGUS" ] ; then
    echo "Error - unable to find template.spec in '$cx_root/support'"
    exit 1
fi

if [ ! -d "$output_dir" ] ; then
    echo "Error - unable to find '$output_dir'; you are probably not operating in shared global mode"
    exit 1
fi



#------------------------------------------------------------------------------
#   Find the plugin directories (this becomes a no op for cxoffice)
#------------------------------------------------------------------------------
plugin_list=""



#------------------------------------------------------------------------------
# Generate a temp spec file name
#------------------------------------------------------------------------------
specfile="/tmp/$1.spec"
cat "$template" | sed "s/@release@/$1/g" | sed "/@plugin_list@/ r /tmp/npcxplugin.list.tmp" | grep -v "@plugin_list" >"$specfile"

#------------------------------------------------------------------------------
# Save all mime settings
#------------------------------------------------------------------------------
if [ -x "$cx_root/bin/cxsetup" ] ; then
    "$cx_root/bin/cxsetup" --save-state "$output_dir/wmstate" --global
fi

#------------------------------------------------------------------------------
# Based on the template spec, create one that uses their tag name
#------------------------------------------------------------------------------
rpmbuild=`cxwhich rpmbuild`
if [ -z "$rpmbuild" ]
then
    rpmbuild=`cxwhich rpm`
    echo "Warning:  rpmbuild not found - this may lead to errors." >&2
fi
if [ -z "$rpmbuild" ]
then
    echo "error: unable to find rpmbuild or rpm" >&2
    exit 1
fi
"$rpmbuild" -bb --rmspec --define "_rpmdir `pwd`" "$specfile"
