Skip to content

Commit 4307aeb

Browse files
committed
the main thing
1 parent f096a60 commit 4307aeb

File tree

1 file changed

+120
-53
lines changed

1 file changed

+120
-53
lines changed

fpkg

Lines changed: 120 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,28 @@
11
#!/bin/bash
22

3-
FPKGDIR="/usr/local/fpkg/" # where all packages are located at
4-
EDITOR="vim" # change this to your favorite editor
3+
FPKGDIR="/usr/local/fpkg" # where all packages are located at
4+
EDITOR="vim" # change this to your preferred editor
55

6-
VERSION="2.1.2" # not sure why i want a variable for that honestly
6+
# logging stuff, leave them commented out
7+
# to keep disabled
8+
#FPKG_LOG="/tmp/fpkg_log" # log directory, /tmp makes sense to me
9+
#LOG_FMT="%Y%m%d-%H%M%S" # the format used to timestamp the log files
10+
# see man page of date for more
711

12+
# to restore the terminal state and quit with a code
813
quit () {
914
popd > /dev/null # restore user's working directory
1015
stty echo # unhide their keystrokes
1116
exit $1 # exit with a given code
1217
}
1318

14-
error () {
19+
# to display an error and also quit with non-zero
20+
error () {
1521
echo "ERROR: $1" # use argument as the error message
1622
quit 1 # exit with error
1723
}
1824

25+
# to check if a package exists
1926
exist_check () {
2027
if [[ -z $1 ]]; then
2128
error "package name was not provided!"
@@ -27,6 +34,7 @@ exist_check () {
2734
fi
2835
}
2936

37+
# to check if elevated privileges are needed
3038
write_check () {
3139
# get return code to see if we can write to any of the files
3240
touch $FPKGDIR > /dev/null 2>&1
@@ -36,12 +44,23 @@ write_check () {
3644
touch $FPKGDIR/ii > /dev/null 2>&1
3745
ret=$((ret + $?))
3846

39-
if [[ $ret > 0 ]]; then # if any of the touch's fail
47+
# if any of the touch's fail
48+
if [[ $ret > 0 ]]; then
4049
error "you cannot write to $FPKGDIR! Please run as root!"
4150
fi
4251
}
4352

53+
# to perform an update
54+
update () {
55+
# not sure if that's a good implementation
56+
# of it
57+
git pull --recurse-submodules --rebase
58+
# and then move to the previous dir
59+
cd - > /dev/null
60+
}
61+
4462
# check if $FPKGDIR exists in the first place
63+
# else perform an initial setup
4564
if [[ ! -d $FPKGDIR ]]; then
4665
stty echo
4766
echo "ERROR: $FPKGDIR does not exist! Create it now? [Y/n]"
@@ -60,53 +79,80 @@ pushd . > /dev/null
6079
cd $FPKGDIR
6180

6281
case $1 in
63-
# refresh every package using git pull
82+
# refresh the packages with `git pull'
6483
"update" | "u")
65-
entries=$(cat pkg.list | wc -l) # how many packages do we have?
84+
# no extra arguments? then all packages will be checked
85+
if [[ -z $2 ]]; then
86+
entries=$(cat pkg.list | wc -l) # how many packages do we have?
6687

67-
for ((i = 1; i <= $entries; i++)); do
68-
pkg_dir=$(head -n $i pkg.list | tail -1)
69-
cd $pkg_dir > /dev/null 2>&1
88+
for ((i = 1; i <= $entries; i++)); do
89+
pkg_dir=$(head -n $i pkg.list | tail -1)
90+
cd $pkg_dir > /dev/null 2>&1
7091

71-
if [[ $? != 0 ]]; then
72-
error "$pkg_dir does not exist! Remove its entry
92+
if [[ $? != 0 ]]; then
93+
error "$pkg_dir does not exist! Remove its entry
7394
with 'fpkg delete' and try again. Aborting..."
74-
fi
95+
fi
7596

76-
echo "$pkg_dir:"
77-
git pull --recurse-submodules # where the update itself happens
78-
cd - > /dev/null
79-
done
80-
;;
97+
echo "$pkg_dir:"
98+
update $pkg_dir
99+
done
100+
# refreshing just a single package?
101+
else
102+
exist_check $2 # check if it exists first
103+
cd $2 # change to its directory then
104+
update $2 # and perform the update
105+
fi
106+
;;
81107

82108
# requested the installation of a package?
83109
"install" | "i")
84110
exist_check $2
111+
write_check
85112

86113
cd $2
87114

88-
$FPKGDIR/ii/$2.ii install
89-
;;
115+
if [[ $FPKG_LOG && $LOG_FMT ]]; then
116+
# check if the log dir exists first
117+
# as it's by default inside /tmp
118+
if [[ ! -d $FPKG_LOG ]]; then
119+
mkdir $FPKG_LOG
120+
fi
121+
122+
# $LOG_FMT gets expanded here for creating the file
123+
LOGFILE=$FPKG_LOG/$(printf "$2-%($LOG_FMT)T")
124+
125+
$FPKGDIR/ii/$2.ii install | tee -a $LOGFILE
126+
127+
# warn the user about it
128+
echo -e \\n"Build log saved to $LOGFILE."
129+
else
130+
$FPKGDIR/ii/$2.ii install
131+
fi
132+
;;
90133

91134
# requested the uninstallation of a package?
92135
"remove" | "r")
93136
exist_check $2
137+
write_check
94138

