#! /bin/sh 
########################################################################
#
# /u/sonmi/bin/qa_stage - /u/svbld/bin/init/nss/qa_stage
#
# this script is supposed to convert the tinderbox and daily QA files
# for use on mozilla.org
#
# parameters
# ----------
#   nssversion  (supported: 30b, 31, tip)
#   builddate   (default - today)
#
########################################################################

if [ -z "$BUILDNUMBER" ]
then
    BUILDNUMBER=1
fi
if [ `uname` = "Linux" ] ; then
    PATH=".:/u/sonmi/bin:/u/sonmi/bin/linux:/usr/bsd:/usr/ucb/:/bin:/usr/bin:/usr/ccs/bin:/usr/sbin:/usr/bin/X11:/usr/etc:/etc:/usr/demos:/usr/demos/bin:/usr/local/bin:/usr/local/X11/bin:/tools/ns/bin"
    export PATH
fi

Echo()
{
    if [ "$O_SILENT" = "OFF" ] ; then
        echo $*
    fi
}

################################### qa_stage_init ##########################
# 
########################################################################
qa_stage_init()
{
    umask 000

    eval_opts $*

    if [ -z "${QAYEAR}" ] ; then 
        QAYEAR=`date +%Y`
    elif [ "$QAYEAR" = ""  ] ; then
        QAYEAR=`date +%Y`
    fi

    Echo "Init..."
    DAYBUILD=${QAYEAR}${BUILDDATE}.${BUILDNUMBER}
    NSS_D0=/share/builds/mccrel3/nss
    NSS_VER_DIR=${NSS_D0}/nss${NSSVER}
    NTDIST=${NSS_VER_DIR}/builds/${DAYBUILD}/blowfish_NT4.0_Win95/mozilla/dist
    UXDIST=${NSS_VER_DIR}/builds/${DAYBUILD}/booboo_Solaris8/mozilla/dist
    TESTSCRIPTDIR=${NSS_VER_DIR}/builds/${DAYBUILD}/booboo_Solaris8/mozilla/security/nss/tests
    RESULTDIR=${NSS_VER_DIR}/builds/${DAYBUILD}/booboo_Solaris8/mozilla/tests_results/security
    TBX_RESULTDIR=${NSS_D0}/nsstip/tinderbox/tests_results/security

    MOZ_D0=/pub/security/nss
    MOZ_RESULTDIR=${MOZ_D0}/daily_qa/${DAYBUILD}
    MOZ_TBX_RESULTDIR=${MOZ_D0}/tinderbox
    
    export BUILDDATE NSSVER QAYEAR NTDIST UXDIST TESTSCRIPTDIR RESULTDIR 


    IPLANET_TBX_URL="http://cindercone.red.iplanet.com${TBX_RESULTDIR}"
    IPLANET_DQA_URL="http://cindercone.red.iplanet.com${RESULTDIR}"

    MOZ_TBX_URL="ftp://ftp.mozilla.org${MOZ_TBX_RESULTDIR}"
    MOZ_DQA_URL="ftp://ftp.mozilla.org${MOZ_RESULTDIR}"

    export IPLANET_TBX_URL IPLANET_DQA_URL MOZ_TBX_URL MOZ_DQA_URL
    STAGE_1=/u/sonmi/tmp/ftp_stage
    
    if [ ! -d  $STAGE_1 ] ; then
        Echo "Staging area daily QA (DQA): $DQA_STAGE does not exist, exit"
        exit 1
    fi
    cd $STAGE_1 || (Echo "Cant cd to $STAGE_1 , exit"; exit)
    rm all.tar* 2>/dev/null
    TBX_STAGE=$STAGE_1/tinderbox
    DQA_STAGE=$STAGE_1/daily_qa/${DAYBUILD}
    Echo "Staging area tbx: $TBX_STAGE"
    Echo "Staging area daily QA (DQA): $DQA_STAGE"
    Echo "Resultdir (sourcedir) for daily QA (RESULTDIR): $RESULTDIR"
}

