#######################################
## Do Not Edit
## san2(at)linuxchannel.net
########################################
##
## misc.lib

get_filename() {

    RDIR="$1"
    SFILE="^${2}(.+)\.(tar\.gz|tgz|tar\.Z)$"
    LFILEnum=`ls $RDIR | egrep "$SFILE" | wc -l 2> /dev/null`
    LFILE=`ls $RDIR | egrep "$SFILE" 2> /dev/null`

    if [ -s "${RDIR}/${LFILE}" -a $LFILEnum -eq 1 ] ; then
        at_least="1"
        if [ "$2" = "php-" ] ; then
            foundphp="$LFILE"
        else
            eval found"$2"="$LFILE"
        fi
    else
        if [ "$2" = "php-" ] ; then
            foundphp="0"
        else
            eval found"$2"="0"
        fi
    fi

    ## Debugging
    ##
    #echo "\$1=$1 \$2=$2 \$foundapache=$foundapache"
}

get_version() {

    re=`echo "$AAA" | awk -F".tar.*" '{print $1}'`
}

addfile() {
    echo "${T_MD}$1${T_ME}"
    echo "$1" >> $2
}

process() {
        if [ "$1" = "" ] ; then
          Q="Are you ready"
        else
          Q="$1"
        fi

        echo ""
        echo -n "${T_MG}$Q ?${T_ME} (${T_MD}[Enter]${T_ME} is default : \"${T_MD}Yes${T_ME}\") [Yes/No/Stop]: "
        read P1

        if [ "$P1" = "" ] ; then
          P1="Y"
        fi

    case "$P1" in
        s*|S*)
            init_flags
            echo "${T_MR}Stopped !!!${T_ME}"
            exit 0
            ;;
        n*|N*)
            if [ "$2" != ""  ] ; then
                eval skip${2}="1"
            fi
            ;;
    esac
}

pid_notice() {
        echo ""
        echo "***If MySQLd is running, Answer \"Stop\", then \"${T_MD}killall mysqld${T_ME}\""
        echo "***If Apache is running, Answer \"Stop\", then \"${T_MD}killall httpd${T_ME}\""
        echo "***And, retry to \"${T_MD}./install-sh${T_ME}\""
}


sub_explain() {
    echo ""
    echo "(+) : Default support(include)"
    echo "(-) : Support, but Options"
    echo "(X) : NOT support, I'll futhure support"
}

## OK, source remove
##
source_remove() {
    srcinstalldone="$1"
    srcdir="$2"
    skiprmsrcdir="0"
    if [ "$srcinstalldone" = "1" ] ; then
        echo ""
        process "Remove Source ${T_MD}$srcdir${T_ME} directory" "rmsrcdir"
        if [ "$skiprmsrcdir" != "1" ] ; then
            rm -rf "$srcdir"
            addfile "Removed Source $srcdir directory" $DONEFILE
        fi
    else
        # force remove
        if [ -d "$srcdir" ] ; then
            rm -rf "$srcdir"
        fi
    fi
}

check_old_src() {
    old_src="$1"
    if [ -d "$old_src" ] ; then
        echo "Old SRC rm -rf ${T_MD}$old_src${T_ME}"
        rm -rf "$old_src"
    fi
}

check_user_root() {
    if [ `whoami 2>/dev/null` != "root" ] ; then
        echo "${T_MR}Error!!! change ${T_MRU}root${T_ME}${T_MR} privileges !!!!${T_ME}"
        exit 0
    fi
}

