#!/usr/bin/perl
# Copyright (c) 2012 Michael Pyne <mpyne@kde.org>
# cleanup-lib removes old .so files and broken .so symlinks, leaving the latest
# installed .so version installed with a symlink. A personal script so it's
# essentially not commented.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

use strict;
use List::MoreUtils qw/uniq/;
use v5.10;
use Getopt::Long qw(:config posix_default);

my $reallyRemove = 0;
my $helpWanted = 0;

my $result = GetOptions("really-remove" => \$reallyRemove,
    "help|usage" => \$helpWanted);

if ($helpWanted) {
    showHelp();
    exit 1;
}

removeOldLibsInCurrentDir($reallyRemove);

if (!$reallyRemove) {
    say "No files were deleted, Use --help or --usage for help.";
}

exit 0;

# ------- Method implementations

sub showHelp
{
    say <<EOF;
$0: Removes old .so library files in the CURRENT DIRECTORY,
leaving the latest installed version, and removes broken .so symlinks. Doesn't
actually do this unless you pass --really-remove.
EOF
}

sub removeOldLibsInCurrentDir
{
    my $reallyRemove = shift;

    my @libs = <lib*.so>;
    my @goners;

    # Go through symlinks
    for (@libs) {
        next unless -l;
        my @sublibs = ($_, <$_.[0-9]*>);
        for my $sublib (@sublibs) {
            next unless -l $sublib;
            my $dest = readlink ($sublib);
            if (not -e $dest) {
                say "Removing broken symlink $sublib";
                unlink "$sublib" or die $! if $reallyRemove;
            }
        }
    }

    my $size = 0;

    for (@libs) {
        next unless -l;
        my @SOs = sort <$_.*.*>;
        my $link = readlink;
        $link = readlink $link while -l $link;
        @SOs = grep { !/^\Q$link\E/ } (@SOs);

        if ($link =~ /\d\.\d\.\d+$/) {
            my $base = $link;
            $base =~ s/\.\d+$//;
            @SOs = grep { $_ ne $base } (@SOs);
        }

        say "For library $_:";
        say "  Will keep: $link";
        for my $SO (@SOs) {
            say "  Deleting : $SO";
            my $fSize = (stat $SO)[7];
            push @goners, $SO;

            if ($reallyRemove) {
                unlink $SO or die "$!";
            }

            $size += $fSize;
        }
    }

    @goners = uniq(@goners);
    say "Deleted $size bytes";
    say ("Victims:\n", join("\n", @goners)) unless $reallyRemove;
}

# vim: set et sw=4:
