#!/bin/ksh 
#
#   @(#)svc_install.sunw	1.3 99/01/28 
# 
# Create SunVideo Compatibility Hierarchy  
# 
# Copyright (c) 1995 MultiMedia Access Corporation - Osprey Systems Division. 
# 1150 S.E. Maynard Rd., Suite 110, Cary, NC 27511 USA. All Rights Reserved.
# 
# This product and related documentation is protected by copyright laws and 
# international treaties. No part of this product or related documentation  
# may be reproduced, distributed, or decompiled in any form by any means 
# without prior written authorization of MultiMedia Access Corporation. 
# Unauthorized reproduction or distribution of this program, or any part of 
# it, may result in severe civil and criminal penalties, and will be 
# prosecuted to the maximum extent possible under the law.
# 
# MMAC reserves the right to change any products herein at any time and 
# without notice. MMAC does not assume any liability arising from the 
# application or use of this product, except as expressly agreed to in 
# writing by MMAC. Use and purchase of this product does not convey a license
# under any patent rights, copyrights, trademark rights, or any other 
# intellectual property rights of MMAC, Sun Microsystems, or others. 
# 
 
progname=${0##*/}

XILDIR=/opt/SUNWits/Graphics-sw/xil
DESTDIR=/opt/SUNWo1kp/svc
O1KDIR=/opt/SUNWo1kp
verbose=""

USAGE=\
"usage: $progname [ opts ] 
        -v              -- verbose
        -t DESTDIR      -- default is $DESTDIR
        -x XILHOME      -- default is $XILDIR
        -s O1KHOME      -- default is $O1KDIR        
"

fatal() {
    if [ "$1" ]
    then
        echo $* >&2
    fi
    echo "$USAGE" 1>&2
    exit 1
}    

#
# process options
#

while getopts vt:x:s: OPT
do
    case "$OPT" in
	v) verbose="yes";;
        t) DESTDIR="$OPTARG";;
        x) XILDIR="$OPTARG";;
        s) O1KDIR="$OPTARG";;        
        *)  echo $OPTARG
            fatal
    esac
done

[ "$verbose" ] && echo xil directory is $XILDIR
[ "$verbose" ] && echo dest directory is $DESTDIR

if [ "$DESTDIR" = "" ]
then
    echo "DESTDIR is not set"
    exit 2
fi

#
# check to make sure required pkgs installed
#
 
if [ ! -f $XILDIR/lib/libxil.so.1 ]
then
    echo "XIL libraries not found in $XILDIR/lib"
    echo "XIL 1.2 is not on this system"
    echo "This script is applicable for Solaris 2.5.x"
    OS=`uname -r`
    echo "Current OS detected as SunOS $OS"
    exit 2
fi
 
if [ ! -f $O1KDIR/lib/libo1kusr.so ] 
then
    echo "SunVideoPlus libraries not found in $O1KDIR/lib"
    exit 2
fi
    
#
# routine to create sym links 
#

create_link()
{
    if [ -h $2 ]
    then
        echo "Removing previous symlink for $2"
        rm -f $2
    fi
    if [ -f $2 ] || [ -h $2 ]
    then
        echo "$2 already exists, no link done"
    else
        ln -s $1 $2
    fi
}

#
# Remove previous SunVideo Compatibility hierarchy 
#    
if [ -d $DESTDIR ] 
then
    echo "A previous SunVideo Compatibility hierarchy was found at $DESTDIR"
    echo "It needs to be removed before a new install can continue."
    echo "Remove $DESTDIR (y/n)?"
    read answer
    if [ "$answer" != y ]
    then
      echo "Removal not performed. Install can not continue"
      exit 0
    else
      rm -rf $DESTDIR
    fi
fi

#
# create SunVideo Compatibility hierarchy if it doesn't exist
#

if [ ! -d $DESTDIR ]
then
    mkdir $DESTDIR
    cd $DESTDIR
    mkdir lib bin include lib/pipelines
fi

if [ ! -d $DESTDIR/lib ]
then
    cd $DESTDIR
    mkdir lib bin include lib/pipelines
fi

#
# create neccessary sym links
#

echo "Creating SVC links in $DESTDIR from $XILDIR ....."

cd $XILDIR/lib/pipelines || fatal "$XILDIR/lib/pipelines does not exist"
find * -type f -print >/tmp/fxil.$$

cd $DESTDIR/lib/pipelines

