#!/bin/bash -e

SCRIPTNAME="$(basename "${0}")"
BASEDIR="$(readlink -f "$(dirname "${0}")")"

if [ ! -d .git ]; then
    echo "$SCRIPTNAME: Full path to the script: $BASEDIR/$SCRIPTNAME"
    echo "$SCRIPTNAME: No .git present. Run this from the base dir of the git checkout."
    exit 1
fi

# Running the script from a directory containing a colon anywhere in the path
# will expose us to the dreaded "[BUG] npm run [command] failed if the directory
# path contains colon" bug https://github.com/npm/cli/issues/633
# (see https://tracker.ceph.com/issues/39556 for details)
if [[ "$BASEDIR" == *:* ]] ; then
    echo "$SCRIPTNAME: Full path to the script: $BASEDIR/$SCRIPTNAME"
    echo "$SCRIPTNAME: The path to the script contains a colon. Their presence has been known to break the script."
    exit 1
fi

version=$1
[ -z "$version" ] && version=$(git describe --long --match 'v*' | sed 's/^v//')
if expr index $version '-' > /dev/null; then
    rpm_version=$(echo $version | cut -d - -f 1-1)
    rpm_release=$(echo $version | cut -d - -f 2- | sed 's/-/./')
else
    rpm_version=$version
    rpm_release=0
fi

outfile="ceph-$version"
echo "version $version"

# update submodules
echo "updating submodules..."
force=$(if git submodule usage 2>&1 | grep --quiet 'update.*--force'; then echo --force ; fi)
if ! git submodule sync || ! git submodule update $force --init --recursive; then
    echo "Error: could not initialize submodule projects"
    echo "  Network connectivity might be required."
    exit 1
fi

download_from() {
    fname=$1
    shift
    sha256=$1
    shift
    set +e
    while true; do
        url_base=$1
        shift
        if [ -z $url_base ]; then
            echo "Error: failed to download $name."
            exit
        fi
        url=$url_base/$fname
        wget -c --no-verbose -O $fname $url
        if [ $? != 0 -o ! -e $fname ]; then
            echo "Download of $url failed"
        elif [ $(sha256sum $fname | awk '{print $1}') != $sha256 ]; then
            echo "Error: failed to download $name: SHA256 mismatch."
        else
            break
        fi
    done
    set -e
}

download_boost() {
    boost_version=$1
    shift
    boost_sha256=$1
    shift
    boost_version_underscore=$(echo $boost_version | sed 's/\./_/g')
    boost_fname=boost_${boost_version_underscore}.tar.bz2
    download_from $boost_fname $boost_sha256 $*
    tar xjf $boost_fname -C src \
        --exclude="$boost_version_underscore/libs/*/doc" \
        --exclude="$boost_version_underscore/libs/*/example" \
        --exclude="$boost_version_underscore/libs/*/examples" \
        --exclude="$boost_version_underscore/libs/*/meta" \
        --exclude="$boost_version_underscore/libs/*/test" \
        --exclude="$boost_version_underscore/tools/boostbook" \
        --exclude="$boost_version_underscore/tools/quickbook" \
        --exclude="$boost_version_underscore/tools/auto_index" \
        --exclude='doc' --exclude='more' --exclude='status'
    mv src/boost_${boost_version_underscore} src/boost
    tar cf ${outfile}.boost.tar ${outfile}/src/boost
    rm -rf src/boost
}

download_liburing() {
    liburing_version=$1
    shift
    liburing_sha256=$1
    shift
    liburing_fname=liburing-${liburing_version}.tar.gz
    download_from $liburing_fname $liburing_sha256 $*
    tar xzf $liburing_fname -C src  \
        --exclude=debian \
        --exclude=examples \
        --exclude=man \
        --exclude=test
    # normalize the names, liburing-0.7 if downloaded from git.kernel.dk,
    # liburing-liburing-0.7 from github.com
    mv src/liburing-* src/liburing
    tar cf ${outfile}.liburing.tar ${outfile}/src/liburing
    rm -rf src/liburing
}

