#!/bin/sh
#
# ###################################################################
# Copyright (c) 2003, EMC Corporation. All Rights Reserved.
#
# Most Recent Author: Jeff Reagen
# Previous Authors: Raghu Adabala and Ed Goggin
#
# Contents:
# This scripts gets the distributor name and version number of linux
# being used on the current host; these are checked against PowerPaths' 
# list of supported distributors and versions
# ###################################################################
#

# @(#) $Header: /cvs/source/master/pxmobility/src/pkg/i386_linux/bin_install/.options/distribution/.script,v 1.11 2004/03/04 01:09:34 iusvyats Exp $


getDistributions()
{
    distributions=""
    distributions_in=`ls $pp_dir/.options/distribution`
    distribution_count=0
    for i in $distributions_in
    do
	version_count=0
	versions=
	versions_in=`cat $pp_dir/.options/distribution/$i/versions`
	for j in $versions_in
	do
	    if [ "$version_count" -eq 0 ]; then
		eval ${i}_default_version=$j
		versions="$j"
	    else 
		versions="$versions  $j"
	    fi
	    version_count=`expr $version_count + 1`
	done
	versions="$versions"
	eval ${i}_versions='$versions'
	eval ${i}_version_count=$version_count
	if [ "$distribution_count" -eq 0 ]; then
	    distributions="$i"
	else
	    distributions="$distributions  $i"
	fi
	distribution_count=`expr $distribution_count + 1`
    done
    return
}

verify_distribution()
{
    for i in $distributions
    do
	if [ "$i" = "$1" ]; then
	    return
	fi
    done
    echo -e "Your linux distribution is not currently supported."
    exit 1
}

verify_distribution_version()
{
    eval versions='$'${my_distribution}_versions
    for i in $versions
    do
	if [ "$i" = "$2" ]; then
	    return
	fi
    done
    echo -e "This version of your $1 linux distribution is not currently supported."
    exit 1
}

process_distribution()
{    
    getDistributions

    #
    # Query installer about linux distribution and distribution version.
    #

    #
    #  if file redhat-release or SuSE-release exists, then
    #  set myfile equal to that file.
    #
    found=0
    if [ -r $RPM_INSTALL_PREFIX/etc/redhat-release ]; then
	myfile=$RPM_INSTALL_PREFIX/etc/redhat-release
    elif [ -r $RPM_INSTALL_PREFIX/etc/SuSE-release ]; then
	myfile=$RPM_INSTALL_PREFIX/etc/SuSE-release
    elif [ -r $RPM_INSTALL_PREFIX/etc/UnitedLinux-release ]; then
	myfile=$RPM_INSTALL_PREFIX/etc/UnitedLinux-release
    else
    	myfile=$RPM_INSTALL_PREFIX/etc/issue
    fi

    my_distribution=$DEFAULT_DISTRIBUTION
    #echo -e "distributions = $distributions\n"

    ## for SuSE -- /etc/issue uses SLES 8 as the distribution and version
    ##          -- /etc/SuSE-release uses SLES-8 as the distribution and version

    for i in $distributions
    do
        if [ $i = "SLES-8" ];
        then
           grep $i ${myfile} > /dev/null 2>&1
           if [ $? -ne 0 ] ; then
               grep "SLES 8" ${myfile} > /dev/null 2>&1
           fi

	elif [ $i = "RedHat" ];
	then
           grep "Red Hat" ${myfile} > /dev/null 2>&1
           if [ $? -ne 0 ] ; then
               if [ "$rhel30" = "TRUE" ]; then
                   found=1
               elif [ "$rhel21" = "TRUE" ]; then
                   found=1
               else
                  #reset the status flag
                  [ 0 = 1 ]
               fi
           fi
        else
           grep $i ${myfile} > /dev/null 2>&1
        fi

	if [ $? -eq 0 -o "$found" = "1" ]; then
	    my_distribution=$i
	    #echo -e "process_distribution:: Dist $my_distribution\n"
	    break
	fi
    done

    # reset for version check
    found=0


    verify_distribution $my_distribution

    eval versions='$'${my_distribution}_versions
    #echo -e "versions = $versions\n"

    for i in $versions
    do
        if [ $i = "SLES-8" ];
        then
           grep $i ${myfile} > /dev/null 2>&1
           if [ $? -ne 0 ] ; then
               grep "SLES 8" ${myfile} > /dev/null 2>&1
           fi

        elif [ $i = "UnitedLinux-1.0" ];
        then
           grep $i ${myfile} > /dev/null 2>&1
           if [ $? -ne 0 ] ; then
               grep "UnitedLinux 1.0" ${myfile} > /dev/null 2>&1
           fi

        elif [ $i = "RHEL3.0" ];
        then
           if [ "$rhel30" = "TRUE" ]; then
              $found=1
	   else
	      #need to set $? to 1
	      [ 0 = 1 ]
           fi
        else
           grep $i ${myfile} > /dev/null 2>&1
        fi

	if [ $? -eq 0 -o "$found" = "1" ]; then
	    my_distribution_version=$i
	    #echo -e "process_distribution:: Ver $my_distribution_version\n"
	    break
	fi
    done

    verify_distribution_version $my_distribution $my_distribution_version
}

get_distribution_TAG()
{
#    TAG="${TAG}_${my_distribution}${my_distribution_version}"
#    if [ ${my_distribution} = RedHat ]; then
#	if [ ${my_distribution_version} = 6.2 ]; then
#	    TAG=${TAG}_295
#	elif [ ${my_distribution_version} = 2.9.5 ]; then
#            TAG=${TAG}_322
#        elif [ ${my_distribution_version} = "RHEL3.0" ]; then
#            TAG=${TAG}_322
#        else
#	    TAG=${TAG}_296
#        fi
#    elif [ ${my_distribution} = SuSE ]; then
#	if [ ${my_distribution_version} = 7.1 ]; then
#	    TAG=${TAG}_295
#	elif [ ${my_distribution_version} = 7.2 ]; then
#	    TAG=${TAG}_295
#	elif [ ${my_distribution_version} = 7.3 ]; then
#	    TAG=${TAG}_295
#	elif [ ${my_distribution_version} = SLES-7 ]; then
#	    TAG=${TAG}_295
#        elif [ ${my_distribution_version} = SLES-8 ]; then
#            TAG=${TAG}_322
#            TAG=${TAG}_-3.2
#	else
#	    TAG=${TAG}_296
#	fi
#    elif [ ${my_distribution} = UnitedLinux ]; then
#        if [ ${my_distribution_version} = UnitedLinux-1.0 ]; then
#	    TAG=${TAG}_322
#	    TAG=${TAG}_-3.2
#	else
#	    TAG=${TAG}_296
#	fi
#    fi

    . $pp_dir/.options/distribution/$my_distribution/.script
}
