#!/bin/bash

install_version=${1:-"3.7.11"}
package_name="ccache-${install_version}"

# Download
cd /tmp
rm -f ${package_name}.tar.gz
wget https://tools.mindspore.cn/tools/build/ccache/${package_name}/${package_name}.tar.gz
rm -rf ${package_name}
tar -zxf ${package_name}.tar.gz

# Install
cd /tmp/${package_name}
./configure --prefix=/usr/local/ccache
make -j8
make install -j8

# Set link
rm -f /usr/local/bin/gcc
rm -f /usr/local/bin/g++
rm -f /usr/local/bin/cc
rm -f /usr/local/bin/c++
ln -s /usr/local/ccache/bin/ccache /usr/local/bin/gcc
ln -s /usr/local/ccache/bin/ccache /usr/local/bin/g++
ln -s /usr/local/ccache/bin/ccache /usr/local/bin/cc
ln -s /usr/local/ccache/bin/ccache /usr/local/bin/c++

# Clear cacche
/usr/local/ccache/bin/ccache -C

# Set ccache
#export CCACHE_DIR=/home/ccache/.ccache
/usr/local/ccache/bin/ccache --max-size=8
/usr/local/ccache/bin/ccache -p

# Delete package
rm -rf /tmp/${package_name}
rm -f /tmp/${package_name}.tar.gz
