This repository has been archived by the owner on Dec 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdv2.install
executable file
·194 lines (174 loc) · 6.37 KB
/
dv2.install
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#!/bin/bash
#shellcheck disable=SC2174
# DéjàVu2 GPT Chatbot/Scriptbot
# Linux terminal interface for OpenAI GPTs
# Gary Dean garydean@yatti.id
# https://github.com/Open-Technology-Foundation/dejavu2
set -euo pipefail
shopt -s extglob globstar checkwinsize
declare -r PRGNAME='DéjàVu2'
declare -r AUTHOR='Gary Dean, garydean@okusi.id'
declare -r REPOSITORY="https://github.com/Open-Technology-Foundation/dejavu2.git"
declare -r PRG0=$(readlink -en -- "$0")
declare -r PRGDIR="${PRG0%/*}"
declare -r PRG="${PRG0##*/}"
((EUID)) && { >&2 echo "$PRG requires root."; exit 1; }
declare -- PRGSHAREDIR=/usr/share/${PRG%.install}
#shellcheck disable=SC2155
declare -- Version=$(cat "$PRGDIR/.version")
#shellcheck disable=SC2034
declare -- PRGVER="$PRG $Version"
declare -i doApt=1 doUpgrade=0
dv2.install.help() {
cat <<-EOT
Name : $PRGNAME vs $Version - Installation
:
Script : $PRG
:
Desc : Installation for $PRGNAME GPT Terminal Chatbot/Scriptbot
:
: - Requires root/sudo to install.
: - apt update and upgrade will be executed before
: installation of python 3, pip and git.
: - ~/.bashrc and /etc/bash.bashrc will be modified
: to include openai environment variables.
: - program files are stored in $PRGSHAREDIR
: - executable 'dejavu2' is installed in /usr/local/bin/
: - a symlink 'dv2' is created for executable 'dejavu'
: in /usr/local/bin, if this does not already exist.
:
Synopsis: $PRG [-vqaVh]
:
Options : -v|--verbose Verbose on (default)
: -q|--quiet Verbose off.
: If environment key OPENAI_API_KEY
: has already been set you will not
: be prompted to input it.
: -a|--no-apt Do not execute apt during installation.
: -u|--upgrade Upgrade $PRGNAME from git repository.
: -V|--version Print $PRGNAME version.
:
Requires: python 3, pip, git, openai API key/s, root access for
: installation.
:
See Also: https://openai.com/api/
: $REPOSITORY
EOT
}
dv2.install() {
if ((UID)); then
if [[ " $(id -Gn) " != *" sudo "* ]]; then
echo 2> "Installation of $PRGNAME requires root access."
dv2.install.help
exit
fi
fi
cd "$PRGDIR"
local verbose_=1 quiet_='' tempdir
while (($#)); do case "$1" in
-u|--upgrade|--update)
doUpgrade=1 ;;
-a|--no-apt) doApt=0 ;;
-v|--verbose) verbose_=1; quiet_='' ;;
-q|--quiet) verbose_=0; quiet_='qq' ;;
-V|--version) echo "$PRGNAME vs $Version"; return 0 ;;
-h|--help) dv2.install.help; return 0 ;;
-N|--no-op) ;;
-[uavqVhN]*) #shellcheck disable=SC2046 # de-aggregate aggregated short options
set -- '' $(printf -- "-%c " $(grep -o . <<<"${1:1}")) "${@:2}";;
-?|--*) echo 2> "Invalid option '$1'" ;;
*) echo 2> "Invalid argument '$1'" ;;
esac; shift; done
if ((doUpgrade)); then
local -- tempdir="/tmp/$PRG-upgrade-$RANDOM"
git clone "$REPOSITORY" "$tempdir" || exit
exec "$tempdir/$PRG" \
-"$( ((verbose_)) && echo -n 'v' || echo -n 'q')" \
-"$( ((doApt)) && echo -n 'a' || echo -n 'N')"
exit
fi
((verbose_)) && { echo "$PRGNAME vs $Version"; echo ''; }
local -- key
key="${OPENAI_API_KEY:-}"
if [[ -z $key ]] || ((verbose_)); then
read -rep "Your openai API Key: " -i "$key" key
if [[ $key == '' ]]; then
echo 2> "You will require an API key from openai.com to run $PRG"
exit
fi
echo
fi
OPENAI_API_KEY="$key"; export OPENAI_API_KEY
key="${OPENAI_ORGANIZATION_ID:-}"
if ((verbose_)); then
read -rep "Your openai Organization ID (if required): " -i "$key" key
echo
fi
OPENAI_ORGANIZATION_ID="$key"; export OPENAI_ORGANIZATION_ID
if ((doApt)); then
sudo apt update -q"$quiet_"
sudo apt upgrade -q"$quiet_"
sudo apt install -q"$quiet_" python3 pip git bc pandoc shellcheck
fi
[[ -d "$PRGSHAREDIR" ]] || sudo mkdir -m 0775 -p "$PRGSHAREDIR"
cd "$PRGSHAREDIR"
set +e
mkdir -m 0775 -p "$PRGSHAREDIR"/{agents,doc,.sys_libs,etc}
cd "$PRGDIR" || exit 1
sudo rsync -al agents/* "$PRGSHAREDIR"/agents/
sudo rsync -al .sys_libs/* "$PRGSHAREDIR"/.sys_libs/
sudo rsync -al etc/* "$PRGSHAREDIR"/etc/
sudo cp -p doc/makedoc "$PRGSHAREDIR"/doc/
sudo chmod 775 "$PRGDIR"/dv2
sudo cp -p dv2 dv2.install dv2.1.gz LICENSE README.md .sys_conf .sys_lib .sys_version .version "$PRGSHAREDIR"/
set -e
cd /usr/local/bin || { sudo mkdir -p -m 755 /usr/local/bin; cd /usr/local/bin; }
sudo rm -f /usr/local/bin/dv2 /usr/local/bin/dejavu2
sudo ln -s --force "$PRGSHAREDIR"/dv2 dv2
sudo ln -s --force "$PRGSHAREDIR"/dv2 dejavu2
# manpages
cd "$PRGSHAREDIR"
[[ -d /usr/local/share/man/man1 ]] \
|| sudo mkdir -p -m 0755 /usr/local/share/man/man1
sudo rm -f /usr/local/man/man1/dv2.1.gz /usr/local/man/man1/dv2.1.gz
sudo cp -p "$PRGSHAREDIR"/dv2.1.gz /usr/local/man/man1/dv2.1.gz
sudo cp -p "$PRGSHAREDIR"/dv2.1.gz /usr/local/man/man1/dejavu2.1.gz
sudo mandb -sq
# default config agent
sudo rsync -a etc/default/dv2.conf /etc/default/
# ~/.bashrc /etc/bash.bashrc
local -- tmpfile file
tmpfile="/tmp/$PRG-$RANDOM"
for file in "$HOME/.bashrc" /etc/bash.bashrc; do
((verbose_)) && echo "Modifying '$file'"
if [[ ! -f "$file" ]]; then
echo "# This file created by $PRG ($PRGNAME)" > "$file"
chmod 664 "$file"
fi
grep -v '^[[:blank:]]*OPENAI_API_KEY=\|^[[:blank:]]*OPENAI_ORGANIZATION_ID=\|^[[:blank:]]*export OPENAI_' "$file" \
>"$tmpfile"
( echo "OPENAI_API_KEY=\"$OPENAI_API_KEY\""
echo "export OPENAI_API_KEY"
if [[ -n $OPENAI_ORGANIZATION_ID ]]; then
echo "OPENAI_ORGANIZATION_ID=\"$OPENAI_ORGANIZATION_ID\""
echo "export OPENAI_ORGANIZATION_ID"
fi
) >>"$tmpfile"
sudo chmod --reference="$file" "$tmpfile"
sudo chown --reference="$file" "$tmpfile"
sudo mv --backup "$tmpfile" "$file" \
|| { echo >&2 "'$tmpfile' could not be moved to '$file'!"; exit 1; }
done
if ((verbose_)); then
echo
echo "$PRGNAME has been installed."
echo
echo "To run, enter 'dv2' or 'dejavu2'"
echo
echo "Contribute! Develop and improve $PRGNAME."
echo " -> $REPOSITORY"
echo
fi
}
dv2.install "$@"
#fin