check_work_log_dir() {

    if [ ! -d $LOGDIR ] ; then
        mkdir -p $LOGDIR
        chmod 705 $LOGDIR
    fi

    if [ ! -d $WORK ] ; then
        mkdir -p $WORK
        chmod 705 $WORK
        echo "-------------------------------------------------"
        echo "[$WORK] directory NOT found"
        echo "so, created [$WORK]"
        echo "-------------------------------------------------"
        echo "Require file !!! in ${T_MD}[$WORK]${T_ME}"
        echo " (-)mysql-xxx.tar.gz (Options)"
        echo " (+)apache_xxx.tar.gz (Required)"
        echo " (+)php-xxx.tar.gz (Required)"
        echo " (-)mod_ssl-xxx.tar.gz (Options)"
        echo " (-)imap-xxx.tar.Z (Options)"
        echo " (-)ZendOptimizer-xxx.tar.gz (Options)"
        echo " (-)openssl-xxx.tar.gz (Options)"
        echo " (-)php-accelerator-xxx.tgz (Options)"
        echo "-------------------------------------------------"

        echo "${T_MR}Require : ${WORK} not found${T_ME}"
        exit 0
    fi
}

check_contine_exit() {
    process "But, Are you continue next" "continue"
    ## answer is No
    ##
    if [ "$skipcontinue" = "1" ] ; then
        exit 0
    fi
}

check_at_least() {
    AT_LEAST="$1"
    if [ "$AT_LEAST" != "1" ] ; then
        echo "${T_MR}Error !!! Not found any tar balls in ${WORK} !!!${T_ME}"
        exit 0
    fi
}

## add 200.04.01
##
check_dir_mv() {
    if [ -d "$1" ] ; then
        mv -f "$1" "${1}-${MYDATE}"
        addfile "" $DONEFILE
        addfile "Moved $1 --> ${1}-${MYDATE}" $DONEFILE
        addfile "" $DONEFILE

        if [ X"$2" != "X" ] ; then
            eval "$2"=1
        fi
    fi
}

usage() {
    echo "usage : $0 [options]"
    echo "options :"
    echo "  --makekey, -makekey"
    echo "         make Apache/mod_ssl KEY(3)"
    echo "  --targetarch[=CPU], -targetarch[=CPU]"
    echo "         force make package for target system. CPU is target system CPU ARCH"
    echo "         this option are using at the 'CFLAGS'(-march=CPU)."
    echo "         if value is not given, default is 'auto'"
    echo "         ex) auto, i386, i486, i586, i686"
    echo "         see bellow 'CPU :'"
    echo "  --buildcpu[=CPU], -buildcpu[=CPU]"
    echo "         force make build, by current(self) system CPU ARCH"
    echo "         this option are using at the 'CFLAGS'(-mcpu=CPU)."
    echo "         if value is not given, default is 'auto'"
    echo "         ex) auto, i386, i486, i586, i686"
    echo "         see below 'CPU :'"
    echo "  --help, -help, -h"
    echo "         help, this screen help"
    echo "CPU :"
    echo "  auto   current system CPU ARCH(${T_MD}${AUTO_ARCH}${T_ME})"
    echo "  i386   for i386 machines"
    echo "  i486   for i486 machines"
    echo "  i586   for generic Pentium/Pentium MMX, celeron, AMD K6-2/3 machines"
    echo "  i686   for generic P-Pro/II/III/IV and AMD Athlon-class(include XP) machines"
    exit 0
}

check_arch() {
    ## $1(arch) $2(user input argument), $3(type var)
    ##
    if [ X$(echo "$1" | egrep "^(i[3456]86|auto)$") = "X" ] ; then
        echo "${T_MR}Error !!! invalid '$2' option${T_ME}"
        usage
        exit 0
    fi
    if [ "$1" = "auto" ] ; then
        eval ${3}_MARCH="$AUTO_ARCH"
    fi
    if [ "$3" = "BUILD" ] ; then
        if [ $(echo "$BUILD_MARCH" | sed 's/[a-z]//g') -gt $(echo "$AUTO_ARCH" | sed 's/[a-z]//g') ] ; then
            echo "${T_MR}Error !!! CPU ARCH over '$1', recommend : ${T_MRU}${AUTO_ARCH}${T_ME}"
            usage
            exit 0
        fi
    fi
}

