# Adapted from archlinux build script
abinfo "Extrating CUDA installer ..."
sh cuda_${PKGVER/+/_}_linux.run --target "${SRCDIR}" --noexec

CUDA_ROOT="/usr/lib/cuda"

abinfo "Removing uninstaller and runfile itself"
rm -fv "$SRCDIR"/builds/*.run
rm -frv "$SRCDIR"/builds/bin

abinfo "Creating installation directories ..."
mkdir -pv "$PKGDIR"/"$CUDA_ROOT"/extras

abinfo "Copying integration and nsight libraries ..."
cp -rv "$SRCDIR"/builds/{integration,nsight_compute,nsight_systems,EULA.txt} \
    "${PKGDIR}${CUDA_ROOT}"

abinfo "Copying cuda compute sanitizer ..."
cp -rv "$SRCDIR"/builds/cuda_sanitizer_api/compute-sanitizer \
    "${PKGDIR}${CUDA_ROOT}/extras/compute-sanitizer"

for lib in "$SRCDIR"/builds/!(version.json|EULA.txt); do
	abinfo "Copying $lib ..."
	cp -rv $lib/* "${PKGDIR}${CUDA_ROOT}/"
done

abinfo "Telling the Makefiles to use the correct prefix ..."
for f in $(find "$PKGDIR"${CUDA_ROOT} -name Makefile); do
	sed -e "s|/usr/local/cuda|${CUDA_ROOT}|g" \
            -i "$f"
done

abinfo "Symlinking the compiler that CUDA uses ..."
ln -sv ../../../../bin/gcc \
    "$PKGDIR"/usr/lib/cuda/bin/gcc
ln -sv ../../../../bin/g++ \
    "$PKGDIR"/usr/lib/cuda/bin/g++

abinfo "Installing EULA ..."
mkdir -pv "$PKGDIR"/usr/share/doc/"$PKGNAME"
ln -sv ../../lib/cuda/doc/pdf/EULA.pdf \
    "$PKGDIR"/usr/share/doc/"$PKGNAME"/EULA.pdf

abinfo "Setting executable bits to all shared libraries ..."
for i in "$PKGDIR"/**/*.so*; do
    chmod -v 0755 $i
done

abinfo "Setting executable bits to all static libraries ..."
for i in "$PKGDIR"/**/*.a; do
    chmod -v 0644 $i
done

abinfo "Generating pkg-config for cuda libraries..."
CUDA_LIBS=(
	"accinj64|OpenACC 64-bit Injection Library"
	"cublas|CUDA BLAS Library"
	"cuda|CUDA Driver Library"
	"cudart|CUDA Runtime Library"
	"cufft|CUDA Fast Fourier Transform"
	"cufftw|CUDA Fast Fourier Transform Wide"
	"cuinj64|CUDA 64-bit Injection Library"
	"curand|CUDA Random Number Generation Library"
	"cusolver|A LAPACK-like library on dense and sparse linear algebra"
	"cusparse|CUDA Sparse Matrix Library"
	"nppc|NVIDIA Performance Primitives - Core"
	"nppi|NVIDIA Performance Primitives - Image Processing"
	"nppial|NVIDIA Performance Primitives - Image Processing - Arithmetic and Logic"
	"nppicc|NVIDIA Performance Primitives - Image Processing - Color Conversion"
	"nppicom|NVIDIA Performance Primitives - Image Processing - Compression"
	"nppidei|NVIDIA Performance Primitives - Image Processing - DEI"
	"nppif|NVIDIA Performance Primitives - Image Processing - Filters"
	"nppig|NVIDIA Performance Primitives - Image Processing - Geometry"
	"nppim|NVIDIA Performance Primitives - Image Processing - Morphological"
	"nppist|NVIDIA Performance Primitives - Image Processing - Statistic and Linear"
	"nppisu|NVIDIA Performance Primitives - Image Processing - Support and Data Exchange"
	"nppitc|NVIDIA Performance Primitives - Image Processing - Threshold and Compare"
	"npps|NVIDIA Performance Primitives - Signal Processing"
	"nvToolsExt|NVIDIA Tools Extension"
	"nvgraph|NVIDIA Accelerated Graph Analytics"
	"nvidia-ml|NVML"
	"nvjpeg|NVIDIA JPEG Library"
	"nvrtc|A runtime compilation library for CUDA C++"
)
install -dm755 "${PKGDIR}/usr/lib/pkgconfig"
VER_PARTS=($(echo $PKGVER | sed -e 's/\./ /g'))
MAJMIN=$(echo "${VER_PARTS[0]}.${VER_PARTS[1]}")
for i in "${CUDA_LIBS[@]}"; do
	LIB=$(echo "$i" | cut -d '|' -f 1)
	DESC=$(echo "$i" | cut -d '|' -f 2)
	CONFIG="${PKGDIR}"/usr/lib/pkgconfig/"$LIB"
	abinfo "Writing pkg-config for $LIB"
	cat "$SRCDIR/autobuild/pkg-config.in" | sed \
		-e "s/@MAJMIN@/${MAJMIN}/g" \
		-e "s/@LIB@/${LIB}/g" \
		-e "s/@DESC@/${DESC}/g" > "$CONFIG"
done
