######################################################
### bgs.py
Library of some basic python functions

##### debuglev(numbertocheck,debuglevel=0)
Returns true if the variable passed as debuglevel is greater than or equal to numbertocheck.

Example:
if bgs.debuglev(3,debuglevel): eprint('This is visible on debuglevel >= 3.')

##### readlinkf(inpath)
Returns the output of a 'readlink -f'

Example:
readlinkf('/usr/bin/python')

##### eprint
Like print, but sends to standard error.

##### caller_args
Returns the values passed to the current function.

Example:
if bgs.debuglev(10,debug): bgs.eprint(bgs.caller_args())

######################################################
### dli.py
DLI stands for "dnf list installed." It provides a quick command which caches the output of "dnf list installed" for the day, so you can search the installed packages.

Dli saves the output to a file of pattern ~/.dli/dnf.installed.YYYY-MM-DD.log.

usage: dli <-i|-a> [-dV] [packagename]
 -i installed  List installed packages.
 -a available  List available packages.
 -r refresh    Force a refresh of an existing file for today.
 -d debug      Set debug level to a number between 0-10.
 -V version
 packagename   Regex to search in the output. Appends .* and prepends .*

Examples:
List all packages that are installed, and load them into vi:
dli
List available packages that match a regular expression:
dli -a 'audacity'
Force refresh of installed packages:
dli -r

######################################################
### modconf.py
Performs actions to ini-like files. This pretty much calls a function from the uvlib library, described below.

Possible actions to a variable in a file include add,set,remove,gone,empty.

Examples:
modconf /etc/ssh/sshd_config -v add AllowUsers bgstack15
modconf /etc/ssh/sshd_config -v gone AllowGroups uselessbutrequiredvalue
modconf /etc/ssh/sshd_config -v set UsePam yes

######################################################
### pwhashgen.py
Pwhashgen accepts a single parameter, or will prompt you, and generates a sha-512 hash suitable for /etc/shadow.

Example:
$ ./pwhashgen.py '123456'
$6$lN7SmaBJyR9icKt.$TkWJrzxzQb7szjPSMNtu1K16bcXMH9VwmWZoryhD4QDzs7HgEULRpBhdYi5wuBUdbBBznnA3l0/J0Ck3zu5GJ1

######################################################
### scrub.py
Scrub does a copy of a whole directory structure, and replaces words with substitutes, defined by the scrub.txt file.
This was written to provide a method of obfuscation to protect private data while preparing git repositories for publication.
Very few of my personal projects use scrub anymore.

It can be called by the 'build/pack scrub' command included in many of my packages.

Scrub.py depends on a scrub.txt, nominally in /home/bgstack15/rpmbuild/SOURCES/packagename/build/scrub.txt

    # for scrubpyversion 2016-11-16a or newer
    source /home/bgstack15/rpmbuild/SOURCES/bgscripts-1.2-2
    target /home/work/bgscripts.clean
    ignore "tgz png gif jpg pyc pyo git swp ico"
    # rest of the lines are "OLD WORD" "NEW WORD"
    BSMITH  BGSTACK15
    Bsmith  Bgstack15
    bsmith  bgstack15
    BRIAN   BNAME
    Brian   Bname
    brian   bname
    SMITH   STACK15
    Smith   Stack15
    smith   stack15

Scrub.py uses python libraries to read in files and perform regular expressions. Only plain text files are supported, which is why there is an option titled "ignore" which is a space separated string of the extensions to ignore in the directory tree.

Example:
cd /etc/ansible
scrub
cd /home/work/ansible.clean
git add .
git commit -m 'new version of our ansible environment'
git push

######################################################
### switchpyver.sh
This is a shell script, that adjust the symlinks of the version-dependent python libs in the specified directory. If no directory is passed as an argument, it will use the directory where the script is located, i.e., /usr/share/bgscripts/py.

If you change which python version is called via /usr/bin/python, rerun this script. Installing the rpm or deb package of bgscripts will run this for you upon installation.

######################################################
### updateval.py
Updateval is my attempt to provide a useful regular expression replacement utility like crudini.
The main body has been rewritten several times. The original was in Bourne shell, and the python version was rewritten once so far as of July 2017.

usage: updateval.py [-dV] [-vab] <infile> <searchstring> <deststring> [-s <stanza>] [--stanzaregex <stanzaregex>]
 -d debug     Set debug level, from 0-10.
 -v verbose   Display the output.
 -a apply     Modify original file.
 infile       File to read
 searchstring Regex string to search
 deststring   The value to have in the file. Will be added if <searchstring> is not found.
 -V version
 -s stanza    Define stanza to match. Will do rudimentary detection for stanza definitions. I.e., -s "[bgscripts]" will recognize all lines with square braces as new stanza definitions, and the specific one to search for the <searchstring> will be the stanza [bgscripts].
              Also recognizes stanza() definition.
              If --stanzaregex is provided, then <stanza> is the specific stanza to match.
 --stanzaregex Define a custom regular expression that defines the stanza headings.

It is best to try to match the whole line. If the <searchstring> is found, it will do a regular expression replacement and place the <deststring>. Experimentation with the --verbose flag is recommended until the results are predictable.

It is possible to provide -s '[]' and then it will recognize square braces as new stanza definitions, and will search for <searchstring> only in the beginning section of the file, before any defined stanzas.

Examples:
Update file /etc/rc.conf to set ntpd_enable=YES, and add the value if it does not exist.
updateval.py /etc/rc.conf "^ntpd_enable=.*" 'ntpd_enable="YES"' --apply

In stanza '[section]', set value example=bar. If example is not found, insert example=bar at the beginning of the stanza.
updateval.py -b -s '[section]' '^example=.*' 'example=bar'

######################################################
### uvlib.py
Both python2 and python3 versions are provided. A symlink, uvlib.py should point to whichever version is the default on your system, defined by $( which python ) --version.
Uvlib is a library for updateval and mdconf. Inside are just a few large functions that perform the heavy lifting for those wrapper scripts.

##### updateval
def updateval(infile,regex,result,verbose=False,apply=False,debug=0,stanza="",stanzaregex="",atbeginning=False,addline="MyUnMatchedSTR1NG"):

This is the main part that performs the regular expression replacements, within requested stanza if any.See file updateval.py for a specific function invocation.

##### manipulatevalue
def manipulatevalue(infile,variable,item,action,itemdelim=",",variabledelim="=",verbose=False,apply=False,comment='#',debug=0,stanza="_NONE_X",stanzaregex="_NONE_X",beginning=False,beginningline=False):

This is the main part that performs the custom ini manipulation like the popular crudini utility. This function accepts different delimiters of the variable from the values, and also between the values themselves.

See modconf.py for a specific invocation.

######################################################
### getpwhash
This utility is very simple. It accepts a parameter or prompts the user interactively for a password. The output is the encrypted and salted hash that is suitable for the shadow file.

Examples:
getpwhash 'MYsup3r5ecre+Pw'
