-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathlkhh-new
More file actions
executable file
·130 lines (113 loc) · 2.82 KB
/
lkhh-new
File metadata and controls
executable file
·130 lines (113 loc) · 2.82 KB
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#! /bin/bash
_rootdir=${_rootdir:-"$(dirname "$(readlink -f "$0")")"}
_ver="0.0.1-dev"
#_author="Wei Peng <me@1pengw.com>"
source "${_rootdir}/_shared_prologue"
#
# parameters
#
declare template=${template:-lkm_barebone}
declare project=${project:-mylkm}
declare author=${author:-$( (
# start subshell to contain env pollution
name=$(git config user.name);
: ${name:=$(whoami)};
echo $name;
) )}
declare license=${license:-GPL}
declare -i force=${force:-0}
#
# getopt processing
#
function my_getopt_cont () {
local n_shift=1 # number of shifts in the outer loop; shift by 1 by default
case "$1" in
-t|--template)
shift
template=$1
n_shift=2
;;
-p|--project)
shift
project=$1
n_shift=2
;;
-a|--author)
shift
author=$1
n_shift=2
;;
-l|--license)
shift
license=$1
n_shift=2
;;
-f|--force)
force=1
n_shift=1
;;
esac
return $n_shift
}
my_getopt "t:p:a:l:f" "template:,project:,author:,license:,force" "my_getopt_cont" "Create a new project from template." "$(
cat <<END
[-t|--template=<template>] [-p|--project=<project>]
[-a|--author=<author>] [-l|--license=<license>]
[-f|--force]
END
)" "$(
cat <<END
-t --template Template from which project is created (default: $template).
-p --project Project name; any sequence of non-C-symbol chars is replaced with underscore/_ (default: $project).
-a --author Author (default: $author).
-l --license License (default: $license).
-f --force Forcibly create a new project even it already exists (default: $force).
END
)" "$@"
ret=$?
eval set -- "$_getopt"
shift $ret
#
# normalization
#
# normalize project name for use in C source code
project=$(echo "$project" | sed 's/[^[:alnum:]_]\+/_/g; s/^[[:digit:]]\+/_/g;')
#
# sanity check
#
vecho 0 <<END
$(
dumpparam <<END1
$(
for name in template project author license force; do
echo "$name=$(echo $(eval "echo \$$name"))"
done
)
END1
)
END
template_dir="$_rootdir/templates/$template"
[[ -d "$template_dir" ]] || die 1 <<END
No such template: $template.
END
[[ -d "$project" ]] && (( force == 0 )) && die 1 <<END
$project already exists; force (-f) or try a new project.
END
mkdir -p "$project"
pushd "$project" &>/dev/null
cp -r "$template_dir/"* . || die 1 <<END
Fail to create $project from $template.
END
popd &>/dev/null
[[ -d "$project" ]] || die 1 <<END
Project $project is not created.
END
#
# Substitute
#
find $project -type f -name '*.template' | while IFS= read f; do
sed -i -e "s/<PROJECT>/$project/g; s/<AUTHOR>/$author/g; s/<LICENSE>/$license/g;" "$f"
rename '.template' '' "$f"
done
# evaluate rest of arguments after creating
eval "$@"