-
Notifications
You must be signed in to change notification settings - Fork 0
/
spm.sh
executable file
·150 lines (136 loc) · 2.47 KB
/
spm.sh
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#!/bin/bash
##### constants
FILENAME="spm.sh"
TITLE="Snooker Player Profile Manager v0.1"
DB_PATH="database/snooker.db"
BACKUP="database/backup.sql"
DB_DEF="database/ddl.sql"
DOC_PATH="doc/usage.txt"
#### functions
function build
{
echo "Building code..."
mkdir -p build/
cd build;
cmake ..
make
}
function run
{
if [ -z "$1" ]
then
echo "Error: Missing file path."
echo "Try spm.sh --help"
exit
fi
if [ ! -f "$1" ]
then
echo "File \"$1\" does not exist."
echo "Try spm.sh --help"
exit
fi
build/spm $1
}
function backup
{
sqlite3 -line $DB_PATH .dump > $BACKUP
}
function restore
{
if [ ! -f $BACKUP ]
then
echo "File $BACKUP does not exist."
echo "Try spm.sh --help"
exit
fi
rm $DB_PATH
sqlite3 $DB_PATH < $BACKUP
}
function search
{
if [ -z "$1" ]
then
echo "Error: Missing search keyword."
exit
fi
find . -iregex '.*\.\(cpp\|sql\|hpp\)' -exec grep -inH $1 {} \;
}
function count
{
find . -iregex '.*\.\(cpp\|sql\|hpp\|sh\)' -exec cat {} \; | wc -l
}
function db
{
sqlite3 --interactive -echo $DB_PATH
}
function syncdb
{
if [ ! -f $DB_DEF ]
then
echo "File $BACKUP does not exist."
echo "Try spm.sh --help"
exit
fi
rm $DB_PATH
touch $DB_PATH
sqlite3 $DB_PATH < $DB_DEF
}
function dbdump
{
sqlite3 -line $DB_PATH .dump
}
function usage
{
echo $TITLE
if [ ! -f $DOC_PATH ]
then
echo "Error: doc/usage.txt missing."
exit
fi
source $DOC_PATH
}
function spm_main
{
case $1 in
-b | --build | build)
build
;;
--rebuild | rebuild)
rm -rf ./build
build
;;
-r | --run | run)
run $2
;;
-s | --search | search)
search $2
;;
-c | --count | count)
count
;;
-db | --database | database)
db
;;
-dd | --dump | dump)
dbdump
;;
--restore | restore)
restore
;;
--backup | backup)
backup
;;
--syncdb | syncdb)
syncdb
;;
-h | --help | help)
usage
;;
*)
echo "Error: Invalid input option."
echo "Try spm.sh --help"
;;
esac
}
#### Main
spm_main $*