################################### qa_stage_dqa ##########################
# 
########################################################################
qa_stage_dqa()
{
    Echo "DQA:..."
    Echo "Resultdir (sourcedir) for daily QA (RESULTDIR): $RESULTDIR"
#set -x
    if [ ! -d  $RESULTDIR ] ; then
        Echo "Resultdir $RESULTDIR does not exist, can't push daily QA"
        return
    fi
    cd $RESULTDIR || return
    #for w in `find . -name "result*html"`
    for w in `find . -name "result.html"`
    do
        if [ ! -d $DQA_STAGE/`dirname $w` ] ; then
            mkdir -p $DQA_STAGE/`dirname $w`
        fi
        rm $DQA_STAGE/$w 2>/dev/null
        cat $w | reformat_qa >$DQA_STAGE/$w
    done
    for w in `find . -name "output.log" -o  -name "results.html"`
    do
#echo $w
        if [ ! -d $DQA_STAGE/`dirname $w` ] ; then
            mkdir -p $DQA_STAGE/`dirname $w`
        fi
        cp $w $DQA_STAGE/$w
    done
}


################################### qa_stage_tbx ##########################
# 
########################################################################
qa_stage_tbx()
{
    Echo "tbx: "
    if [ ! -d  $TBX_RESULTDIR ] ; then
        Echo "TBX_RESULTDIR $TBX_RESULTDIR does not exist"
        return
    fi
    cd $TBX_RESULTDIR || return
    Echo "find from $TBX_FIND_FROM"
    for w in `find $TBX_FIND_FROM  -name "result.html"`
    do
        if [ ! -d "$TBX_STAGE/`dirname $w`" ] ; then
            mkdir -p $TBX_STAGE/`dirname $w`
        fi
        rm $TBX_STAGE/$w 2>/dev/null
        cat $w | reformat_qa >$TBX_STAGE/$w
    done
    for w in `find $TBX_FIND_FROM -name "output.log" -o  -name "results.html"`
    do
        if [ ! -d $TBX_STAGE/`dirname $w` ] ; then
            mkdir -p $TBX_STAGE/`dirname $w`
        fi
        cp $w $TBX_STAGE/$w
    done
}

match_tbxdirs()
{
    YY=`date +%Y`
    DD=`date +%d`
    MM=`date +%m`
    HH=`date +%H`

    TBX_FIND_FROM="*-$YY$MM$DD-$HH.*"
    i=$1
    while [ $i -gt 0 ] ; do
        i=`expr $i - 1`
        HH=`expr $HH - 1`
        if [ $HH -lt 0 ] ; then
            HH=23
            DD=`expr $DD - 1`
            if [ $DD  -eq 0 ] ; then
                MM=`expr $MM - 1`
                case $MM in
                    0)
                        YY=`expr $YY - 1`
                        MM=12
                        DD=31
                        ;;
                    [13578]|10|12)
                        DD=31
                        ;;
                    2)
                        DD=28
                        ;;
                    [469]|11)
                        DD=30
                        ;;
                esac
            fi
        fi
        case $MM in
            [123456789])
               MM=0$MM
               ;;
        esac
        case $DD in
            [123456789])
               DD=0$DD
               ;;
        esac
        case $HH in
            [0123456789])
               HH=0$HH
               ;;
        esac
        TBX_FIND_FROM="$TBX_FIND_FROM *-$YY$MM$DD-$HH.*"
    done
}

################################### eval_opts ##########################
# global shell function, evapuates options and parameters, sets flags
# variables and defaults
########################################################################
eval_opts()
{
  DO_TBX=OFF
  DO_DQA=OFF
  DO_CLEAN=OFF
  O_SILENT=OFF
  O_INCREMENTAL=OFF
  O_MAIL=OFF
  BUILDDATE=`date +%m%d`
  NSSVER=tip

  TBX_FIND_FROM="."

  while [ -n "$1" ]
  do
    case $1 in
        -d)
            DO_DQA=ON
            ;;
        -m)
            O_MAIL=ON
            shift
            MAILINGLIST=$1
            if [ -z "$MAILINGLIST" ]
            then
                echo "Error: -m requires a mailinglist to follow, for example sonmi@iplanet.com"
                exit
            fi
            ;;
        -ti)
            DO_TBX=ON
            match_tbxdirs 2
            O_INCREMENTAL=ON
            ;;
        -t)
            DO_TBX=ON
            ;;
        -c)
            DO_CLEAN=ON
            ;;
        -s)
            O_SILENT=ON
            ;;

        tip|3[0-9]*)
            NSSVER=$1
            ;;
        [01][0-9][0-3][0-9])
            BUILDDATE=$1
            ;;
    esac
    shift
  done
}

