#!/bin/sh
set -e

. $KUMATUXBIN/kumatux.org-vars

for SEARCH1 in lib lib64 share
do
    SEARCH="$SEARCH1/perl5"
    ROOTDIR=$KUMATUXROOTPERL
    ETCDIR=$KUMATUXETCPERL

    ## check whether app already linked
    LINKED=`$KUMATUXFIND/linked-allready.sh $1 $ROOTDIR $SEARCH`
    if [ "$LINKED" ]
    then
    ## don't link
	echo "$1/$SEARCH linked already in $LINKED"
    else
    ## link
        $KUMATUXLINK/link-line.sh $1 $SEARCH $ETCDIR $ROOTDIR
    fi

    SEARCH="$SEARCH1/perl5/site_perl"

    ## check whether app already linked
    LINKED=`$KUMATUXFIND/linked-allready.sh $1 $ROOTDIR $SEARCH`
    if [ "$LINKED" ]
    then
    ## don't link
        echo "$1/$SEARCH linked already in $LINKED"
    else
    ## link
        $KUMATUXLINK/link-line.sh $1 $SEARCH $ETCDIR $ROOTDIR
    fi
done

#PERLVERSION=`perl --version 2>/dev/null | grep "This is perl" | sed 's/(//g' | sed 's/)//g' | sed 's/This is perl.* v\(.*\) built.*/\1/' | sed 's/ .*//' | sed 's/\(....\)\..*/\1/'`
PERLVERSIONFULL=`perl --version 2>/dev/null | grep "This is perl" | sed 's/(//g' | sed 's/)//g' | sed 's/This is perl.* v\(.*\) built.*/\1/' | sed 's/ .*//'`
PERLVERSION=`echo $PERLVERSIONFULL | sed 's/\(....\)\..*/\1/'`
for SEARCH in "share/perl/$PERLVERSION" "share/perl/$PERLVERSIONFULL" "lib/$PERLVERSION" "lib/$PERLVERSIONFULL" "lib/site_perl/$PERLVERSION" "lib/site_perl/$PERLVERSIONFULL"
do
    ## check whether app already linked
    LINKED=`$KUMATUXFIND/linked-allready.sh $1 $ROOTDIR $SEARCH`
    if [ "$LINKED" ]
    then
        ## don't link
	echo "$1/$SEARCH linked already in $LINKED"
    else
        ## link
	$KUMATUXLINK/link-line.sh $1 $SEARCH $ETCDIR $ROOTDIR
    fi
done

APPNAME=`$KUMATUXBASENAME $1 | sed 's/_.*//'`
if [ $APPNAME = "perl" ]
then
    echo "Symlinking $1/bin/perl to $KUMATUXBIN/..."
    ln -s $1/bin/perl $KUMATUXBIN/
    echo "Symlinked."
fi
