##
## Copyright (C) Centeris Corporation 2004-2007
## Copyright (C) Likewise Software    2007-2008
## All rights reserved.
## 
## This program is free software; you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation; either version 2 of the License, or
## (at your option) any later version.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this program.  If not, see <http:##www.gnu.org/licenses/>.
##

#
# Copyright Centeris Corporation.  All rights reserved.
#
# This short program fixes a problem with the bash command prompt on Red Hat systems
# by installing two prompt files in /etc/sysconfig.
#

use strict;
use warnings;

use FindBin;
use lib "$FindBin::Bin";
use Centeris;

sub main();

exit(main());

sub CreatePromptFile($$$)
{
    my $path = shift || die;
    my $content = shift || die;
    my $opt = shift || die;

    # do not overwrite existing files
    if ( -e $path )
    {
	print "File $path already exists.\n" if $opt->{debug};
	return;
    }

    print "Writing file $path.\n" if $opt->{debug};

    WriteFile( $path, $content ) if (not $opt->{test});

    chmod 0755, $path;

    print "Wrote $path\n";
}

sub CreateSysConfigPromptFiles($)
{
    my $opt = shift || die;

    my $testPrefix = $opt->{testprefix} || '';

    my $sysConfigDir = "$testPrefix/etc/sysconfig";
    my $bash_prompt_xterm_path = "$sysConfigDir/bash-prompt-xterm";
    my $bash_prompt_screen_path = "$sysConfigDir/bash-prompt-screen";

    if (not -d $sysConfigDir)
    {
        return;
    }

    my @bash_prompt_screen_content = ( q(#!/bin/bash), q(echo -n $'\033'"_${USER}@${HOSTNAME%%.*}:${PWD/#$HOME/~}"$'\033\\\\') );
    CreatePromptFile($bash_prompt_screen_path, \@bash_prompt_screen_content, $opt);

    my @bash_prompt_xterm_content = ( q(#!/bin/bash), q(echo -n $'\033'"]0;${USER}@${HOSTNAME%%.*}:${PWD/#$HOME/~}"$'\007'));
    CreatePromptFile($bash_prompt_xterm_path, \@bash_prompt_xterm_content, $opt);
}

sub FixSkelBashRcPrompt($)
{
    my $opt = shift || die;

    my $testPrefix = $opt->{testprefix} || '';

    my $path = "$testPrefix/etc/skel/.bashrc";

    if (not -f $path)
    {
        return;
    }

    # OLD: PROMPT_COMMAND='echo -ne "\033]0;${USER}@${HOSTNAME}: ${PWD/$HOME/~}\007"'
    # NEW: PROMPT_COMMAND=$'echo -n "\033]0;${USER}@${HOSTNAME}: ${PWD/$HOME/~}\007"'

    my $modified;
    my $lines = ReadFile($path);
    map {
        if ($_ =~ /^(\s*)PROMPT_COMMAND=\'echo -ne \"\\033\]0;\$\{USER\}\@\$\{HOSTNAME\}: \$\{PWD\/\$HOME\/~\}\\007\"\'(\s*)$/)
        {
            my $pre = $1 || '';
            my $post = $2 || '';
            my $replace = "PROMPT_COMMAND=\$\'echo -n \"\\033\]0;\$\{USER\}\@\$\{HOSTNAME\}: \$\{PWD\/\$HOME\/~\}\\007\"\'";
            $_ = $pre.$replace.$post;
            $modified = 1;
        }
    } @$lines;

    if ($modified)
    {
        ReplaceFile($path, $lines);
    }
}

sub usage()
{
    use Centeris;

    $0 = QuickBaseName($0);
    return <<DATA;
usage: $0 [options]

  options (case-sensitive):

    --testprefix <prefix>   Prefix for test directory
    --debug                 Print debugging messages
    --test                  Run but do not change anything
DATA
}

sub main()
{
    use Centeris;
    use Getopt::Long;

    Getopt::Long::Configure('no_ignore_case', 'no_auto_abbrev') || die;

    my $opt = {};
    my $ok = GetOptions($opt,
			'help|h|?',
                        'testprefix=s',
                        'test',
                        'debug'
                       );

    if ( not $ok or
         $opt->{help} or
         ( $#ARGV != -1 ) )
    {
	die usage();
    }

    my $testPrefix = $opt->{testprefix} || '';
    CheckTestPrefix($testPrefix);

    my $osType = GetOsType($testPrefix);
    my $distroType = GetDistroType($testPrefix);
    my $distroVersion = GetDistroVersion($testPrefix);

    my $do_distro;
    if ($osType eq 'Linux')
    {
        if ( $distroType eq 'rhel' || $distroType eq 'centos' || $distroType eq 'fedora' )
        {
            $do_distro = 1;
            CreateSysConfigPromptFiles($opt);
        }
        elsif ( $distroType eq 'ubuntu' )
        {
            $do_distro = 1;
            FixSkelBashRcPrompt($opt);
        }
    }

    if (not $do_distro)
    {
	print "No prompt modifications are needed for $distroType distribution.\n" if ( $opt->{debug} );
    }

    return 0;
}
