File: init-plugin

package info (click to toggle)
bro-aux 0.42-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 1,104 kB
  • sloc: sh: 999; ansic: 943; ruby: 71; makefile: 68; perl: 35; cpp: 26
file content (98 lines) | stat: -rwxr-xr-x 2,300 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#! /usr/bin/env bash

function abspath
{
    (cd "$1"; pwd)
}

function relpath
{
    echo "$1" | sed "s#`pwd`/\{0,1\}##g"
}

function lower
{
    echo "$1" | tr A-Z a-z
}

function upper
{
    echo "$1" | tr a-z A-Z
}

function init-skeleton
{
    for i in `cd ${skeleton} && find * -type d`; do
        mkdir -p `echo ${plugin_src}/$i`
    done

    for i in `cd ${skeleton} && find * .??* -type f`; do
        src=${skeleton}/$i
        dst=`echo ${plugin_src}/$i`
        dst=`echo $dst | sed "s#%NS_LOWER#${plugin_ns_lower}#g"`
        dst=`echo $dst | sed "s#%NAME_LOWER#${plugin_name_lower}#g"`
        dst=`echo $dst | sed "s#%NS#${plugin_ns}#g"`
        dst=`echo $dst | sed "s#%NAME#${plugin_name}#g"`
        dst=`echo $dst | sed "s#@#/#g"`

        tmp=$dst.$$.tmp

        mkdir -p `dirname ${dst}`
        cp -p ${src} ${tmp} # Copy first to preserve permissions.
        cat ${src} \
            | sed "s/@PLUGIN_NAME@/${plugin_name}/g" \
            | sed "s/@PLUGIN_NAMESPACE@/${plugin_ns}/g" \
            | sed "s/@PLUGIN_NAME_UPPER@/${plugin_name_upper}/g" \
            | sed "s/@PLUGIN_NAMESPACE_UPPER@/${plugin_ns_upper}/g" \
            | sed "s/@PLUGIN_NAME_LOWER@/${plugin_name_lower}/g" \
            | sed "s/@PLUGIN_NAMESPACE_LOWER@/${plugin_ns_lower}/g" \
            >${tmp}

        if [ -e ${dst} ]; then
            cmp -s ${tmp} ${dst} || echo "`relpath ${dst}` exists, not installing new version"
            rm -f ${tmp}
            continue
        fi

        echo Installing `relpath ${dst}` ...
        mv ${tmp} ${dst}

    done
}

update=0

if [ "$1" == "-u" ]; then
    update=1
    shift
fi

if [ $# != 3 ]; then
    echo "Usage: `basename $0` [-u] <dst-dir> <namespace> <plugin name>"
    exit 1
fi

dstdir=$1

if [ -e "${dstdir}" -a ${update} != 1 ]; then
    echo "error: ${dstdir} already exists, use -u if you want to update skeleton files in there."
    exit 1
fi

mkdir -p ${dstdir}
dstdir=`abspath ${dstdir}`

basedir=`dirname $0`
skeleton=`abspath ${basedir}`/skeleton
plugin_ns=$2
plugin_name=$3
plugin_ns_lower=`lower $2`
plugin_name_lower=`lower $3`
plugin_ns_upper=`upper $2`
plugin_name_upper=`upper $3`
plugin_src=${dstdir}
plugin_build=${dstdir}/dylib

init-skeleton

which git >/dev/null 2>&1 && (cd ${dstdir} && git init . && git add .)