# Maintainer: Vryali [vryali {at} gmail -dot- com]
# Former Maintainer: LeeF <leef (at) hushmail (dot) com
# Original Author  : Andrwe <lord-weber-andrwe (at) renona-studios (dot) org>

pkgname="citrix-client"
pkgver="12.0"
pkgrel="0"
pkgdesc="Provides XenApp(Citrix) for linux (x86_64 & i686)."

url="http://www.citrix.com"
source=('http://download.citrix.com.edgesuite.net/akdlm/6527/linuxx86-12.0.0.189834.tar.gz?__gda__=1320415777_d8fc05f7711b187757c3d7f1af7df7a9&__dlmgda__=1320501877_9649f574831a0e39685b05c9b90f8e4a&fileExt=.gz')
arch=('i686' 'x86_64')
license=('CAL')
md5sums=('d99029de72f8bb6ada8297d16f39ad90')

case ${CARCH} in
    i686 )
        depends=(
            'gtk2'
            'libx11'
            'libxaw'
            'libxext'
            'libxft'
            'libxmu'
            'libxp'
            'libxpm'
            'libxt'
            'openmotif'
            'printproto'
            'xextproto'
            'alsa-lib'
            'glib2'
        )
        instdir="/usr/lib/ICAClient"
        ;;
    x86_64 )
        depends=(
            'gtk2'
            'lib32-gcc-libs'
            'lib32-libjpeg'
            'lib32-libpng'
            'lib32-libvorbis'
            'lib32-libx11'
            'lib32-libxaw'
            'lib32-libxext'
            'lib32-libxft'
            'lib32-libxinerama'
            'lib32-libxmu'
            'lib32-libxp'
            'lib32-libxpm'
            'lib32-libxt'
            'lib32-openmotif'
            'printproto'
            'xextproto'
            'lib32-alsa-lib'
            'lib32-glib2'
            'lib32-gtk2'
        )
        instdir="/usr/lib32/ICAClient"
        ;;
esac

install=citrix-client.install

build() {
    # Create folder structure
    mkdir -p "${pkgdir}${instdir}"
    mkdir -p "${pkgdir}/usr/lib/mozilla/plugins"
    mkdir -p "${pkgdir}/usr/bin/"

    # copy needed files
    cp -r ./linuxx86/linuxx86.cor/* "${pkgdir}${instdir}"/
    cp ./setupwfc "${pkgdir}${instdir}"/

    # Go to package directory
    cd "${pkgdir}${instdir}"/
    
    # Create symlinks for the Npica.ad binary
    ln -s ./nls/en/Npica.ad ./Npica.ad
    ln -s ./Npica.ad ./Npica

    # Copy EULA to program root directory
    cp ${srcdir}/linuxx86/linuxx86.cor/nls/en/eula.txt ./

    # Copy default configuration files to config directory of the program and setting them readable
    cp ./nls/en/{appsrv.ini,wfclient.ini,module.ini} ./config/
    chmod 666 ./config/{appsrv.ini,wfclient.ini,module.ini}
    chmod 777 ./config

    # Go ahead and do the different operations pursiant with the architecture...
    if [[ "${CARCH}" != "i686" ]]; then
        # Create the 32 bit client script
        echo -e "#!/bin/sh\n${instdir}/wfcmgr" > "${pkgdir}"/usr/bin/citrix-client.sh
        chmod a+x "${pkgdir}"/usr/bin/citrix-client.sh
        # Create executable wrapper for the binaries
        echo -e "#!/bin/sh\nexport ICAROOT=${instdir}\n\${ICAROOT}/wfica -file \$1" > ./wfica.sh
        echo -e "#!/bin/sh\nexport ICAROOT=${instdir}\n\${ICAROOT}\n\${ICAROOT}/wfica -associate -fileparam \$1" > ./wfica_assoc.sh
        cp ./wfica.sh "${pkgdir}"/usr/bin/wfica
        cp ./wfica_assoc.sh "${pkgdir}"/usr/bin/wfica_assoc
        chmod 755 ${pkgdir}/usr/bin/{wfica,wfica_assoc}
        chmod 755 ./{wfica.sh,wfica_assoc.sh}
        # Copy Firefox plugin into plugin directory
        cp ./npica.so "${pkgdir}"/usr/lib/mozilla/plugins/
    else
        # We're 64 bit - Create executable wrapper for the binaries
        echo -e "#!/bin/sh\nexport ICAROOT=${instdir}\nexport GCONV_PATH=/usr/lib32/gconv/\n\${ICAROOT}/wfica -file \"\$1\"" > ./wfica.sh
        echo -e "#!/bin/sh\nexport ICAROOT=${instdir}\nexport GCONV_PATH=/usr/lib32/gconv/\n\${ICAROOT}/wfica -associate -fileparam \"\$1\"" > ./wfica_assoc.sh
        chmod a+x ./{wfica.sh,wfica_assoc.sh}
    fi
}
