#!/usr/bin/env bash

set -e
set -x



TMPDIR=$(mktemp -d /theHoard/export/home/www/releases.heavy.ai/meta/incoming/tmp.$(date +%s).XXX)
BASE="/theHoard/export/home/www/releases.heavy.ai/"
META="/theHoard/export/home/www/releases.heavy.ai/meta"
ARCHES="x86_64"

while (( $# )); do
    case "$1" in
        --power)
            ARCHES="$ARCHES ppc64le"
            ;;
        -p)
            shift
            PASS=$1
            ;;
        *)
            break ;;
    esac
    shift
done


pushd $TMPDIR

for a in $ARCHES ; do
  for e in os ; do
    for c in cpu cuda ; do
      for p in rpm deb ; do
        if [ "$c" == "cpu" ] ; then
          rr="-cpu"
        else
          rr=''
        fi

        FN=heavyai-$e-$1-Linux-$a$rr.$p

        wget --continue --no-check-certificate --user mapd --password $PASS https://builds.mapd.com/custom/$FN
        if [ "$p" == "rpm" ] ; then
          rpm --addsign $FN
          cp $FN $BASE/$e/yum/stable/$c/
          pushd $BASE/$e/yum/stable/$c/
          createrepo --update .
          popd
        fi
        if [ "$p" == "deb" ] ; then
          aptly -config $META/aptly/$e/aptly.conf repo add $c $FN
        fi
      done
    done
  done
done

###for e in os ; do
###  if [ "$e" == "ee" ] ; then
###    AWS="-aws"
###  fi
###  FN=heavyai-$e-$1-Linux-x86_64-render$AWS.rpm
###  wget --continue --user mapd --password $PASS https://builds.mapd.com/custom/$FN
###  rpm --addsign $FN
###  cp $FN $BASE/aws-$e/yum/stable/cuda/
###  pushd $BASE/aws-$e/yum/stable/cuda/
###  createrepo --update .
###  popd
###done

aptly -config $META/aptly/$e/aptly.conf publish drop stable


D=$(date +%s)

for e in os ; do
  for c in cpu cuda ; do
    aptly -config $META/aptly/$e/aptly.conf snapshot create $c-$D from repo $c
  done
  aptly -config $META/aptly/$e/aptly.conf publish snapshot --component=, cpu-$D cuda-$D
done

rm -r $TMPDIR
