#!/usr/local/bin/pgoblin
  Copyright (C)2021..2023 @BABOLO http://www.babolo.ru/
  pkg = babolo-libmake
  All rights reserved.

  Redistribution and use in source and binary forms, with or without
  modification, are permitted provided that the following conditions
  are met:
  1. Redistributions of source code must retain the above copyright
     notice, this list of conditions and the following disclaimer.
  2. Redistributions in binary form must reproduce the above copyright
     notice, this list of conditions and the following disclaimer in the
     documentation and/or other materials provided with the distribution.

  THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  SUCH DAMAGE.
 
  $Id: copyrtest,v 1.4 2023/03/08 00:05:44 babolo Exp $

#connect 0001
sqlite3 :memory:
#cat     N
/dev/null
#job     00001
local -Z 1
#echo    Q
##Q copy E
#style   00Q00Q
Q
#perform 0001
CREATE TEMP TABLE a(i int, a text);
#perform 0001
CREATE TEMP TABLE o(o text);
#perform 0001
CREATE TEMP TABLE f(i int, f text);
#getarg  _
#copyin  00_1
COPY a FROM stdin;
#wait
#close   _0_
#perform 0001
INSERT INTO o SELECT (SELECT a FROM a WHERE i = 1 LIMIT 1);
#perform 0001
UPDATE o SET o = '.' WHERE o IS NULL;
#select  _001
SELECT '/usr/bin/find', o, '-type', 'f' FROM o;
#exec    1_N
#getstr  201
#copyin  0021
COPY f FROM stdin;
#wait
#close   101
#close   202
#echo    _
BEGIN{
    y = ""
    f = ""
}

/^.*Copyright +\(C\)[12][0-9]+.*$/{
    t = $0
    if  ($0 ~ /^.*Copyright +\(C\)[12][0-9]+\.\.[12][0-9]+ .*$/){
        sub(/^.*Copyright +\(C\)[12][0-9]+\.\./, "", t)
        sub(/ .*$/, "", t)
    } else if ($0 ~ /^.*Copyright +\(C\)([12][0-9]+, *)+[12][0-9]+ .*$/){
        sub(/^.*Copyright +\(C\)([12][0-9]+, *)+/, "", t)
        sub(/ .*$/, "", t)
    } else{
        sub(/^.*Copyright +\(C\)/, "", t)
        sub(/ .*$/, "", t)
    }
    if  ((y != "") && (y != t)){
        print t " <> " y " " FILENAME
        print "_ _ _ _ _ _ " f
        print "_ _ _ _ _ _ " $0
        exit
    }
    y = t
    f = $0
}

$0 ~ "\\$" "Id: [^ ]+ [0-9.]+ [12][0-9]+\\/[0-9][0-9]\\/[0-9][0-9] [0-9][0-9]:[0-9][0-9]:[0-9][0-9] [^ ]+ Exp \\$"{
    t = $0
    sub(/^.*\$Id: [^ ]+ [0-9.]+ /, "", t)
    sub(/\/[0-9][0-9]\/[0-9][0-9] [0-9][0-9]:[0-9][0-9]:[0-9][0-9] [^ ]+ Exp \$.*$/, "", t)
    if  ((y != "") && (y != t)){
        print t " <> " y " " FILENAME
        print "_ _ _ _ _ _ " f
        print "_ _ _ _ _ _ " $0
        exit
    }
    y = t
    f = $0
}

#select  10_1
SELECT '/usr/bin/awk', ?, f FROM f ORDER BY i;
#exec    01N01
#wait    00001
#exit