## add 2002.03.30
##
check_argument_flags() {

    for ARGS in $1 ; do
        case "$ARGS" in
            *makekey)
                process "Now, Re Make Apache/mod_ssl KEY(3)" "makemodsslkey"
                if [ "$skipmakemodssleky" != "1" ] ; then
                    make_sh_apachesslkey
                fi
                exit 0
                ;;
            *targetarch*)
                USE_MARCH="$ARGS"
                TARGET_MARCH=`echo "$ARGS" | awk -F"=" '{print ($2)}' | sed 's/[^a-z0-9]//g'`
                TARGET_MARCH=${TARGET_MARCH:-${AUTO_ARCH}}
                ;;
            *buildcpu*)
                USE_BUILDCPU="$ARGS"
                BUILD_MARCH=`echo "$ARGS" | awk -F"=" '{print ($2)}' | sed 's/[^a-z0-9]//g'`
                BUILD_MARCH=${BUILD_MARCH:-${AUTO_ARCH}}
                ;;
            -h*|--h*)
                usage
                exit 0
                ;;
        esac
    done

    if [ X"$USE_BUILDCPU" != "X" ] ; then
        check_arch "$BUILD_MARCH" "$USE_BUILDCPU" "BUILD"
        MCPU="-mcpu=${BUILD_MARCH}"
        MARCH="-march=${AUTO_ARCH}"
    fi

    if [ X"$USE_MARCH" != "X" ] ; then
        check_arch "$TARGET_MARCH" "$USE_MARCH" "TARGET"
        MARCH="-march=${TARGET_MARCH}"

        ## make install root diretory
        ##
        #INSTALL_ROOT="${BASE}/install_root"
        #INSTALL_OPT="root=$INSTALL_ROOT"
        #rm -rf "$INSTALL_ROOT"
        #mkdir -p "$INSTALL_ROOT"
    fi

    ## gentoo linux : CFLAGS, CXXFLAGS, FFLAGS
    ## "-mcpu=i686 -march=i686 -O3 -pipe"
    ##
    if [ X"$MARCH" != "X" ] ; then
        CFLAGS="${MCPU} ${MARCH} -O3 -pipe"
        CXXFLAGS="$CFLAGS"
        FFLAGS="$CFLAGS"
        export CFLAGS CXXFLAGS FFLAGS
    fi
}

init_flags() {
    export CFLAGS='' CXXFLAGS='' FFLAGS=''
}


## source : configure of mod_ssl-xxx.tar.gz
## some optional terminal sequences
##
#case $TERM in
#    xterm|xterm*|vt220|vt220*)
#        T_MD=`echo dummy | awk '{ printf("%c%c%c%c", 27, 91, 49, 109); }'`
#        T_ME=`echo dummy | awk '{ printf("%c%c%c", 27, 91, 109); }'`
#        ;;
#    vt100|vt100*)
#        T_MD=`echo dummy | awk '{ printf("%c%c%c%c%c%c", 27, 91, 49, 109, 0, 0); }'`
#        T_ME=`echo dummy | awk '{ printf("%c%c%c%c%c", 27, 91, 109, 0, 0); }'`
#        ;;
#    default)
#        T_MD=''
#        T_ME=''
#        ;;
#esac

## change 2000.02.30
## set color
## source '/etc/sysconfig/init'
##
case $TERM in
    xterm|xterm*|vt220|vt220*|linux)
        T_MG=`echo -e "\\033[1;32m"`  ## bold green
        T_MB=`echo -e "\\033[1;34m"`  ## bold blue
        T_MR=`echo -e "\\033[1;31m"`  ## bold red
        T_MD=`echo -e "\\033[1;39m"`  ## bold white
        T_ME=`echo -e "\\033[0;39m"`  ## normal white
        T_MGU=`echo -e "\\033[4;32m"` ## underline green
        T_MBU=`echo -e "\\033[4;34m"` ## underline blue
        T_MRU=`echo -e "\\033[4;31m"` ## underline red
        T_MDU=`echo -e "\\033[4;39m"` ## underline white
        ;;
    default)
        T_MG=''
        T_MB=''
        T_MR=''
        T_MD=''
        T_ME=''
        T_MGU=''
        T_MBU=''
        T_MRU=''
        T_MDU=''
        ;;
esac

## default bold font color
##
T_MD="$T_MB"
T_MDU="$T_MBU"