#!/bin/bash

# 定时任务配置：10 16 */5 * * /usr/bin/bash /home/hailey/shell/lede.sh >> /home/hailey/shell/lede.log 2>&1

# 目录路径变量
LEDE_DIR="/home/hailey/lede"
BUILD_DIRS=("dl" "build_dir" "feeds" "staging_dir" "tmp" "bin/targets/x86/64/packages")


# 清理函数
clean_directories() {
  for dir in "${BUILD_DIRS[@]}"; do
    rm -rf "$LEDE_DIR/$dir"
  done
  echo "" > "/home/hailey/shell/lede.log"
  make clean
  make dirclean
}

# 更新代码和依赖
update_feeds() {
  cd "$LEDE_DIR" || exit
  git pull
  echo 'src-git opentopd  https://github.com/sirpdboy/sirpdboy-package' >> feeds.conf.default
  echo 'src-git kenzo https://github.com/kenzok8/openwrt-packages' >> feeds.conf.default
  echo 'src-git small https://github.com/kenzok8/small' >> feeds.conf.default
  ./scripts/feeds update -a
  ./scripts/feeds install -a
}

# 编译函数
build_project() {
  make defconfig
  make download -j8
  make download V=s -j1
  make download V=s -j1
  rm -rf feeds/packages/lang/golang
  git clone https://github.com/sbwml/packages_lang_golang -b 24.x feeds/packages/lang/golang
  make V=s -j1
}

# 脚本主流程
main() {
  pushd "${LEDE_DIR}"
#   clean_directories
  update_feeds
  build_project
  tar zcvf /home/hailey/bin.tar.gz /home/hailey/lede/bin
  popd > "/dev/null"
}

main