create_link $XILDIR/include/xil $DESTDIR/include/xil
create_link $XILDIR/bin/xilcompdesc $DESTDIR/bin/xilcompdesc
create_link $XILDIR/lib/locale $DESTDIR/lib/locale
create_link $XILDIR/lib/libxil.so.1 $DESTDIR/lib/libxil.so.1
create_link $XILDIR/lib/libxil.so $DESTDIR/lib/libxil.so
create_link $XILDIR/lib/libxil_to_xgl.a $DESTDIR/lib/libxil_to_xgl.a
create_link $O1KDIR/lib/libo1kusr.so $DESTDIR/lib/libo1kusr.so
create_link $O1KDIR/lib/libo1kusr.so $DESTDIR/lib/libo1kusr.so.1

for i in `cat /tmp/fxil.$$`
do
   create_link $XILDIR/lib/pipelines/$i ./$i
done

if [ -f $XILDIR/lib/pipelines/xilSUNWrtvc.so.1 ]
then

#
# rename SUNWrtvc .so's 
#

mv xilSUNWrtvc.so.1              xilsvcSUNWrtvc.so.1
mv xilcomputeCellB_SUNWrtvc.so.1 xilsvccomputeCellB_SUNWrtvc.so.1
mv xilcomputeJpeg_SUNWrtvc.so.1  xilsvccomputeJpeg_SUNWrtvc.so.1
mv xilcomputeMpeg1_SUNWrtvc.so.1 xilsvccomputeMpeg1_SUNWrtvc.so.1
mv xilcomputeUYVY_SUNWrtvc.so.1  xilsvccomputeUYVY_SUNWrtvc.so.1

# create links for H261 in svc mode (note that rtvc versions do not really exist)
ln -s xilcomputeH261_MMACo1k.so.1   xilsvccomputeH261_SUNWrtvc.so.1
ln -s xilcomputeH261gx_MMACo1k.so.1 xilsvccomputeH261gx_SUNWrtvc.so.1

fi

#
# create copies to make dlopen happy
#

cp -p xilMMACo1k.so.1     xilSUNWrtvc.so.1
cp -p xilMMACo1k.so.1     ../xilMMACo1k.so.1
cp -p xilsvcSUNWrtvc.so.1 ../xilSUNWrtvc.so.1

#
# set up compatibility links
#

ln -s xilcomputeCellB_MMACo1k.so.1  xilcomputeCellB_SUNWrtvc.so.1
ln -s xilcomputeMpeg1_MMACo1k.so.1  xilcomputeMpeg1_SUNWrtvc.so.1
ln -s xilcomputeUYVY_MMACo1k.so.1   xilcomputeUYVY_SUNWrtvc.so.1
ln -s xilcomputeJpeg_MMACo1k.so.1   xilcomputeJpeg_SUNWrtvc.so.1


# 
# do the links from /usr/openwin if they exist 
# (done only if XILDIR == /opt/SUNWits)
#

OPENWINDIR=/usr/openwin/SUNWits/Graphics-sw/xil/lib/pipelines
if [ ! -d $OPENWINDIR ]
then
   OPENWINDIR=/usr/openwin/etc/devhandlers
fi 

if [ "$XILDIR" = "/opt/SUNWits/Graphics-sw/xil" ]
then
    cd $OPENWINDIR
    find * -type f -print >/tmp/fxil.$$
    for i in `cat /tmp/fxil.$$`
    do
        create_link $OPENWINDIR/$i $DESTDIR/lib/pipelines/$i
    done
fi 
	
rm /tmp/fxil.$$

#
# Now munge in addition xil.compute if desired
#
cd $DESTDIR/lib
rm -f xil.compute
cp -p $XILDIR/lib/xil.compute .

#
# Add in addition Compatibilty dependencies
#
echo "svcSUNWrtvc svcSUNWrtvc" >> xil.compute
echo "svccomputeCellB_SUNWrtvc   svcSUNWrtvc CellB" >> xil.compute
echo "svccomputeJpeg_SUNWrtvc    svcSUNWrtvc Jpeg"  >> xil.compute
echo "svccomputeMpeg1_SUNWrtvc   svcSUNWrtvc Mpeg1" >> xil.compute
echo "svccomputeUYVY_SUNWrtvc    svcSUNWrtvc UYVY"  >> xil.compute

echo "SVC hierarchy creation complete."
echo " - If you have not already setup pseudo svc devices, run: svc_devices install"

echo " - To run in SunVideoPlus SunVideo Compatibility Mode, pre-append "
echo "     $DESTDIR/lib to your LD_LIBRARY_PATH environment variable."
echo "     Also set XILHOME to $DESTDIR"