95139
cd $2
96140

97141
$FPKGDIR/ii/$2.ii remove
98-
;;
142+
;;
99143

100144
# requested the list of registered packages?
101145
"list" | "l")
102146
cat pkg.list
147+
103148
echo -e \\n"Total: $(cat pkg.list | wc -l)" # see how many packages we have too
104-
;;
149+
;;
105150

106151
# adding an entry to the list?
107152
"add" | "a")
108153
write_check
109154

155+
# and check if we even have a package name
110156
if [[ -z $1 ]]; then
111157
error "package name was not provided!"
112158
fi
@@ -118,76 +164,70 @@ case $1 in
118164
echo $2 >> pkg.list
119165

120166
# write a template for it to make it easier for the user
121-
echo -e "# Installation instructions for package $2"\\n > ii/$2.ii
167+
# i couldn't get a single call to echo to work so i had to
168+
# come up with this
169+
echo "# Installation instructions for package $2" > ii/$2.ii
122170
echo "# Inside install (), write the commands for" >> ii/$2.ii
123171
echo "# installing the package," >> ii/$2.ii
124172
echo "# and inside remove (), write the commands for" >> ii/$2.ii
125-
echo -e "# uninstalling it too."\\n >> ii/$2.ii
126-
127-
# write a commented out privilege check section so that if
128-
# there're any steps requiring elevated privileges a check
129-
# is made for it
130-
echo -e "#if [[ \$(whoami) != \"root\" ]]; then" >> ii/$2.ii
131-
echo -e "#\\techo \"This action requires root privileges!\"" >> ii/$2.ii
132-
echo -e "#\\texit 1" >> ii/$2.ii
133-
echo -e "#fi"\\n >> ii/$2.ii
173+
echo -e "# uninstalling it too."\\n\\n >> ii/$2.ii
134174

135175
echo -e "install () {"\\n\\n"}"\\n >> ii/$2.ii
136176
echo -e "remove () {"\\n\\n"}"\\n >> ii/$2.ii
137177

138178
# this is necessary for being able to call one of the functions when needed
139-
echo "call=\$1; \$call" >> ii/$2.ii
179+
echo "# DO NOT REMOVE! This is used for calling the" >> ii/$2.ii
180+
echo "# functions from this ii..." >> ii/$2.ii
181+
echo "call=\$1; \$call" >> ii/$2.ii
140182

141183
# open up the text editor so that the user can write on
142184
# that template
143185
$EDITOR ii/$2.ii
144186

145187
# finally, make it executable so that fpkg can run it
146188
chmod +x ii/$2.ii
147-
;;
189+
;;
148190

149-
# just checking the .ii?
191+
# just checking an .ii?
150192
"peek" | "p")
151193
exist_check $2
152194

153195
cat ii/$2.ii
154-
;;
196+
;;
155197

156198
# going to the package's directory?
157199
"goto" | "g")
200+
exist_check $2
201+
158202
# see if we even have the .bashrc to begin with
159203
if [[ ! -r $FPKGDIR/.bashrc ]]; then
160204
error "your \$FPKGDIR doesn't have an existing or readable .bashrc!"
161205
fi
162206

