#!/usr/bin/env bash
#
# Script to promote the latest build in the specified committers area
#

function usage() {
    printf "\n\tUsage: %s [-v] [-d] [-s] [-c] [-z] [-p] projectname " $(basename $0) >&2
    printf "\n\t\t%s\n" "where <projectname> (for -p) is cc project, such as wtp-R3.2.3-M, wtp-R3.3.0-I, etc." >&2	
    printf "\n\t\t%s\t%s" "-v" "verbose" >&2
    printf "\n\t\t%s\t%s" "-d" "delete old builds" >&2
    printf "\n\t\t%s\t%s" "-s" "send notification mail" >&2
    printf "\n\t\t%s\t%s" "-c" "copy the build from committers to downloads" >&2
    printf "\n\t\t%s\t%s" "-z" "process artifacts (create pack200 gz jars, etc.) Remember, is long running." >&2    
}

source rsync-retry.sh

verboseFlag=
deleteOld=
doCopy=
projectname=
processArtifacts=
while getopts 'hvdcszp:' OPTION
do
    case $OPTION in
        h)    usage
        exit 1
        ;;
        v)    verboseFlag=1
        ;;
        d)    deleteOld=1
        ;;
        c)    doCopy=1
        ;;
        s)    sendmail=1
        ;;
        z)    processArtifacts=1
        ;;        
         p)    projectname=$OPTARG
        ;;
        ?)    usage
        exit 2
        ;;
    esac
done

shift $(($OPTIND - 1))



# check we have at least the project name
if [ -z $projectname ]
then
    printf "\n\t%s\n"   "Error: project name is required." >&2
    usage
    exit 1
fi 

if [[ "$projectname" =~ (.*)-(.*)-(.*) ]] 
then
    distribution=${BASH_REMATCH[1]}
    buildBranch=${BASH_REMATCH[2]}
    buildType=${BASH_REMATCH[3]}
    printf "\n\t%s\n\n" "Promoting latest build from ${1} ..."
    if [ $verboseFlag ]
    then
        echo "distribution: $distribution"
        echo "buildBranch: $buildBranch"
        echo "buildType: $buildType"
    fi 
else
    printf "\n\t%s\n"   "Error: projectname doesn't match <distribution>-<buildbranch>-<buildtype> pattern." >&2
    usage
    exit 3
fi

# remember to leave no slashes on filename in source command,
# (the commonVariations.shsource file, that is)
# so that users path is used to find it (first)
if [ -z $BUILD_INITIALIZED ]
then
    source commonVariations.shsource
    source ${BUILD_HOME}/releng.control/commonComputedVariables.shsource
fi

artifactsDir=${PROJECT_ARTIFACTS}
promoteProjectDir=${artifactsDir}'/'${projectname}
if [ $verboseFlag ] 
then 
    echo "Project directory to promote: ${promoteProjectDir} "
fi 

if [ ! \( -d ${promoteProjectDir} \) ] 
then
    printf "\n\t%s\n"   "Error: directory ${promoteProjectDir} does not exist." >&2
    usage
    exit 4
fi

