#!/bin/bash

cd $(dirname "${BASH_SOURCE[0]}")

# source file => output file => package name
files=(
../../pkg/limiter/limiter.go limiter/limiter.go limiter
../../pkg/stats/tracer.go stats/tracer.go stats
../../pkg/remote/bytebuf.go remote/bytebuf.go remote
../../pkg/remote/trans_handler.go remote/trans_handler.go remote
../../pkg/remote/codec.go remote/codec.go remote
../../pkg/remote/remotecli/conn_wrapper.go remote/conn_wrapper.go remote
../../pkg/remote/connpool.go remote/connpool.go remote
../../pkg/remote/dialer.go remote/dialer.go remote
../../pkg/remote/trans_meta.go remote/trans_meta.go remote
../../pkg/remote/trans_pipeline.go remote/trans_pipeline.go remote
../../pkg/remote/payload_codec.go remote/payload_codec.go remote
../../pkg/generic/generic_service.go generic/generic_service.go generic
../../pkg/klog/log.go klog/log.go klog
../../internal/generic/thrift/thrift.go generic/thrift.go generic
../../pkg/discovery/discovery.go discovery/discovery.go discovery
../../pkg/loadbalance/loadbalancer.go loadbalance/loadbalancer.go loadbalance
../../pkg/proxy/proxy.go proxy/proxy.go proxy
../../pkg/utils/sharedticker.go utils/sharedticker.go utils
../../../netpoll/connection.go netpoll/connection.go netpoll
../../../gopkg/bufiox/bufreader.go bufiox/bufreader.go bufiox
$GOROOT/src/net/net.go net/net.go net
)

i=0
while [ $i -lt ${#files[@]} ]
do
    infile=${files[$i]}
    let i++
    outfile=${files[$i]}
    let i++
    package=${files[$i]}
    let i++

    if [ ! -f $infile ]; then
        echo "ERROR: $infile not existing"
        exit 1
    fi

    # echo $infile
    # echo $outfile
    mockgen -source=$infile -package=$package > $outfile || exit 1
    if [[ "$OSTYPE" =~ ^darwin ]];
    then
      sed -i '' -e '1i \
/*\
\ * Copyright 2022 CloudWeGo Authors\
\ *\
\ * Licensed under the Apache License, Version 2.0 (the "License");\
\ * you may not use this file except in compliance with the License.\
\ * You may obtain a copy of the License at\
\ *\
\ *     http://www.apache.org/licenses/LICENSE-2.0\
\ *\
\ * Unless required by applicable law or agreed to in writing, software\
\ * distributed under the License is distributed on an "AS IS" BASIS,\
\ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\
\ * See the License for the specific language governing permissions and\
\ * limitations under the License.\
*/\
\
' $outfile
    else
      sed -i '' -e '1i /*\
\ * Copyright 2021 CloudWeGo Authors\
\ *\
\ * Licensed under the Apache License, Version 2.0 (the "License");\
\ * you may not use this file except in compliance with the License.\
\ * You may obtain a copy of the License at\
\ *\
\ *     http://www.apache.org/licenses/LICENSE-2.0\
\ *\
\ * Unless required by applicable law or agreed to in writing, software\
\ * distributed under the License is distributed on an "AS IS" BASIS,\
\ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\
\ * See the License for the specific language governing permissions and\
\ * limitations under the License.\
 */\
\
' $outfile
    fi
done
