#############################################
# void retrieve_file (src, dst)
#
# Description: retrieve file from an URL
#
# Parameter
#  downloader: program to download the files
#  src: source url to get file from
#  dst: path to store file to
#
#############################################
function  retrieve_file() {
  local downloader=$1
  local src=$2
  local dst=$3
  
  # Check, if the URL is a file URL starting with file://
  if [ -f $dst ] && [ -z ${DIST_FORCE_DOWNLOAD} ]; then
    echo "  Info: File still cached/downloaded. To force a download, set DIST_FORCE_DOWNLOAD=1"
    elif [[ $src == file://* ]]; then
    fileurl=`echo $src | sed "s/^file:\/\///"`
    cp  $fileurl $dst  2>/dev/null
  else
    rm -f $dst
    if [ "$downloader" = "wget" ]; then
      wget  --no-check-certificate --tries=1 -O $dst --timeout=5 -q --no-verbose "$src"
      if [ "$?" == "1" ] ; then
        rm $dst
      fi
      elif [ "$downloader" = "plowdown" ]; then
      plowdown --max-retries=1 -o ${dst%/*} "$src"
      elif [ "$downloader" = "aria2c" ]; then
      #aria2c --seed-time=0 -d ${dst%/*} -o ${dst##*/} "$src"
      aria2c --seed-time=0 --allow-overwrite=true -o ${dst##*/} "$src"
      mv -f ${dst##*/} ${dst%/*}
    else
      fatal_error "Downloader not implemented: $downloader"
    fi
  fi
}

#############################################
# void extract_file (format, src, dst)
#
# Description: Extract a file
#
# Parameter
#  format: compression format
#  src: source file to be used
#  dst: path to extract the file
#
#############################################
function  process_file() {
  
  local format=$1
  local src=$2
  local dst=$3
  local option=$4
  
  log_debug "Compression format: $format"
  
  if [ "$format" = "cp" ]; then
    cp $src $dst
    elif [ "$format" = "7zip" ]; then
    $CMD_7z x -y $option -o$dst $src
    elif [ "$format" = "unzip" ]; then
    $CMD_unzip $option -o $src -d $dst
    elif [ "$format" = "unrar" ]; then
    $CMD_unrar x -y $option $src $dst
    elif [ "$format" = "lha" ]; then
    $CMD_lha x $option -w=$dst $src
    elif [ "$format" = "targz" ]; then
    $CMD_tar xzvf $option $src -C $dst
    elif [ "$format" = "tarbz2" ]; then
    $CMD_tar xjvf $option $src -C $dst
    elif [ "$format" = "cab" ]; then
    $CMD_cabextract $option -d $dst $src
  else
    fatal_error "Unknown compression format: $format"
  fi
}


#############################################
# check if method is available and call it
#############################################
function  call_entry_point() {
  local _resultvar=$1
  local func=$2
  
  # Entry point
  type $func &>/dev/null
  if [ $? == 0 ] ; then
    $func
    eval $_resultvar="0"
  else
    eval $_resultvar="1"
  fi
  
}

###################
# Fata Error
###################
fatal_error() {
  echo "FATAL: $1"
  exit 1
}

###################
# Check error
###################
builder_check_error() {
  if [ "$?" == "1" ] ; then
    fatal_error "$1"
  fi
}

###################
# Logging Debug
###################
log_debug() {
  local str=$1
  
  if [ "$DEBUG_LEVEL" = "debug" ] ||  [ "$DEBUG_LEVEL" = "info" ]  ; then
    echo $str
  fi
}

###################
# Logging Info
###################
log_info() {
  local str=$1
  
  if [ "$DEBUG_LEVEL" = "info" ]  ; then
    echo $str
  fi
}

#####################
# void calc_release()
#
# Description:
#   Calculate new release based on
#   the latest one found in the repository.
#
# $RELEASE is set to the calculated release.
#
####################
function calc_release() {
  
  # Find all revision files and sort them
  local file_list=`mktemp /tmp/aio-builder.calc_release.XXXXXXXXXXX`
  for cfg_file in `find ${AIO_REPOS_BASE_DIR} -name "${PN}-${VERSION}-${CREATOR_TAG}*.cfg" -print ` ; do
    . ${cfg_file}
    printf "%08d;$cfg_file\n" $REV_RELEASE >> ${file_list}
  done
  local oldest_cfg=`sort -n ${file_list} | cut -f 2 -d ";" | tail -1`
  rm -f ${file_list}
  
  if [ ! -f "${oldest_cfg}" ] ; then
    echo "Warning: no cfg-file found for this product. RELEASE will be set to 1"
    RELEASE=1
  else
    log_debug "calc_release() oldest_cfg: ${oldest_cfg}"
    . ${oldest_cfg}
    log_debug "  latest release: $REV_RELEASE"
    RELEASE=`expr ${REV_RELEASE} + 1 2> /dev/null`
    builder_check_error "Cannot incrememnt REV_RELEASE from file ${oldest_cfg}"
  fi
}

###################
# Write Package Ini file
#
# Create a ini file containing needed information for package uninstall
#
# Parameter
#  file: file to create / modify
#
###################
write_ini_file() {
  local ini_file=$1
  local ini_section=$2
  local ini_option=$3
  local ini_value=$4

  touch $ini_file
  $CMD_inifile $(cygpath -pw "$ini_file") [$ini_section] $ini_option=$ini_value
}


###################
# Get Verison of File
#
# Copy file only if it is newer
#
# Parameter
#  File: to process
#  Var:
#
###################
get_file_version() {
  local getver_file=$1
  local getver_var=$2
  
  if [ -f "$getver_file" ] ; then
    $CMD_showver $getver_file | grep FileVersion: | rev | cut -d: -f1 | rev | tr -d ' ' > $getver_ver
    echo "file $getver_file has version $getver_ver"
  else
    echo "$getver_file not found"
  fi
}

###################
# Copy Verison
#
# Copy file only if it is newer
#
# Parameter
#  source: sourcefiel
#  target: Targetfile
#
###################
copyver() {
  local source_file=$1
  local target_file=$2
  local source_ver
  local target_ver
  
  get_file_version $source_file $source_ver
}

###################
# Tidy xml
#
# Tidy XML files (mainly Sereby package.xml)
#
# Parameter
#  source: sourcefiel
#  target: Targetfile
#
###################
tidy_xml() {
  local source_file=$1
  local target_file=$2
  
  cat $source_file | $CMD_tidy -utf8 -xml -w 255 -i -c -q -asxml | sed -e 's!&gt;!>!g' -e 's!&lt;!<!g' > $target_file
}