qa_stage_init $*

if [ "$DO_CLEAN" = "ON" ] ; then
    Echo "Cleaning old stuff"
    if [ ! -d  $STAGE_1 ] ; then
        Echo "Staging area daily QA (DQA): $DQA_STAGE does not exist, exit"
        exit 1
    fi
    cd $STAGE_1 || (Echo "Cant cd to $STAGE_1 , exit"; exit)
    if [ -n "$TBX_STAGE" -a -d "$TBX_STAGE" ] ; then
        rm -rf $TBX_STAGE/*
    else
        Echo "nothing here to clean..."
    fi
fi
if [ "$DO_DQA" = "ON" ] ; then
    qa_stage_dqa 
    if [ "$O_MAIL" = "ON" -a -f "$DQA_STAGE/result.html" ] ; then
         cat $DQA_STAGE/result.html | /usr/sbin/sendmail $MAILINGLIST 
    fi
fi
if [ "$DO_TBX" = "ON" ] ; then
    qa_stage_tbx
fi
if [ ! -d  $STAGE_1 ] ; then
    Echo "Staging area daily QA (DQA): $DQA_STAGE does not exist, exit"
    exit 1
fi
cd $STAGE_1 || (Echo "Cant cd to $STAGE_1 , exit"; exit)
Echo "tar..."
if [ "$O_SILENT" = "ON" ] ; then
    TARPARAM=cf
else
    TARPARAM=cvf
fi


if [ "$DO_DQA" = "ON" -a "$DO_TBX" = "ON" ] ; then
        Echo "tar $TARPARAM all.tar daily_qa        tinderbox"
        tar $TARPARAM all.tar daily_qa        tinderbox
elif [ "$DO_DQA" = "ON" ] ; then
        Echo "tar $TARPARAM all.tar daily_qa"
        tar $TARPARAM all.tar daily_qa
else
        Echo "tar $TARPARAM all.tar tinderbox"
        tar $TARPARAM all.tar tinderbox
fi
gzip all.tar
# ssh-agent > /u/sonmi/.ssh/ssh-agent.info
# setenv like it says in that file
# ssh-add

SSH_AUTH_SOCK=`grep SSH_AUTH_SOCK /u/sonmi/.ssh/ssh-agent.info | sed -e 's/setenv SSH_AUTH_SOCK //' -e 's/;//'`
SSH_AGENT_PID=`grep SSH_AGENT_PID /u/sonmi/.ssh/ssh-agent.info | sed -e 's/setenv SSH_AGENT_PID //' -e 's/;//'`
export SSH_AUTH_SOCK SSH_AGENT_PID
if [ "$O_SILENT" = "OFF" ] ; then
   set -x
   scp all.tar.gz sonmi@stage.mozilla.org:/home/ftp/pub/security/nss
   ssh -l sonmi stage.mozilla.org '/home/sonmi/bin/nssqa_stage '
else
   scp all.tar.gz sonmi@stage.mozilla.org:/home/ftp/pub/security/nss >/dev/null 2>/dev/null 
   ssh -l sonmi stage.mozilla.org '/home/sonmi/bin/nssqa_stage ' >/dev/null 2>/dev/null 
fi

#" rlogin huey "
#" sftp  sonmi@stage.mozilla.org"
#" cd /home/ftp/pub/security/nss"
#" lcd tmp/ftp_stage"
#" put all.tar.gz"
#" quit "
#" ssh -l sonmi stage.mozilla.org"
#" cd /home/ftp/pub/security/nss"
#" gunzip all.tar.gz"
#" tar xvf all.tar"
#" rm all.tar"

