#!/bin/bash
set -e

# MARK: Prepare variable for auto fill contents
TS_START=$(date +20%y%m%d-%H%M%S:%Z)

# MARK: Base tmp dir
T_NAME='apt_dnf_auto.bashrc'
TMP_DIR=$(mktemp -d -t ci-$(date +%Y-%m-%d-%H-%M-%S)-XXXXXXXXXX)
T_TMP_PATH="${TMP_DIR}/${T_NAME}"


APT_AUTO_PATH='3e049384adaa16f00a0028cd355f7a7d'
APT_AUTO_URI="https://gist.githubusercontent.com/Cellularhacker/${APT_AUTO_PATH}/raw/linux-tools-install_apt_dnf_auto_rc.bash"

#T_BASHRC_NAME="${T_NAME}"
#T_MIRROR_HOST='gist.githubusercontent.com'
#T_PROTO_SCHEME='https'
#T_BASE_PATH='/Cellularhacker/d8e87f0e2a068802971cf4b22205e468/raw'

#T_URI="${T_PROTO_SCHEME}://${T_MIRROR_HOST}${T_BASE_PATH}/${T_BASHRC_NAME}"
T_URI="${APT_AUTO_URI}"

# MARK: Download working files on tmp directory just made
echo 'Downloading the latest bashrc file...'
echo " → URI: ${T_URI} ..."
echo ''
curl "${T_URI}" --output "${T_TMP_PATH}"

# MARK: Replace CONSTANTS
echo "Replacing CONSTANTS to variable with current executed environments ..."
echo " → REPLACE_DL_FROM: ${T_URI}"
echo " → APPENDED_AT: ${TS_START}"
echo ''
sed -i "s|REPLACE_DL_FROM|${T_URI}|g" "${T_TMP_PATH}"
sed -i "s|APPENDED_AT|${TS_START}|g" "${T_TMP_PATH}"
echo '' && \
echo '' && \


# MARK: Reflecting the final changes on the /etc/profile file ...
echo 'Reflecting the final changes on the /etc/profile file...' && \
if [[ ! -f '/etc/apt_dnf_auto.bashrc' ]]; then
	echo '' >> /etc/profile && \
	echo '' >> /etc/profile && \
	echo '### MARK: apt_dnf_auto alias tools from mirrors.naho.moe' >> /etc/profile && \
	echo 'source /etc/apt_dnf_auto.bashrc' >> /etc/profile && \
	echo '' >> /etc/profile && \
	echo '' >> /etc/profile
fi

# # MARK: update .bashrc
# BASHRCS=(  )
# 
# ### MARK: check /etc/skel/.bashrc
# if [[ -f "/etc/skel/.bashrc" ]]; then
# 	BASHRCS+=( '/etc/skel/.bashrc' )
# fi
# ### MARK: check /root/.bashrc
# if [[ -f "/root/.bashrc" ]]; then
# 	BASHRCS+=( '/root/.bashrc' )
# fi
# ### MARK: check /home/...
# declare -a HOME_BASHRCS=$( find /home -depth -maxdepth 2 -type f -name '.bashrc' -print 2>/dev/null )
# for i in "${HOME_BASHRCS=$[@]}"; do
#	BASHRCS+=( ${i} )
# done
### MARK: Loop BASHRCS

# BASHRC_HEAD_N_FIND_STR="ls -CF"
# for i in "${BASHRCS[@]}"; do
# 	echo "${i}"
# 
# 	BASHRC_HEAD_N=$(grep "${BASHRC_HEAD_N_FIND_STR}" ${i} -n | cut -d':' -f1)
# 	echo "BASHRC_HEAD_N -> '${BASHRC_HEAD_N}'"
# 
#	# exit #DEBUG
#
#	sed -i ${i} 's|HISTSIZE=1000|HISTSIZE=10000000|g'
#	sed -i ${i} 's|HISTFILESIZE=2000|HISTFILESIZE=20000000|g'
#	sed -i ${i} 's|#force_color_prompt=yes|force_color_prompt=yes|g'
#	sed -i ${i} "s|#alias grep='grep --color=auto'|alias grep='grep --color=auto'|g"
#	sed -i ${i} "s|#alias fgrep='fgrep --color=auto'|alias fgrep='fgrep --color=auto'|g"
#	sed -i ${i} "s|#alias egrep='egrep --color=auto'|alias egrep='egrep --color=auto'|g"
#	sed -i ${i} "s|##export GCC_COLORS='error=01;31:warning=01;35:note=01;36:caret=01;32:locus=01:quote=01'|#export GCC_COLORS='error=01;31:warning=01;35:note=01;36:caret=01;32:locus=01:quote=01'|g"
#	sed -i ${i} "s|"
#	sed -i ${i} "s|"
#	sed -i ${i} "s|"
#	sed -i ${i} 's|'
#	sed -i ${i} 's|'
#	sed -i ${i} 's|'
#	sed -i ${i} 's|'
# done

cp -pf ${T_TMP_PATH} '/etc/apt_dnf_auto.bashrc' && \
echo '' && \
echo 'Finished to install apt & dnf util successfully!' && \
echo '' && \
echo 'You can use it for `apt_init` or `dnf_init` fot install utilities,' && \
echo ' use `apt_auto` or `dnf_auto` for update repo cache, update all packes and clean all caches' && \
echo ''
echo " Ah, please don't forget to re-open your console!"
echo ''