163207
stty echo
164-
165-
exist_check $2
166208

167209
export FPKGDIR # those are necessary to make it able to start the
168210
export PKG_DIR=$2 # shell at the desired directory
169211

170212
sh -c 'cd $FPKGDIR/$PKG_DIR; # change to the directory
171213
echo "Working on $PKG_DIR/, ^D to exit"; # make the user aware of it
172214
exec bash --rcfile $FPKGDIR/.bashrc' # and here it goes
173-
;;
215+
;;
174216

175217
# editing a package's .ii?
176218
"edit" | "e")
177-
write_check
178-
179219
exist_check $2
220+
write_check
180221

181222
stty echo
182223

183224
$EDITOR ii/$2.ii
184-
;;
225+
;;
185226

186227
# delete an entry from the list?
187228
"delete" | "d")
188-
write_check
189-
190229
exist_check $2
230+
write_check
191231

192232
stty echo
193233
entry=$(grep -w "^$2" pkg.list -n | awk -F: '{ print $1 }') # get index of the package
@@ -201,7 +241,7 @@ case $1 in
201241
read dir
202242
rm -rf $dir
203243
fi
204-
;;
244+
;;
205245

206246
# requested the message of last git commit?
207247
"message" | "m")
@@ -216,41 +256,68 @@ case $1 in
216256
if [[ $1 = "-d" ]]; then
217257
git show
218258
else
219-
echo $(git log -1 --pretty=%B)
259+
git --no-pager log -1 --pretty="%s: %b"
220260
fi
221-
;;
261+
;;
262+
263+
# requested the commit history?
264+
"history" | "H")
265+
# regular expression to check if argument
266+
# is a number
267+
number_check="^[0-9]+$"
268+
269+
# shift then
270+
if [[ $2 =~ $number_check ]]; then
271+
shift
272+
fi
273+
274+
exist_check $2
275+
cd $2
276+
277+
# and change the depth to that
278+
if [[ $1 =~ $number_check ]]; then
279+
DEPTH=$1
280+
else # or not
281+
DEPTH=5
282+
fi
283+
284+
git --no-pager log -$DEPTH --pretty="(%cd) %s: %b"
285+
;;
222286

223287
# requested the version?
224288
"version" | "v")
225-
echo "fpkg - version $VERSION"
226-
echo "2024 ruby R53 (https://github.com/ruby-R53)"
227-
;;
289+
echo "fpkg - version 3.0.0-beta1"
290+
echo "ruby R53 (https://github.com/ruby-R53), May 2024"
291+
;;
228292

229293
# didn't even input a valid action or wants to know
230294
# them?
231295
"help" | "h" | *)
232296
echo "Usage: $0 <action> [package]"
233297
echo "Actions:"
234298
echo "help | h - shows this help message"
235-
echo "update | u - git pull every package listed in pkg.list"
299+
echo "update | u [pkg] - git pull every package listed in pkg.list, you"
300+
echo " can optionally update only [pkg] as well"
236301
echo "install | i <pkg> - install <pkg> using \$FPKGDIR/ii/<pkg>.ii"
237302
echo "remove | r <pkg> - uninstall <pkg> using \$FPKGDIR/ii/<pkg>.ii also"
238303
echo "list | l - list registered packages"
239304
echo "add | a <pkg> - register <pkg>"
240305
echo "peek | p <pkg> - take a peek at <pkg>'s .ii"
241306
echo "goto | g <pkg> - go to <pkg>'s directory"
242307
echo "edit | e <pkg> - edit <pkg>'s ii file"
243-
echo "delete | d <pkg> - remove <pkg>"
308+
echo "delete | d <pkg> - remove <pkg> from pkg.list"
244309
echo "message | m [-d] <pkg> - get comment from last commit of <pkg>. Takes an"
245310
echo " optional -d for getting its diff"
311+
echo "history | H [n] <pkg> - get <pkg>'s commit history. Takes an optional"
312+
echo " [n] to get a custom depth. Default is $DEPTH"
246313
echo "version | v - get fpkg's version"
247314

248315
# exit with error if input wasn't
249316
# for the help text
250317
if [[ $1 != "help" ]]; then
251318
quit 1
252319
fi
253-
;;
320+
;;
254321
esac
255322

256323
# and the program is done!

0 commit comments

Comments
 (0)