1+ #! /bin/env bash
2+ # ##############################################################################
3+ # Purpose: The purpose of this script is to simply generate a .deb file.
4+ # Once the .deb file is generated a copy is moved to /tmp.
5+ #
6+ # It can be installed and uninstalled like so:
7+ # $ sudo apt -y install ./chatgpt-shell-cli_0.0.1_all.deb
8+ # $ sudo apt -y remove chatgpt-shell-cli
9+ # ##############################################################################
10+
11+ # Functions
12+ make_directory () {
13+ local filepath=" $1 "
14+ local shouldRemake=$2
15+
16+ if [ $shouldRemake -ne 0 ]; then # delete old directory first if desired
17+ rm -rf " $filepath "
18+ fi
19+
20+ mkdir -p " $filepath "
21+ }
22+
23+ add_installation_files () {
24+ local ABSOLUTE_INSTALL_DIRECTORY=" $1 "
25+ local PROGRAM_NAME=" $2 "
26+ local ARCHITECTURE=" $3 "
27+ local PROGRAM_FILEPATH=" ${ABSOLUTE_INSTALL_DIRECTORY} /${PROGRAM_NAME,,} "
28+
29+ # Place the files in the directory based on the architecture
30+ # curl -sS https://raw.githubusercontent.com/0xacx/chatGPT-shell-cli/main/chatgpt.sh -o "$PROGRAM_FILEPATH"
31+ cp ../chatgpt.sh " $PROGRAM_FILEPATH "
32+
33+ # Configure open image command with xdg-open for Linux systems (copied from install.sh)
34+ if [[ " $OSTYPE " == " linux" * ]] || [[ " $OSTYPE " == " freebsd" * ]]; then
35+ sed -i ' s/open "${image_url}"/xdg-open "${image_url}"/g' " $PROGRAM_FILEPATH "
36+ fi
37+ }
38+
39+ set_attributes () {
40+ local STARTING_DIRECTORY=" $1 "
41+ local IN_DEB_PROGRAM_NAME=" $2 "
42+
43+ find " ${STARTING_DIRECTORY} " -type d -exec chmod 0755 {} \; # set all dirs
44+ find " ${STARTING_DIRECTORY} " -type f -exec chmod 0644 {} \; # set all files
45+ find " ${STARTING_DIRECTORY} " -name " ${IN_DEB_PROGRAM_NAME} " -type f -exec chmod 0755 {} \; # set executable
46+ find " ${STARTING_DIRECTORY} " -name " postinst" -type f -exec chmod 0755 {} \; # set postinst
47+ }
48+
49+ add_control_file () {
50+ local CONTROL_FILE=" $1 "
51+
52+ SECTION=" utils"
53+ DEPENDS=" curl (>= 7.68), jq (>= 1.6)" # HELPFUL: Use `$ dpkg-query -W -f='${VERSION}\n' depNameHere` to find version info
54+ MAINTAINER=" Alwyn \" KeyboardSage\" Berkeley <sagedev@leaflesstree.com>"
55+ HOMEPAGE=" https://gptshell.cc"
56+ DESCRIPTION=" A simple, lightweight shell script to use OpenAI's chatGPT and
57+ DALL-E from the terminal without installing python or node.js."
58+ cat << EOF > "$CONTROL_FILE "
59+ Package: $PACKAGE_NAME
60+ Version: $PROGRAM_VERSION
61+ Architecture: $ARCHITECTURES
62+ Section: $SECTION
63+ Essential: no
64+ Priority: optional
65+ Depends: $DEPENDS
66+ Maintainer: $MAINTAINER
67+ Homepage: $HOMEPAGE
68+ Description: $DESCRIPTION
69+ EOF
70+ }
71+
72+ add_postinst_file () {
73+ local POSTINST=" $1 "
74+ ALIAS_NAME=" chatgpt.sh"
75+
76+ cat << EOF > "$POSTINST "
77+ #!/bin/env bash
78+
79+ # only continue if the file exists
80+ if [ -f "/usr/bin/${PACKAGE_NAME} " ]; then
81+
82+ echo "File /usr/bin/${PACKAGE_NAME} exists."
83+
84+ # Make Alias (no sudo, already running as root)
85+ ln -s /usr/bin/${PACKAGE_NAME} /usr/bin/${ALIAS_NAME}
86+
87+ # Show user message
88+ echo "Installed $PACKAGE_NAME script to /usr/bin/${PACKAGE_NAME} "
89+ echo "The OPENAI_KEY environment variable containing your key is necessary for the script to function."
90+ echo -e "Add the line below to your shell profile with a valid key:\n"
91+ echo "export OPENAI_KEY=\"your_key_here\""
92+ echo -e "\nIf needed, detailed instructions are available:\nhttps://github.com/0xacx/chatGPT-shell-cli/tree/main#manual-installation"
93+
94+ fi
95+
96+ exit 0
97+ EOF
98+ }
99+
100+ # Main
101+ cd ` dirname $0 ` # Run from the directory that stores the script
102+
103+ # Remove the old .debs if there are any
104+ PROGRAM_NAME=" chatGPT-shell-cli"
105+ PACKAGE_NAME=" ${PROGRAM_NAME,,} "
106+ rm -f " ${PACKAGE_NAME} *.deb" > /dev/null
107+ sudo rm -f " /tmp/${PACKAGE_NAME} .deb" > /dev/null
108+
109+ # Stage directory
110+ RELATIVE_STAGING_DIR=" ./debpkgs"
111+ ABSOLUTE_STAGING_DIR=$( readlink --canonicalize " ${RELATIVE_STAGING_DIR} " )
112+ make_directory " $ABSOLUTE_STAGING_DIR " 1 # remake staging directory
113+
114+ # Architectures
115+ ABSOLUTE_ARCH_DIRECTORIES=()
116+
117+ PROGRAM_VERSION=" 0.0.1" # No versioning at the moment, 0.0.1 is a placeholder
118+ ARCHITECTURES=" all"
119+ for arch in ` echo $ARCHITECTURES ` ; do
120+ architectureDirectory=" ${ABSOLUTE_STAGING_DIR} /${PACKAGE_NAME} _${PROGRAM_VERSION} _${arch} "
121+ make_directory " $architectureDirectory " 0
122+ ABSOLUTE_ARCH_DIRECTORIES+=(" $architectureDirectory " )
123+ done
124+
125+ # Installation Directories and Installation Files
126+ INSTALL_DIRECTORIES=(" /usr/bin" )
127+ for (( i= 0 ; i< ${# ABSOLUTE_ARCH_DIRECTORIES[@]} ; i++ )) ; do # For each arch directory...
128+ for dir in " ${INSTALL_DIRECTORIES[@]} " ; do
129+ # Create the install directories needed...
130+ installDirectory=" ${ABSOLUTE_ARCH_DIRECTORIES[i]}${dir} "
131+ make_directory " $installDirectory " 0
132+ # DEBUG: echo "Architecture directory $((i+1)): ${ABSOLUTE_ARCH_DIRECTORIES[i]} processed and now has $installDirectory"
133+
134+ # Create the installation file(s) needed based on the install directory
135+ add_installation_files " $installDirectory " " $PROGRAM_NAME " " ${ABSOLUTE_ARCH_DIRECTORIES[i]##* _} "
136+ done
137+ done
138+
139+ # For each architecture directory make the...
140+ for (( i= 0 ; i< ${# ABSOLUTE_ARCH_DIRECTORIES[@]} ; i++ )) ; do
141+ # ...DEBIAN directory
142+ ABSOLUTE_DEBIAN_DIR=" ${ABSOLUTE_ARCH_DIRECTORIES[i]} /DEBIAN"
143+ make_directory " ${ABSOLUTE_DEBIAN_DIR} " 0
144+
145+ # ...Post installation file
146+ add_postinst_file " ${ABSOLUTE_DEBIAN_DIR} /postinst"
147+
148+ # ...Control file
149+ add_control_file " ${ABSOLUTE_DEBIAN_DIR} /control"
150+
151+ # ...then measure the staged size of the package and add that to the control file
152+ STAGING_SIZE_IN_KB=" $( du -s ${ABSOLUTE_ARCH_DIRECTORIES[i]} | awk ' {print $1;}' ) "
153+ echo " Installed-Size: ${STAGING_SIZE_IN_KB} " >> " ${ABSOLUTE_DEBIAN_DIR} /control"
154+ done
155+
156+ # Ensure proper file attributes on all files for all architectures
157+ set_attributes " ${ABSOLUTE_STAGING_DIR} " " ${PROGRAM_NAME,,} "
158+
159+ # Build
160+ for (( i= 0 ; i< ${# ABSOLUTE_ARCH_DIRECTORIES[@]} ; i++ )) ; do
161+ PROGRAM_ARCH=" ${ABSOLUTE_ARCH_DIRECTORIES[i]##* _} "
162+ DEB_FILENAME=" ${PACKAGE_NAME} _${PROGRAM_VERSION} _${PROGRAM_ARCH} .deb"
163+ dpkg-deb --root-owner-group --build " ${ABSOLUTE_ARCH_DIRECTORIES[i]} " " $DEB_FILENAME "
164+ chmod 644 " $DEB_FILENAME "
165+ sudo cp " $DEB_FILENAME " /tmp # Copied to /tmp for testing since apt is not allowed to access /home/*
166+ done
0 commit comments