! Copyright (C) GFD Dennou Club, 2000.  All rights reserved.
! gtcopy - 変数複写ツール

! 書式
!   gtcopy 変数名 出力変数名
!   gtcopy 変数名 変数名 ... 出力ファイル名

program gtcopy
    use gtool
    implicit none
    type(VSTRING):: source
    type(VSTRING):: dest, dest_f, dest_v
    integer:: i, argc
    argc = GtArgCount()
    if (argc < 2) then
        print *, "usage: gtcopy source ... dest"
        stop
    else if (argc == 2) then
        call GtArgGet(1, source)
        call GtArgGet(argc, dest)
        call do_copy(source, dest)
    else
        call GtArgGet(argc, dest)
        call UrlSplit(dest, file=dest_f)
        do, i = 1, argc - 1
            call GtArgGet(i, source)
            call UrlSplit(source, var=dest_v)
            dest = UrlMerge(file=dest_f, var=dest_v)
            call do_copy(source, dest)
        enddo
    endif
contains

    subroutine do_copy(source, dest)
        type(VSTRING), intent(in):: source
        type(VSTRING), intent(in):: dest
        type(GT_VARIABLE):: vSource
        type(GT_VARIABLE):: vDest
        call Open(vSource, url=source)
        call Create(vDest, url=dest, copyfrom=vSource, copyvalue=.TRUE.)
        call Close(vSource)
        call Close(vDest)
    end subroutine

end program