download_pmdk() {
    pmdk_version=$1
    shift
    pmdk_sha256=$1
    shift
    pmdk_fname=pmdk-${pmdk_version}.tar.gz
    download_from $pmdk_fname $pmdk_sha256 $*
    tar xzf $pmdk_fname -C src \
        --exclude="pmdk-${pmdk_version}/doc" \
        --exclude="pmdk-${pmdk_version}/src/test" \
        --exclude="pmdk-${pmdk_version}/src/examples" \
        --exclude="pmdk-${pmdk_version}/src/benchmarks"
    mv src/pmdk-${pmdk_version} src/pmdk
    tar cf ${outfile}.pmdk.tar ${outfile}/src/pmdk
    rm -rf src/pmdk
}

build_dashboard_frontend() {
  CURR_DIR=`pwd`
  TEMP_DIR=`mktemp -d`

  $CURR_DIR/src/tools/setup-virtualenv.sh $TEMP_DIR
  $TEMP_DIR/bin/pip install nodeenv
  $TEMP_DIR/bin/nodeenv --verbose -p --node=12.18.2
  cd src/pybind/mgr/dashboard/frontend

  . $TEMP_DIR/bin/activate
  NG_CLI_ANALYTICS=false timeout 1h npm ci
  echo "Building ceph-dashboard frontend with build:localize script";
  # we need to use "--" because so that "--prod" survives accross all
  # scripts redirections inside package.json
  npm run build:localize -- --prod
  deactivate
  cd $CURR_DIR
  rm -rf $TEMP_DIR
  tar cf dashboard_frontend.tar $outfile/src/pybind/mgr/dashboard/frontend/dist
}

generate_rook_ceph_client() {
  $outfile/src/pybind/mgr/rook/generate_rook_ceph_client.sh
  tar cf rook_ceph_client.tar $outfile/src/pybind/mgr/rook/rook_client/*.py
}

# clean out old cruft...
echo "cleanup..."
rm -f $outfile*

# build new tarball
echo "building tarball..."
bin/git-archive-all.sh --prefix ceph-$version/ \
		       --verbose \
		       --ignore corpus \
		       $outfile.tar

# populate files with version strings
echo "including src/.git_version, ceph.spec"

(git rev-parse HEAD ; echo $version) 2> /dev/null > src/.git_version

for spec in ceph.spec.in; do
    cat $spec |
        sed "s/@PROJECT_VERSION@/$rpm_version/g" |
        sed "s/@RPM_RELEASE@/$rpm_release/g" |
        sed "s/@TARBALL_BASENAME@/ceph-$version/g" > `echo $spec | sed 's/.in$//'`
done
ln -s . $outfile
tar cvf $outfile.version.tar $outfile/src/.git_version $outfile/ceph.spec
# NOTE: If you change this version number make sure the package is available
# at the three URLs referenced below (may involve uploading to download.ceph.com)
boost_version=1.75.0
download_boost $boost_version 953db31e016db7bb207f11432bef7df100516eeb746843fa0486a222e3fd49cb \
               https://boostorg.jfrog.io/artifactory/main/release/$boost_version/source \
               https://downloads.sourceforge.net/project/boost/boost/$boost_version \
               https://download.ceph.com/qa
download_liburing 0.7 8e2842cfe947f3a443af301bdd6d034455536c38a455c7a700d0c1ad165a7543 \
                  https://github.com/axboe/liburing/archive \
                  https://git.kernel.dk/cgit/liburing/snapshot
pmdk_version=1.10
download_pmdk $pmdk_version 08dafcf94db5ac13fac9139c92225d9aa5f3724ea74beee4e6ca19a01a2eb20c \
               https://github.com/pmem/pmdk/releases/download/$pmdk_version
build_dashboard_frontend
generate_rook_ceph_client
for tarball in $outfile.version   \
               $outfile.boost     \
               $outfile.liburing  \
               $outfile.pmdk  \
               dashboard_frontend \
               rook_ceph_client   \
               $outfile; do
    tar --concatenate -f $outfile.all.tar $tarball.tar
    rm $tarball.tar
done
mv $outfile.all.tar $outfile.tar
rm $outfile

echo "compressing..."
bzip2 -9 $outfile.tar

echo "done."