i=0
for FN in ${promoteProjectDir}/* 
do
    dirName=$(basename ${FN})
    if [ $verboseFlag ] 
    then 
        echo -n "${i}: "
        echo ${dirName}
    fi
    # todo: could check that the name follows the expected date pattern
    dirList[${i}]=${dirName}
    i=$(($i + 1))
done

nDir=${#dirList[*]}

if [ $verboseFlag ] 
then 
    echo "Number of directories: ${nDir}" 
fi 

# echo "Least recent: ${dirList[0]}"
# echo "Most  recent: ${dirList[$(($nDir - 1))]}"

mostRecent=${dirList[$(($nDir - 1))]}

mostRecentDir=${promoteProjectDir}/${mostRecent}


i=0
for FN in ${mostRecentDir}/* 
do
    dropDirName=$(basename ${FN})
    if [ $verboseFlag ] 
    then 
        echo -n "${i}: "
        echo ${dropDirName}
    fi
    # todo: could check that the name follows the expected drop directory pattern
    dropDirList[${i}]=${dropDirName}
    i=$(($i + 1))
done

ndropDir=${#dropDirList[*]}

# there should be exactly one drop directory
if [ $ndropDir != 1 ] 
then 
    printf "\n\t%s\n"   "Error: there was not exactly one drop direc:tory. There was $ndropDir found instead." >&2
    usage
    exit 5
fi

# knowing there is exactly one, the value of dropDirName is still valid

echo "Drop directory: ${dropDirName}"

FROMDIR=${mostRecentDir}/${dropDirName}

if [[ processArtifacts ]] 
then
#run pack200 (and, recompute checksums) before literally promoting

       repoDirLocation=$FROMDIR/repository
       if [[ -d "${repoDirLocation}" ]] 
       then
              echo "INFO: processing artifacts in code repo: $repoDirLocation";
              ${RELENG_CONTROL}/runAntRunner.sh process-artifacts.xml -DrepoDirLocation="${repoDirLocation}" 
       else 
              echo "WARNING: expected code repo directory does not exist: $repoDirLocation";
       fi 
       
       repoDirLocation=$FROMDIR/repositoryunittests
       if [[ -d "${repoDirLocation}" ]] 
       then
              echo "INFO: calling processing artifacts in test repo: $repoDirLocation";
              ${RELENG_CONTROL}/runAntRunner.sh process-artifacts.xml -DrepoDirLocation="${repoDirLocation}"
       else 
              echo "WARNING: expected test repo directory does not exist: $repoDirLocation";
       fi 
fi 

if [ "patches" == $distribution ] 
then
   TODIR=${DOWNLOAD_ROOT}/webtools/patches/drops/${buildBranch}/
else
   TODIR=${DOWNLOAD_ROOT}/webtools/downloads/drops/${buildBranch}/
fi

printf "\t%s\n"         "declaring build ${dropDirName} on buildstream  ${buildBranch}"
printf  "\t\t%s\n"      "into ${TODIR}"
printf  "\t\t%s\n\n"   "using the build from ${FROMDIR}"

if [ $doCopy ] 
then
	rsync-retry ${FROMDIR} ${TODIR} $verboseFlag
	exitCode=$?
	if [ $exitCode -ne 0 ] 
	then 
		exit $exitCode
	fi

    fromString="webtools/committers"
    if [ "patches" == $distribution ] 
    then
      toString="webtools/patches"
    else
      toString="webtools/downloads"
    fi
    replaceCommand="s!${fromString}!${toString}!g"

    # remember TODIR already has a slash
    ${REMOTE_SSH_COMMAND} perl -w -pi -e ${replaceCommand} ${TODIR}${dropDirName}/*.php

    # now do the composite reference site update
    
    case $buildType in
        I )
            referenceCompositeDir=${BUILD_HOME}/integration;;
        M )
            referenceCompositeDir=${BUILD_HOME}/maintenance;;
        * )
            print "error: 'buildType' ($buildType) was not an expected value."
            print "        composite reference not updated." ;;
    esac
    if [ ! -z referenceCompositeDir ] 
    then
        dropDirectory=${TODIR}${dropDirName}
        ./runAntRunner.sh "${PWD}/updateReferenceComposite.xml" "-DreferenceCompositeDir=$referenceCompositeDir" "-DdropDirectory=$dropDirectory"
    fi 
else
    printf  "\n\t%s\n\n"   "Nothing copied: specify -c to actually do the copy"
fi 

if [ $sendmail ]
then
    ./sendPromoteMail.sh "$projectname" "$TODIR" "$dropDirName"
    echo "mail sent"
else
    echo "no mail sent. Specify -s if desired"
fi 


if [ $deleteOld ] 
then 

    maxToDelete=$(($nDir - 1))
    if [ $verboseFlag ]
    then
        echo "maxToDelete: $maxToDelete"
    fi
    for (( i=0; i < $maxToDelete; i++)) 
    do
        dirbasename=${dirList[$i]}
        dropDirName=${promoteProjectDir}/${dirbasename}
        if [ $verboseFlag ]
        then
            echo -n "${i}: "
            echo ${dropDirName}
        fi
        rm -fr ${dropDirName}
    done

fi
