#!/bin/bash
rpms_path=""
#source_path=/opt/mount/source_code
source_path=""
#rpms_path=/opt
blank=0
comment=0
code=0
current_path=$(
  pwd
  cd $(dirname $0)
)
rpm_count_file="${current_path}/rpm_count_file.txt"

function clean_env() {
  echo "[INFO] Start to clean env"
  if [[ -d "/root/rpmbuild/SOURCES" ]]; then
    rm -rf /root/rpmbuild/SOURCES/*
  else
    mkdir -p /root/rpmbuild/SOURCES
  fi
  if [[ -d "/root/rpmbuild/SPEC" ]]; then
    rm -rf /root/rpmbuild/SPEC/*
  else
    mkdir -p /root/rpmbuild/SPEC
  fi
  if [[ -d "${source_path}" ]]; then
    rm -rf ${source_path}/*
  else
    mkdir -p ${source_path}
  fi
  if [[ -f "${rpm_count_file}" ]]; then
    rm -rf "${rpm_count_file}"
  else
    touch "${rpm_count_file}"
  fi
}

function main_process() {
  echo "[INFO] Start !!!"
  for file in $(ls ${rpms_path}); do
    if [[ ${file} =~ "src.rpm" ]]; then
      install_src_rpm_and_statistic ${file}
    fi
  done
}

function install_src_rpm_and_statistic() {
  rpm_name=$1
  echo "[INFO] Start to install ${rpm_name}"
  rpm -ivh ${rpms_path}/${rpm_name}
  echo "[INFO] Move source to ${source_path}/${rpm_name}"
  mkdir -p ${source_path}/${rpm_name}/
  mv /root/rpmbuild/SOURCES/* ${source_path}/${rpm_name}/
  mv /root/rpmbuild/SPEC/* ${source_path}/${rpm_name}/
  cloc_count ${source_path}/${rpm_name} ${rpm_name}
  rm -rf ${source_path}/${rpm_name}
}

function not_install_statistic() {
  for path in $(ls ${source_path}); do
    cloc_count ${source_path}/${path}
  done
}

function cloc_count() {
  files_path=$1
  rpm_name=$2
  echo "[INFO] Start to eval code count of ${files_path}"
  for file in $(ls ${files_path}); do
    sum_result=$(cloc ${files_path}/${file} | grep "SUM:")
    current_blank=$(echo ${sum_result} | awk -F ' ' '{print $3}')
    current_comment=$(echo ${sum_result} | awk -F ' ' '{print $4}')
    current_code=$(echo ${sum_result} | awk -F ' ' '{print $5}')
    ((blank = ${current_blank} + ${blank}))
    ((comment = ${current_comment} + ${comment}))
    ((code = ${current_code} + ${code}))
  done
  echo "${rpm_name}: blank: ${blank}    comment: ${comment}    code: ${code}" >>${rpm_count_file}
  print_result
}

function read_file() {
  file=$1
  echo "[INFO] Start read ${file}"
  while read line; do
    src_rpm=$(ls ${rpms_path} | grep ${line})
    if [[ -n ${src_rpm} ]]; then
      install_src_rpm_and_statistic ${src_rpm}
    else
      echo "[ERROR] ${line} is not exist"
      echo "[ERROR] ${line} is not exist" >>${rpm_count_file}
    fi
  done <${file}
}

function print_result() {
  echo "[INFO] blank: ${blank}    comment: ${comment}    code: ${code}"
}

function main() {
  rpms_path=$2
  source_path=$3
  clean_env
  if [[ $1 == "statistic" ]]; then
    main_process
  elif [[ $1 == "delete" ]]; then
    rpm_count_file="${current_path}/delete_rpm_count_file.txt"
    rm -rf ${rpm_count_file}
    read_file ${current_path}/"delete.txt"
  elif [[ $1 == "add" ]]; then
    rpm_count_file="${current_path}/add_rpm_count_file.txt"
    rm -rf ${rpm_count_file}
    read_file ${current_path}/"add.txt"
  elif [[ $1 == "not_install_statistic" ]]; then
    not_install_statistic
  fi
  print_result
}

main "$@"
