#!/bin/bash
current_dir=$(pwd)
resources="${current_dir}/resources1"
status_folder="${current_dir}/status1"

echo "resources->${resources}"
echo "status_folder->${status_folder}"

# Function
run_command() {
    echo "执行命令: $@"
    if ! sudo "$@"; then
        echo "错误：命令执行失败 - $@"
        exit 1
    fi
}

#############################################
if [ ! -e ${resources} ]; then
	echo "${resources} folder is empty, stop "
	exit 1
fi


if [ ! -e ${status_folder} ]; then
	echo "mkdir ..............."
	mkdir -p ${status_folder}
fi

if [ ! -e ${status_folder}/success-1 ]; then
	run_command cp ${resources}/sources.list /etc/apt/sources.list
	run_command apt update
	run_command apt install     python3-dev python3-pip lrzsz git gcc pkg-config \
								libcairo2-dev tig libgirepository1.0-dev minicom \
								cron logrotate \
								-y
	run_command apt autoremove -y
	run_command apt autoclean -y
	touch ${status_folder}/success-1
fi

if [ ! -e ${status_folder}/success-2 ]; then
	run_command pip3 install --upgrade pip -i https://mirrors.aliyun.com/pypi/simple/
	run_command pip3 install --upgrade setuptools -i https://mirrors.aliyun.com/pypi/simple/
	touch ${status_folder}/success-2
fi

if [ -e ${status_folder}/success-1 ] && [ -e ${status_folder}/success-2 ]; then
	touch ${status_folder}/success_ok
	echo "所有命令执行成功。"
fi