#!/usr/bin/env ruby
# coding: utf-8
require 'bundler/setup'
require 'net/http'
require 'uri'
require_relative 'move_old_files'

# このスクリプトは TARGET_DIR と BAK_DIR が WebDAV 上に存在するかを確認するだけです
# 実行: bundle exec ruby test_webdav.rb

def exists_on_webdav?(path)
  uri = URI.parse(BASE_URL.chomp('/') + "/remote.php/dav/files/#{NC_USER}" + path)
  req = Net::HTTP::Propfind.new(uri.request_uri)
  req['Depth'] = '0'
  req.basic_auth(NC_USER, NC_PASS)

  res = Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == 'https') do |http|
    http.request(req)
  end
  # 207 Multi-Status または 200 系を存在と判断
  [200, 207].include?(res.code.to_i)
rescue => e
  warn "Error checking #{path}: #{e}"
  false
end

puts "Checking TARGET_DIR: #{TARGET_DIR}"
puts exists_on_webdav?(TARGET_DIR) ? "FOUND" : "NOT FOUND"
puts "Checking BAK_DIR: #{BAK_DIR}"
puts exists_on_webdav?(BAK_DIR) ? "FOUND" : "NOT FOUND"
