11#! /bin/bash
22
33# where all packages are located at
4- FPKG_DIR =" /usr/local/fpkg"
4+ FPKG_ROOT =" /usr/local/fpkg"
55# change this to your preferred editor
66EDITOR=" vim"
77
88# logging stuff, leave them commented out
99# to keep disabled
10- # FPKG_LOG="/tmp/fpkg_log" # log directory, /tmp makes sense to me
10+ # FPKG_LOG="/tmp/fpkg_log" # log directory, /tmp seems to be a good choice
1111# LOG_FMT="%Y%m%d-%H%M%S" # the format used to timestamp the log files
1212 # see man page of `date' for more
1313
14- # to restore the terminal state and quit with a code
14+ # to restore the terminal state and exit with a code
1515quit () {
1616 popd > /dev/null # restore user's working directory quietly
1717 stty echo # unhide their keystrokes
@@ -26,67 +26,61 @@ error() {
2626
2727# to check if a package exists
2828exist_check () {
29- if [[ -z $1 ]]; then
30- error " package name was not provided!"
31- fi
29+ [[ -z $1 ]] && error " package name was not provided!"
3230
33- grep -w $1 pkg.list > /dev/null 2>&1 # running this just to get its return value
34- if [[ $? > 0 ]]; then
35- error " $1 not found in pkg.list! Was it a typo?"
36- fi
31+ # checking return type to see if the package exists
32+ grep -w $1 pkg.list > /dev/null
33+ [[ $? > 0 ]] && error " $1 not found in pkg.list! Was it a typo?"
3734}
3835
3936# to check if elevated privileges are needed
4037write_check () {
4138 # get return code to see if we can write to any of the files
42- touch $FPKG_DIR > /dev/null 2>&1
39+ touch $FPKG_ROOT > /dev/null
4340 RET=$(( RET + $? ))
44- touch $FPKG_DIR /pkg.list > /dev/null 2>&1
41+ touch $FPKG_ROOT /pkg.list > /dev/null
4542 RET=$(( RET + $? ))
46- touch $FPKG_DIR /ii > /dev/null 2>&1
43+ touch $FPKG_ROOT /ii > /dev/null
4744 RET=$(( RET + $? ))
4845
4946 # if any of the touch's fail
50- if [[ $RET > 0 ]]; then
51- error " you cannot write to $FPKG_DIR ! Please run as root!"
52- fi
47+ [[ $RET > 0 ]] && error " $FPKG_ROOT is unwriteable! Try running as root!"
5348}
5449
5550# to perform an update
5651update () {
5752 PULL_WITH=" --recurse-submodules=on-demand"
5853
5954 # check if rebase is required
60- if [[ $REBASE = " YES" ]]; then
61- PULL_WITH+=" --rebase"
62- fi
55+ [[ $REBASE = " YES" ]] && PULL_WITH+=" --rebase"
6356
6457 git pull $PULL_WITH
6558
66- # and then move to the previous dir
59+ # and then go to the previous dir
6760 cd - > /dev/null
6861}
6962
70- # check if $FPKG_DIR exists in the first place
63+ # check if $FPKG_ROOT exists in the first place
7164# else perform an initial setup
72- if [[ ! -d $FPKG_DIR ]]; then
65+ if [[ ! -d $FPKG_ROOT ]]; then
7366 stty echo
74- echo " ERROR: $FPKG_DIR does not exist! Create it now? [Y/n]"
67+ echo " ERROR: $FPKG_ROOT does not exist! Create it now? [Y/n]"
7568 read CHOICE
7669
7770 if [[ $CHOICE != " n" ]]; then # so that the default is "yes"
78- mkdir -pv $FPKG_DIR /ii # create the ii one too already
79- cp .bashrc $FPKG_DIR # and copy .bashrc to the main dir
71+ mkdir -pv $FPKG_ROOT /ii # create the ii one too already
72+ cp .bashrc $FPKG_ROOT # and copy .bashrc to the main dir
8073 fi
8174
8275 quit 1
8376fi
8477
8578# environment setup
86- stty -echo # hide keystrokes, note that they still get registered
79+ stty -echo # hide keystrokes, not sure how to discard them too
8780pushd . > /dev/null # save user's current directory
88- cd $FPKG_DIR # so that we can go to $FPKG_DIR
81+ cd $FPKG_ROOT # so that we can go to $FPKG_ROOT
8982
83+ # now, parse the command line arguments!
9084case $1 in
9185 # update all packages with `git pull'
9286 " update" | " u" )
@@ -97,11 +91,11 @@ case $1 in
9791 for (( i = 1 ; i <= $ENTRIES ; i++ )) ; do
9892 # a hacky way of getting the package name
9993 PKG_DIR=$( head -$i pkg.list | tail -1 | awk -F" " ' { print $1 }' )
100- cd $PKG_DIR > /dev/null 2>&1
94+ cd $PKG_DIR > /dev/null
10195
10296 if [[ $? != 0 ]]; then
10397 error " $PKG_DIR does not exist!\\ n
104- Remove its entry with \` fpkg delete' and try again .\\ n
98+ Remove its entry with \` fpkg delete' and retry .\\ n
10599 Aborting..."
106100 fi
107101
@@ -114,7 +108,7 @@ case $1 in
114108 cd $2 # change to its directory if so
115109
116110 # hacky way of checking if a package needs rebase or not
117- export $( grep " $2 REBASE" $FPKG_DIR /pkg.list | awk -F" " ' { print $2 }' )
111+ export $( grep " $2 REBASE" $FPKG_ROOT /pkg.list | awk -F" " ' { print $2 }' )
118112 # and perform the update
119113 update
120114 fi
@@ -127,33 +121,32 @@ case $1 in
127121
128122 cd $2
129123
130- # 10k IQ move here
131- export $( grep INTERACTIVE $FPKG_DIR /ii/$2 .ii)
124+ # check if the installation requires
125+ # interaction with the user
126+ export $( grep INTERACTIVE $FPKG_ROOT /ii/$2 .ii)
132127
133- # if the process requires some
134- # interaction with the user, unhide
135- # the keystrokes here
136- if [[ $INTERACTIVE = " YES" ]]; then
137- stty echo
138- fi
128+ # if it does, unhide keystrokes here
129+ [[ $INTERACTIVE = " YES" ]] && stty echo
139130
140131 if [[ $FPKG_LOG && $LOG_FMT ]]; then
141132 # check if the log dir exists first
142133 # as it's by default inside /tmp
143- if [[ ! -d $FPKG_LOG ]]; then
144- mkdir $FPKG_LOG
145- fi
134+ [[ ! -d $FPKG_LOG ]] && mkdir $FPKG_LOG
146135
147136 # $LOG_FMT gets expanded here for creating the file
148137 LOGFILE=$FPKG_LOG /$( printf " $2 -%($LOG_FMT )T" )
149138
150- $FPKG_DIR /ii/$2 .ii install | tee -a $LOGFILE
139+ $FPKG_ROOT /ii/$2 .ii install | tee -a $LOGFILE
151140
152141 # warn the user about it
153142 echo -e \\ n" Build log saved to $LOGFILE ."
154143 else
155- $FPKG_DIR /ii/$2 .ii install
144+ $FPKG_ROOT /ii/$2 .ii install
156145 fi
146+
147+ # and then, ring the terminal bell when done, that if
148+ # the operation has succeeded
149+ [[ $? = 0 ]] && tput bel
157150 ;;
158151
159152 # requested the uninstallation of a package?
@@ -163,7 +156,7 @@ case $1 in
163156
164157 cd $2
165158
166- $FPKG_DIR /ii/$2 .ii remove
159+ $FPKG_ROOT /ii/$2 .ii remove
167160 ;;
168161
169162 # requested the list of registered packages?
@@ -176,17 +169,13 @@ case $1 in
176169
177170 # adding an entry to the list?
178171 " add" | " a" )
179- grep -w $2 pkg.list > /dev/null 2>&1
180- if [[ $? = 0 ]]; then
181- error " $2 is already registered!"
182- fi
172+ grep -w $2 pkg.list > /dev/null
173+ [[ $? = 0 ]] && error " $2 is already registered!"
183174
184175 write_check
185176
186177 # and check if we even have a package name
187- if [[ -z $1 ]]; then
188- error " package name was not provided!"
189- fi
178+ [[ -z $1 ]] && error " package name was not provided!"
190179
191180 # make keystrokes visible again
192181 stty echo
@@ -220,9 +209,9 @@ case $1 in
220209 echo -e " #\\ texit 1" >> ii/$2 .ii
221210 echo -e " #fi" \\ n >> ii/$2 .ii
222211
223- echo " # Put 'YES' if interaction is required" >> ii/$2 .ii
212+ echo " # Say 'YES' if interaction is required" >> ii/$2 .ii
224213 echo " # to install this package, otherwise keep" >> ii/$2 .ii
225- echo " # this 'NO'. This is required !!" >> ii/$2 .ii
214+ echo " # this 'NO'. Don't remove this line !!" >> ii/$2 .ii
226215 echo -e " INTERACTIVE=NO" \\ n >> ii/$2 .ii
227216
228217 echo -e " install() {" \\ n\\ n" }" \\ n >> ii/$2 .ii
@@ -253,18 +242,18 @@ case $1 in
253242 exist_check $2
254243
255244 # see if we even have the .bashrc to begin with
256- if [[ ! -r $FPKG_DIR /.bashrc ]]; then
257- error " your \$ FPKG_DIR doesn't have an existing or readable .bashrc!"
245+ if [[ ! -r $FPKG_ROOT /.bashrc ]]; then
246+ error " no existing or readable .bashrc on \$ FPKG_ROOT !"
258247 fi
259248
260249 stty echo
261250
262- export FPKG_DIR # those are necessary to make it able to start the
251+ export FPKG_ROOT # those are necessary to make it able to start the
263252 export PKG_DIR=$2 # shell at the desired directory
264253
265- sh -c ' cd $FPKG_DIR /$PKG_DIR; # change to the directory
254+ sh -c ' cd $FPKG_ROOT /$PKG_DIR; # go to that directory
266255 echo "Working on $PKG_DIR/, ^D to exit"; # tell the user about it
267- exec bash --rcfile $FPKG_DIR /.bashrc' # and here it goes
256+ exec bash --rcfile $FPKG_ROOT /.bashrc' # and here it goes
268257 ;;
269258
270259 # editing a package's .ii?
@@ -285,25 +274,21 @@ case $1 in
285274 stty echo
286275 # get index number of package
287276 ENTRY=$( grep -w " ^$2 " pkg.list -n | awk -F: ' { print $1 }' )
288- sed -i " $ENTRY " d pkg.list # and delete the package using it
277+ sed -i " $ENTRY " d pkg.list # and delete the package from it
289278 rm ii/$2 .ii
290279 echo " $2 removed from pkg.list, would you like to remove its files"
291280 echo " as well? [y/N]"
292281 read CHOICE
293282
294283 if [[ $CHOICE = ' y' ]]; then # same thing but default'ing to "no"
295284 rm -rf $2
296- if [[ $? = 0 ]]; then
297- echo " Done"
298- fi
285+ [[ $? = 0 ]] && echo " Done"
299286 fi
300287 ;;
301288
302289 # requested the message of last git commit?
303290 " message" | " m" )
304- if [[ $2 = " -d" ]]; then
305- shift
306- fi
291+ [[ $2 = " -d" ]] && shift
307292
308293 exist_check $2
309294 cd $2
@@ -323,9 +308,7 @@ case $1 in
323308 NUMBER_CHECK=" ^[0-9]+$"
324309
325310 # shift if it is
326- if [[ $2 =~ $NUMBER_CHECK ]]; then
327- shift
328- fi
311+ [[ $2 =~ $NUMBER_CHECK ]] && shift
329312
330313 exist_check $2
331314 cd $2
@@ -342,8 +325,9 @@ case $1 in
342325
343326 # requested the version?
344327 " version" | " v" )
345- echo " fpkg, version 3.0.0-beta4"
346- echo " Written by ruby R53 (https://github.com/ruby-R53) on July 2024"
328+ echo " fpkg, version 3.0.0"
329+ echo " Written by ruby R53 (https://github.com/ruby-R53),"
330+ echo " September 2024"
347331 ;;
348332
349333 # didn't even input a valid action or wants to know
@@ -353,26 +337,24 @@ case $1 in
353337 echo " Actions (and their short forms):"
354338 echo " (h)elp - shows this help message"
355339 echo " (u)pdate [pkg] - git pull every package listed in pkg.list, you"
356- echo " can optionally update only [pkg] as well "
357- echo " (i)nstall <pkg> - install <pkg> using \$ FPKG_DIR /ii/<pkg>.ii"
358- echo " (r)emove <pkg> - uninstall <pkg> using \$ FPKG_DIR /ii/<pkg>.ii also"
340+ echo " can also update [pkg] only "
341+ echo " (i)nstall <pkg> - install <pkg> using \$ FPKG_ROOT /ii/<pkg>.ii"
342+ echo " (r)emove <pkg> - uninstall <pkg> using \$ FPKG_ROOT /ii/<pkg>.ii also"
359343 echo " (l)ist - list registered packages"
360344 echo " (a)dd <pkg> - register <pkg>"
361345 echo " (p)eek <pkg> - take a peek at <pkg>'s .ii"
362346 echo " (g)oto <pkg> - go to <pkg>'s directory"
363347 echo " (e)dit <pkg> - edit <pkg>'s ii file"
364348 echo " (d)elete <pkg> - remove <pkg> from pkg.list"
365349 echo " (m)essage [-d] <pkg> - get comment from last commit of <pkg>. Takes an"
366- echo " optional -d for getting its diff"
350+ echo " optional -d switch to get its diff"
367351 echo " (H)istory [n] <pkg> - get <pkg>'s commit history. Takes an optional"
368- echo " [n] to get a custom depth. Defaults to 5"
352+ echo " number [n] to get a custom depth. Defaults to 5"
369353 echo " (v)ersion - get fpkg's version"
370354
371- # exit with error if input wasn't
355+ # exit with error code if input wasn't
372356 # for the help text
373- if [[ $1 != " help" ]]; then
374- quit 1
375- fi
357+ [[ $1 != " help" ]] && quit 1
376358 ;;
377359esac
378360
0 commit comments