#!/bin/sh
#

prefix=@prefix@
suppbindir=@prefix@/lib/mc/bin

do_compat_replace () {
    cp $1 $1.orig
    if test "$?" != "0"; then
	echo "Command \`cp $1 $1.orig' failed. File $1 not changed!"
	return 1
    fi
    sed '/^mc ()/ {
     # Add comment
     i\
#Commented out by mc_fninstall. Source @prefix@/lib/mc/bin/mc.sh instead.
}

/^mc ()[^}]*$/,/}[; ]*$/{
    # Function definition over several lines
    s/^\(.*\)$/#\1/g
}

/^mc ().*}.*$/{
    # Function definition on one line
    s/^\(.*\)$/#\1/g
}' < $1.orig > $1

    echo >> $1
    echo ". $suppbindir/mc.sh" >> $1
    echo ""
    echo "I commented out the mc () function definitions in $1 and added"
    echo "\`. $suppbindir/mc.sh'. Your previous profile has been saved as"
    echo "$1.orig."
}

if test -n `echo $prefix | grep prefix`; then
    prefix=/usr/local
fi
if test x$BASH = x; then
    BASHRC=
else
    BASHRC=~/.bashrc
fi
if test "x$EUID" = x0; then
    PROFILE=/etc/profile
else
    PROFILE=~/.profile
fi
if test -f $PROFILE; then
    A=`grep "mc ()" $PROFILE`
    B=
    if test -n "$BASHRC"; then
	if test -f $BASHRC; then
	    B=`grep "mc ()" $BASHRC`
	fi
    fi

    if test -n "$A" -o -n "$B"; then
	if test -n "$B"; then
	    echo "While examining your $PROFILE and $BASHRC,"
	else
            echo "While examining your $PROFILE,"
	fi
	echo "I've found that you have a probably outdated mc () function definition"
        echo "there. Do you want to replace this function definition with "
        echo "        . $suppbindir/mc.sh"
        read rep
        case $rep in
	    [Yy]*) ;;
	    *) exit ;;
        esac
        if test -n "$A"; then
		do_compat_replace $PROFILE
        fi
        if test -n "$B"; then
		do_compat_replace $BASHRC
        fi

	exit 0
    fi
    unset -f do_compat_replace

    # This doesn't handle the case that $prefix changed 
    A=`grep "/mc.sh" $PROFILE`
    B=
    if test -n "$BASHRC"; then
	if test -f $BASHRC; then
	    B=`grep "/mc.sh" $BASHRC`
	fi
    fi
    if test -n "$A"; then
	:
    else 
        if test -n "$B"; then
	    :
	else
            A=`typeset -f | grep "mc ()" 2>/dev/null`
	    if test ! -n "$A"; then
	        echo "mc () installation."
		if test -n "$BASHRC"; then
		    echo "While examining your $PROFILE and $BASHRC,"
		else
	            echo "While examining your $PROFILE,"
		fi
		echo "I've found that you do not source $suppbindir/mc.sh there."
		echo "This shell script defines a function which enables a feature of mc(1)"
                echo "that when you leave mc(1), you return to a directory where you were"
                echo "in mc just before exiting and not to the directory you've started mc"
                echo "from."
	        echo "Would you like to append"
	        echo "test -f $suppbindir/mc.sh && . $suppbindir/mc.sh"
		if test -n "$BASHRC"; then
	            echo "to your (p) $PROFILE (mc function will be active in your login shells)"
		    echo -n "or to your (b) $BASHRC (in every bash instance) or (n) no? [p|b|n] "
		else
	            echo -n "function to your $PROFILE? [y|n] "
		fi
	        read rep
		if test -n "$BASHRC"; then
		    INITFILE=$BASHRC
		else
		    INITFILE=$PROFILE
		fi
	        case $rep in
		    [Yy]*) ;;
		    [Pp]*) INITFILE=$PROFILE ;;
                    *) exit 1;;
	        esac
                echo >>$INITFILE
	        echo 'test -f $suppbindir/mc.sh && . $suppbindir/mc.sh' >>$INITFILE
	        echo "mc () function appended to your $INITFILE"
	    fi
        fi 
    fi
fi