33FPKGDIR=" /usr/local/fpkg/" # where all packages are located at
44EDIOR=" vim" # change this to your favorite editor
55
6- VERSION=" 1.0.0" # not sure why i want a variable for that honestly
6+ VERSION=" 2.0.0" # not sure why i want a variable for that honestly
7+
8+ quit () {
9+ popd > /dev/null # restore user's working directory
10+ stty echo # unhide their keystrokes
11+ exit $1 # exit with a given code
12+ }
713
814error () {
9- popd > /dev/null
10- stty echo
11- exit 1
15+ echo " ERROR: $1 "
16+ quit 1
17+ }
18+
19+ exist_check () {
20+ if [[ -z $1 ]]; then
21+ error " package name was not provided!"
22+ fi
23+
24+ grep -w $1 pkg.list > /dev/null 2>&1
25+ if [[ $? > 0 ]]; then
26+ error " $1 not found in pkg.list! Was it a typo?"
27+ fi
1228}
1329
1430write_check () {
@@ -21,27 +37,28 @@ write_check () {
2137 ret=$(( ret + $? ))
2238
2339 if [[ $ret > 0 ]]; then # if any of the touch's fail
24- echo " ERROR: you cannot write to $FPKGDIR ! Please run as root"
25- error
40+ error " you cannot write to $FPKGDIR ! Please run as root!"
2641 fi
2742}
2843
29- # hide keystrokes, save current directory to go to $FPKGDIR
30- stty -echo
31- pushd . > /dev/null
32- cd $FPKGDIR
33-
34- if [[ $? != 0 ]]; then
44+ # check if $FPKGDIR exists in the first place
45+ if [[ ! -d $FPKGDIR ]]; then
46+ stty echo
3547 echo " ERROR: $FPKGDIR does not exist! Create it now? [Y/n]"
3648 read choice
3749
38- if [[ choice != " n" ]]; then # so that the default is "yes"
39- mkdir -v $FPKGDIR
50+ if [[ $ choice != " n" ]]; then # so that the default is "yes"
51+ mkdir -pv $FPKGDIR /ii # create the ii one too already
4052 fi
4153
42- error
54+ quit 1
4355fi
4456
57+ # hide keystrokes, save current directory to go to $FPKGDIR
58+ stty -echo
59+ pushd . > /dev/null
60+ cd $FPKGDIR
61+
4562case $1 in
4663 # refresh every package using git pull
4764 " update" | " u" )
@@ -52,99 +69,100 @@ case $1 in
5269 cd $pkg_dir > /dev/null 2>&1
5370
5471 if [[ $? != 0 ]]; then
55- echo " ERROR: $pkg_dir does not exist!"
56- echo " Remove its entry with 'fpkg delete' and try again. Aborting..."
57- error
72+ error " $pkg_dir does not exist! Remove its entry
73+ with 'fpkg delete' and try again. Aborting..."
5874 fi
5975
6076 echo " $pkg_dir :"
61- git pull
77+ git pull --recurse-submodules # where the update itself happens
6278 cd - > /dev/null
6379 done
6480 ;;
6581
6682 # requested the installation of a package?
6783 " install" | " i" )
68- if [[ -z $2 ]]; then
69- echo " ERROR: package name was not provided!"
70- error
71- fi
72-
73- cd ii/
74-
75- if [[ $? != 0 ]]; then
76- echo " ERROR: $FPKGDIR /ii/ does not exist! Create it now? [Y/n]"
77- read choice
78-
79- if [[ choice != " n" ]]; then
80- mkdir -v ii/
81- fi
82-
83- error
84- fi
84+ exist_check $2
8585
86- ./$2 .ii
86+ ./ii/ $2 .ii
8787 ;;
8888
8989 " list" | " l" )
9090 cat pkg.list
91- echo " "
92- echo " Total: $( cat pkg.list | wc -l) " # get the number of installed packages as well
91+ echo -e \\ n" Total: $( cat pkg.list | wc -l) " # see how many packages we have too
9392 ;;
9493
9594 # adding an entry to the list?
9695 " add" | " a" )
9796 write_check
9897
98+ exist_check $2
99+
100+ stty echo
101+ echo $2 >> pkg.list
102+ echo -e " # Writing installation instructions for package $2 " \\ n > ii/$pkg_name .ii
103+ $EDITOR ii/$2 .ii
104+ chmod +x ii/$2 .ii
105+ ;;
106+
107+ " peek" | " p" )
108+ exist_check $2
109+
110+ cat ii/$2 .ii
111+ ;;
112+
113+ " goto" | " g" )
99114 stty echo
100- echo " Package name:"
101- read pkg_name
102- echo $pkg_name >> pkg.list
103- echo " # Writing installation instructions for package, you can ignore this line" > ii/$pkg_name .ii
104- echo " cd ../<package_dir_here>/" > ii/$pkg_name .ii
105- $EDITOR ii/$pkg_name .ii
106- chmod +x ii/$pkg_name .ii
115+
116+ exist_check $2
117+
118+ export FPKGDIR=$FPKGDIR
119+ export PKG_DIR=$2
120+ sh -c ' cd $FPKGDIR/$PKG_DIR;
121+ echo "Working on $PKG_DIR/, ^D to exit";
122+ exec "${SHELL:-sh}"'
123+ ;;
124+
125+ # editing a package's .ii?
126+ " edit" | " e" )
127+ write_check
128+
129+ exist_check $2
130+
131+ stty echo
132+
133+ $EDITOR ii/$2 .ii
107134 ;;
108135
109136 # delete an entry from the list?
110137 " delete" | " d" )
111138 write_check
112139
140+ exist_check $2
141+
113142 stty echo
114- echo " Removing:"
115- read pkg_name
116- entry=$( grep -w " ^$pkg_name " pkg.list -n | awk -F: ' { print $1 }' ) # FIXME: is there a better way of doing this?
143+ entry=$( grep -w " ^$2 " pkg.list -n | awk -F: ' { print $1 }' ) # get index of the package
117144 sed -i " $entry " d pkg.list
118- rm ii/$pkg_name .ii
119- echo " $pkg_name removed, would you like to remove its files as well? [y/N]"
120- read file_removal
145+ rm ii/$2 .ii
146+ echo " $2 removed from pkg.list , would you like to remove its files as well? [y/N]"
147+ read choice
121148
122- if [[ $file_removal = ' y' ]]; then # same thing but with "no"
123- echo " Package directory (relative to /usr/local/fpkg/ ):"
149+ if [[ $choice = ' y' ]]; then # same thing but with "no"
150+ echo " Package directory (relative to $FPKGDIR ):"
124151 read dir
125152 rm -rf $dir
126153 fi
127154 ;;
128155
129156 # requested the message of last git commit?
130157 " message" | " m" )
131- if [[ -z $2 ]]; then
132- echo " ERROR: package name was not provided!"
133- error
134- fi
135-
136158 if [[ $2 = " -d" ]]; then
137- cd $3
138- else
139- cd $2
159+ shift
140160 fi
141161
142- if [[ $? != 0 ]]; then
143- echo " Package does not exist! Was it a typo?"
144- error
145- fi
162+ exist_check $2
163+ cd $2
146164
147- if [[ $2 = " -d" ]]; then
165+ if [[ $1 = " -d" ]]; then
148166 git show
149167 else
150168 echo $( git log -1 --pretty=%B)
@@ -153,32 +171,33 @@ case $1 in
153171
154172 # requested the version?
155173 " version" | " v" )
156- echo " fpkg version $VERSION "
174+ echo " fpkg - version $VERSION "
157175 echo " 2024 ruby R53 (https://github.com/ruby-R53)"
158176 ;;
159177
160178 # didn't even input a valid action or wants to know
161179 # them?
162- " help" | * )
163- echo " Usage: fpkg [ action] [package]"
180+ " help" | " h " | * )
181+ echo " Usage: $0 < action> [package]"
164182 echo " Actions:"
165183 echo " help | h - shows this help message"
166184 echo " update | u - git pull every package listed in pkg.list"
167- echo " install | i <pkg> - install <pkg> using instruction file "
185+ echo " install | i <pkg> - install <pkg> using $FPKGDIR /ii/<pkg>.ii "
168186 echo " list | l - list registered packages"
169- echo " add | a - register a package"
170- echo " delete | d - remove a package"
171- echo " message | m [-d] <pkg> - get comment from last git commit of <pkg>. Takes an"
172- echo " optional -d for getting the diff"
187+ echo " add | a <pkg> - register <pkg>"
188+ echo " peek | p <pkg> - take a peek at <pkg>'s .ii"
189+ echo " goto | g <pkg> - go to <pkg>'s directory"
190+ echo " edit | e <pkg> - edit <pkg>'s ii file"
191+ echo " delete | d <pkg> - remove <pkg>"
192+ echo " message | m [-d] <pkg> - get comment from last commit of <pkg>. Takes an"
193+ echo " optional -d for getting its diff"
173194 echo " version | v - get fpkg's version"
174195
175196 if [[ $1 != " help" ]]; then
176- error
197+ quit 1
177198 fi
178199 ;;
179200esac
180201
181- # restore the directory, and unhide
182- # keystrokes
183- popd > /dev/null
184- stty echo
202+ # and the program is done!
203+ quit 0
0 commit comments