#!/bin/bash
if [ $# -lt 4 ] ; then
    echo "Usage: $0 prod_name prod_dir list_entries local_prod [patch_mode]"
    exit 1
fi
prod_name=$1
prod_dir=$2
list_entries=$3
local_prod=$4
patch_mode=$5
# DEBUG HERE
if [ "$DEBUG" = "Y" ] ; then
    echo "DEBUG INFO:"
    echo "prod name=$prod_name"
    echo "prod_dir=$prod_dir"
    echo "list_entries=$list_entries"
    echo "local_prod=$local_prod"
    echo "patch_mode=$patch_mode"
fi
# DEBUG END
PROD_DIR=/exlibris/product
pushd `dirname $0` > /dev/null
basedir=$PWD
popd > /dev/null
if [ ! -d $PROD_DIR ] ; then
    mkdir -p -m 775 $PROD_DIR/bin $PROD_DIR/local
fi
if [ ! -w $PROD_DIR ] ; then
    echo "$PROD_DIR is not writable or could not be created - please change permissions"
    echo "patch failed"
    exit 1
fi
echo "$PROD_DIR writable [OK]"
os_name=`uname`
if [ "$os_name" = "SunOS" -a "`uname -p`" = "i386" ] ; then
    os_name="SunOS.i386"
fi
pack_name="${prod_name}.$os_name.tar.gz"
if [ ! -f $pack_name ] ; then
    if [ -f ${prod_name}.tar.gz ] ; then
        pack_name="${prod_name}.tar.gz"
    else
        echo "$pack_name not found - aborting"
        exit 1
    fi
fi
if [ -f ${prod_name}.`uname`.`uname -p`.tar.gz ] ; then
    pack_name="${prod_name}.`uname`.`uname -p`.tar.gz"  # For Linux x86_64 packages
fi

echo "$pack_name found"
cd $PROD_DIR
if [ -d $prod_dir -a "$patch_mode" != "R" ] ; then
    echo "$prod_dir already exists in $PROD_DIR"
    else
    if [ -d $prod_dir ] ; then
        echo "Backing up old package"
        mv $prod_dir $prod_dir.`date +%y%m%d%H%M%S`
    fi
    echo "Extracting $pack_name..."
    tar zxf $basedir/$pack_name
    if [ $? != 0 ] ; then
        echo "Error extracting $prod_name"
        exit 1
    fi
fi

IFS=:
for entry in $list_entries ; do
    file=`echo $entry | cut -d, -f1`
    link=`echo $entry | cut -d, -f2`
    if [ "$link" = "" ] ; then
        continue
    fi
    echo "checking link $link to $file"
    if [ ! -e $file ] ; then
        echo "Could not find $PROD_DIR/$file"
        echo "patch failed"
        exit 1
    fi
    if [ -e $local_prod/$link  -o  -L $local_prod/$link ] ; then
        if [ -L $local_prod/$link ] ; then
            oldlink=`ls -l $local_prod/$link | sed 's/.*-> //'`
            if [ "$oldlink" = "$PROD_DIR/$file" ] ; then
                echo "correct link already exists"
                continue
            fi
        fi
        echo "Backing up existing link"
        mv $local_prod/$link $local_prod/$link.`date +%y%m%d%H%M%S`
    fi
    echo "linking $PROD_DIR/$file $local_prod/$link"
    dirname=`dirname $link`
    if [ ! -d  $local_prod ] ; then
        
        mkdir -p $local_prod && echo $local_prod created || exit 1
    fi
echo "local product is: $local_prod"
    pushd  $local_prod /dev/null
    IFS="/"
    for d in $dirname ; do
        if [ -L $d ] ; then
            mv $d $d.bak  && echo "Soft link $d moved to $d.bak"
        fi
        if [ ! -d $d ] ; then
            mkdir $d || (echo "failed to create $d" ;  exit 1)
            echo $d created
        else
            echo "$d exists"
        fi
        cd $d
    done
    popd >/dev/null
    unset IFS

    ln -s $PROD_DIR/$file $local_prod/$link
    grep "$entry" $PROD_DIR/linklist > /dev/null 2>&1
    if [ $? != 0 ] ; then
        echo $entry >> $PROD_DIR/linklist
        echo "$entry added to linklist"
    fi
    
done
