#!/usr/bin/perl

# cpanel - autofixer/pureauth                     Copyright(c) 2013 cPanel, Inc.
#                                                           All rights Reserved.
# copyright@cpanel.net                                         http://cpanel.net
# This code is subject to the cPanel license. Unauthorized copying is prohibited

# This script will add symlinks to pureauth on 11.36 if the pure-ftpd is blocked from upgrades.

use strict;

my $rpm_q = `rpm -q pure-ftpd`;
exit if ( $rpm_q =~ m/package .* is not installed/ms );    # Skip if pureauth isn't there.
exit if ( $rpm_q =~ m/\.cp\d\d\d\d/ms );                   # Don't Fix pureauth if the cp1136 RPM is installed.

# Create the symlinks if they don't point to the right place.
foreach my $link (qw{ /usr/sbin/pureauth /usr/local/sbin/pureauth }) {
    next if ( -l $link && ( readlink($link) eq '/usr/local/cpanel/bin/pureauth' ) );

    if ( -e $link ) {
        warn "File $link is being replaced with a symlink to /usr/local/cpanel/bin/pureauth.";
        if ( !unlink($link) ) {
            warn "Failed to unlink file: $link";
        }
    }
    symlink( '/usr/local/cpanel/bin/pureauth', $link );
}
