#!/usr/bin/perl
use strict;
use otool;


# +-------------------
# | findFileLike(fileName)
# | look at the directory that fileName is in, and see
# | if there's a file that matches that name but MAYBE has
# | less version dots.
# | example, a/b/c/libfoo.1.0.6.dylib might return libff.1.dylib
sub findFileLike($)
{
    my($fileName) = (@_);
    dprint "findFileLike($fileName)\n";

    my $dir = "./";
    if($fileName =~ /^(.*\/)(.*)$/)
    {
        $dir = $1;
        $fileName = $2;
    }

    opendir DIR,$dir;
    my @files = readdir DIR;
    closedir DIR;

    opendir DIR,"../Frameworks/$dir";
    my @files = (@files,readdir DIR);
    closedir DIR;

    foreach my $file (@files)
    {
        my $foundFile = $file;
        while(1)
        {
            if($file eq $fileName)
            {
                dprint "found match $fileName\n";
                return $foundFile;
            }

            if($file =~ /^(.*\.)([0-9]+\.)(.*)$/)
            {
                my $newFile = $1.$3;
                dprint "checking $file -> $newFile\n";
                $file = $newFile;
            }
            else
            {
                last;
            }
        }
    }
    return "";
}

# +----------------
# | lib -- the source library
# | prefix -- the new relative path to libraries, usually ../Framework
# | library -- a dependent library
# | switch -- id or change, depending if its lib, or a ref
# |
sub doNameTool($$$$)
{
    my ($lib,$prefix,$library,$switch) = (@_);

    #print "dnt $library\n";
    if($library =~ /^.*\/(.*?)$/)
    {
        my $reference = $1;
        #print "$library became $reference\n";

        # +-----
        # | Swap it out if and only if it exists locally in the current dir...
        # | Meaning, it's sitting our source dir with all the other
        # | to-be-bundled libraries.
        #while(! (-f $reference))
        #{
            ## also, check by removing parts of the version number, if any
            #if($reference =~ /^(.*)(\.[0-9]+\.)(.*)$/)
            #{
                #my $referenceLess = $1 . $3;
                #print "$reference not here, trying $referenceLess\n";
                #$reference = $referenceLess;
            #}
            #else
            #{
                #last;
            #}
        #}


        $reference = findFileLike($reference);
        dprint "found file like $reference\n" if $reference;
        #if(-f $reference)
        if($reference)
        {
            my $cmd;
            if($switch eq "id")
            {
                $cmd = "install_name_tool -$switch \"$prefix$reference\" \"$lib\"";
            }
            else
            {
                my $newLibrary = $prefix.$reference;
                if($library ne $newLibrary)
                {
                    $cmd = "install_name_tool -$switch \"$library\" \"$prefix$reference\" \"$lib\"";
                }
            }

            if($cmd)
            {
                print "$cmd\n";
                print `$cmd`;
            }
        }
    }
}


sub main($$)
{
    my ($lib,$prefix) = (@_);
    if(!$prefix)
    {
        $prefix = "\@executable_path/../Frameworks/";
    }
    dprint "fixing $lib with $prefix\n";

    my @libraries = getLibraries($lib);

    doNameTool($lib,$prefix,$libraries[0],"id");
    
    for(my $i = 1; $i < scalar(@libraries); $i++)
    {
        doNameTool($lib,$prefix,$libraries[$i],"change");
    }
}


main($ARGV[0],$ARGV[1]);


