Skip to content

Commit

Permalink
minor update and few new scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
yaswant committed Feb 21, 2020
1 parent cfd23e0 commit 9012707
Show file tree
Hide file tree
Showing 11 changed files with 332 additions and 81 deletions.
2 changes: 1 addition & 1 deletion benchmark.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ cat<<EOF
Benchmark runner
Note: Requires sudo to clear filesystem memory cache
Usage: ${0##*/} [OPTION] COMMAND
Usage: ${0##*/} [OPTION] COMMAND|SCRIPT
Options:
Expand Down
162 changes: 162 additions & 0 deletions desktop-app-generator
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
#!/bin/bash
#
# History:
# 2019.05 beta release.
# 2019.10 allow user to type in custom commands.
# update application categories.
#
vsn=2019.10

# ----------------------------------------------------------------------------
OPTS=$(getopt -o hv --long help,version -n "${0##*/}" -- "$@") \
|| { echo "Terminating..." >&2; exit 1; }
eval set -- "$OPTS"

version(){
cat<<EOF
${0##*/} $vsn
Copyright (C) 2019 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
Written by Yaswant Pradhan.
EOF
}

usage(){
cat<<EOF
Generate GNOME desktop application launcher.
Usage: ${0##*/} [OPTION]
Options:
-h, --help print this help and exit
-v, --version print version and exit
GUI Options:
See https://developer.gnome.org/desktop-entry-spec for detailed desktop entry specs.
Filename Name of the desktop launcher filename. default: newapp.desktop
Type Type of desktop entry: Application (type 1)
Name Specific name of the application, for example "MyFunkyApp"
Version Version of the Desktop Entry Specification that the desktop entry conforms with.
Entries that confirm with this version of the specification should use 1.0.
Comment Tooltip for the entry, for example "Does Funky things.."
Command Program to execute, possibly with arguments
Optional Args %f - A single file name, even if multiple files are selected.
The system reading the desktop entry should recognize that the program
in question cannot handle multiple file arguments, and it should should
probably spawn and execute multiple copies of a program for each selected
file if the program is not able to handle additional file arguments.
If files are not on the local file system (i.e. are on HTTP or FTP
locations), the files will be copied to the local file system and
%f will be expanded to point at the temporary file. Used for programs
that do not understand the URL syntax.
%F - A list of files. Use for apps that can open several local files at once.
Each file is passed as a separate argument to the executable program.
%u - A single URL. Local files may either be passed as file: URLs or as file path.
%U - A list of URLs. Each URL is passed as a separate argument to the executable
program. Local files may either be passed as file: URLs or as file path.
%c - The translated name of the application as listed in the appropriate Name key
in the desktop entry.
%k - The location of the desktop file as either a URI (if for example gotten from
the vfolder system) or a local filename or empty if no location is known.
Icon Icon to display in file manager, menus, etc. If the name is an absolute path,
the given file will be used
Caregory Categories in which the entry should be shown in a menu (for possible values
see http://www.freedesktop.org/Standards/menu-spec)
In terminal If selected, the program will run in a terminal window
Install for user If selected, the application will be installed in user
~/.local/share/applications directory so that it is discoverable from GNOME menu
Report bugs to <yaswant.pradhan@metoffice.gov.uk>
EOF
}

while true; do
case "$1" in
-h |--help ) usage; exit 0 ;;
-v |--version ) version; exit 0 ;;
-- ) shift; break ;;
* ) break ;;
esac
done
# ----------------------------------------------------------------------------


command -v yad 1>/dev/null || { echo "yad: command not found" && exit 127; }

# \nSee https://developer.gnome.org/desktop-entry-spec/" --show-uri
# --text="<i>Edit fields below to create and install a custom desktop launcher</i>\nhttps://developer.gnome.org/desktop-entry-spec" \

# collect form data
data=$(yad --title="Desktop Entry Generator $vsn" --width=450 --posx=10 --posy=10 \
--text-align=center --show-uri\
--form --align=right \
--field=" Filename" "newapp.desktop" \
--field=" Type:RO" "Application" \
--field=" Name" "LauncherName" \
--field=" Version" "1.0" \
--field=" Comment" "Generate and install desktop application launcher" \
--field=" Command" "$HOME/bin/" \
--field=" Optional Args:CB" "!%f!%F!%u!%U!%c!%k" \
--field=" Icon:FL" "/usr/share/icons/hicolor/48x48/apps/system-logo-icon.png" \
--field=" Categories:CBE" "Utility!AudioVideo!Development!Education!Game!Graphics!Network!Office!Science!Settings!System" \
--field=" In terminal:CHK" FALSE \
--field=" Install for user: ${USER}:CHK" FALSE)
ret=$?
[[ "$ret" -eq 1 || "$ret" -eq 252 ]] && { echo "Cancelled"; exit $ret; }


# retrieve fields array
IFS='|' read -ra fields <<< "$data"


# validate command
exe="${fields[5]%% *}"
if command -v "$exe" &>/dev/null; then :
else
yad --image dialog-error --title 'Error' \
--text "<b> ${exe}: command not found</b>\n\nPlease try again and input a valid Command" \
--width=450 --posx=10 --posy=10 \
--button=gtk-cancel:127
exit $?
fi


# parse application file path
if [[ ${fields[10]} = TRUE ]]; then
filepath="${HOME}/.local/share/applications/${fields[0]}"
mkdir -p "${filepath%/*}"
else
filepath="${fields[0]}"
fi

# prompt overwrite
overwrite=0
if [ -f "$filepath" ]; then
yad --image dialog-question \
--title Overwite --width=450 --posx=10 --posy=10 \
--button=gtk-no:1 \
--button=gtk-yes:0 \
--text "<b> ${filepath##*/} exists</b>\n\n Overwrite file?"
overwrite=$?
fi

# write desktop entry file
(( overwrite == 0 )) && \
cat > "$filepath" <<EOF
[Desktop Entry]
Type=${fields[1]}
Name=${fields[2]}
Version=${fields[3]}
Comment=${fields[4]}
Exec=${fields[5]} ${fields[6]}
Icon=${fields[7]}
Terminal=${fields[9],,}
Categories=${fields[8]}
EOF

# make the app executable
[ -f "$filepath" ] && chmod +x "$filepath"
27 changes: 17 additions & 10 deletions doy
Original file line number Diff line number Diff line change
Expand Up @@ -48,18 +48,25 @@ Examples:
EOF
}

while getopts "h:v" OPTION
do
case $OPTION in
h) usage;
exit 1 ;;
v) echo -e "\n$0 version $version\n"
exit ;;
?) usage;
exit ;;
esac
#------------------------------------------------------------------------------
# Parse arguments
#------------------------------------------------------------------------------
while [[ $1 == -* ]]; do
case $1 in
-v|--version)
echo -e "\n${0##*/}: v-$version\n"
shift 1 ;;

-h|--help|-?|*)
usage
exit ;;
esac
done


#------------------------------------------------------------------------------
# Parse input date and return doy
#------------------------------------------------------------------------------
case $# in
0) # Current system doy
date +'%j'; exit $?;;
Expand Down
10 changes: 5 additions & 5 deletions files
Original file line number Diff line number Diff line change
Expand Up @@ -145,14 +145,14 @@ if [[ $# -le 1 ]]; then
# echo "find $LOPT $SPATH $OPT -exec du --apparent -sh {} \;"
find $LOPT "$SPATH" $OPT -exec du --apparent -sh {} + \
| sort -h \
| tee $LOG
| tee "$LOG"
else
find $LOPT "$@" $OPT -exec du --apparent -sh {} + \
| sort -h \
| tee $LOG
| tee "$LOG"
fi

nf=$(wc -l < $LOG)
nf=$(wc -l < "$LOG")
[[ $nf -gt 1 ]] && s='s'
[[ $nf -eq 0 ]] && echo -e "No match.\n" && rm $LOG && exit 0

Expand All @@ -168,11 +168,11 @@ echo -e "\nTotal\t$nf File$s"
if [ -n "$DEL" ]; then
# echo -e ">> Delete these file$s? (y|n): \c"; read -r x
read -rp ">> Delete these file$s? (y|n): " x
[[ $x = 'y' ]] && rm $(cut -f 2 $LOG) && echo -e " File$s deleted." #m1
[[ $x = 'y' ]] && rm $(cut -f 2 "$LOG") && echo -e " File$s deleted." #m1
# [[ $x = 'y' ]] && rm $(cat $LOG |tr '\t' ',' |cut -d',' -f2) #m2
# [[ $x = 'y' ]] && find $LOPT "$SPATH" $OPT $DEL #m3
fi


# Clean-up (Remove log file) --------------------------------------------------
[ -f $LOG ] && [ -z "$KEEP" ] && rm $LOG
[ -z "$KEEP" ] && rm -f "$LOG"
71 changes: 41 additions & 30 deletions restore
Original file line number Diff line number Diff line change
@@ -1,49 +1,60 @@
#!/bin/bash
# syntax:
# restore filepath [snapshot]
# restore [file|dirname] [snapshot]
#
# restore files/dirs from snapshots (Limited to data in $HOME only)
# restore files/directories from snapshots (limited to data in $HOME only)
# yaswant.pradhan 2014/15
# -----------------------------------------------------------------------------
rootdir=$(readlink -f $HOME)
SNAPSHOTDIR=$rootdir/.snapshot
rspoint=${2:-'hourly.0'}
rootdir=$(readlink -f "$HOME")
SNAPSHOTDIR=${rootdir}/.snapshot
LAST="$(/bin/ls -t1 "$SNAPSHOTDIR" | head -n 1)" # most recent restore point
recent=${2:-$LAST} # parse recent rsp argument
snapshots=($(/bin/ls -t1 "$SNAPSHOTDIR")) # all available snapshots

function line(){ for i in {1..80}; do echo -e '-\c'; done; echo ""; }
function usage()
{
list_all(){
more << EOF
$(basename $0) file[dir]name [snapshot]
---------------------
Available snapshots
---------------------
$(ls -lt $SNAPSHOTDIR |tail -n+2 |\
awk '{printf"%10s @%6s on %s-%s\n", $9,$8,$7,$6}')
---------------------------------------------
Available snapshots
---------------------------------------------
$(/bin/ls -lt "$SNAPSHOTDIR" | tail -n+2 | nl | \
awk '{printf"%2s %-22s @%6s on %s-%s\n", $1, $10,$9,$8,$7}')
---------------------------------------------
EOF
}
usage(){ echo "usage: ${0##*/} file|dirname [snapshot]"; list_all; }
# -----------------------------------------------------------------------------

[[ "$#" -lt 1 ]] && usage && exit

# Parse file path
fpath=$(readlink -f $1) # Full paths of File/Directory to be restored

# -- parse file path
fpath=$(readlink -f "$1") # Full paths of File/Directory to be restored
rpath=${fpath#$rootdir/} # Path relative to $HOME

if [ ! -e "$SNAPSHOTDIR/$rspoint" ]; then
echo "** $SNAPSHOTDIR/$rspoint doesnot not exist **"
echo "Select a snapshot directory to restore:"
line; ls -t $SNAPSHOTDIR && line || exit $?
read rspoint
if [ -e "${SNAPSHOTDIR}/${recent}" ]; then
list_all;
snapfile="${SNAPSHOTDIR}/${recent}/${rpath}"
else
echo -e "** ERROR: ${SNAPSHOTDIR}/${recent} doesn't not exist **\n"
list_all;

read -rp "Please select a snapshot 'id' to restore ('0' to exit): " _id
(( _id == 0 )) && { echo "Aborted."; exit; }
(( _id-- ))
snapfile="${SNAPSHOTDIR}/${snapshots[$_id]}/${rpath}"
fi
snapfile="$SNAPSHOTDIR/$rspoint/$rpath"


if [ -e "$snapfile" ]; then echo "Restoring from $rspoint"
else
echo "$rpath not found in $rspoint"
exit 1
fi
# -- check existence of file in snapshot
[ -e "$snapfile" ] || { echo "$snapfile: No such file or directory"; exit 2; }


# -- begin restore
echo "Confirm restore from: $snapfile"
# echo "[command] rsync -aiLt --delete $snapfile $(dirname $fpath)"
read -rp "** Once restored, this cannot be undone. Continue y|(n)? " ans

echo "[command] rsync -avi --delete $snapfile $(dirname $fpath)"
read -ep "WARN! Once restored, this cannot be reverted - Do you want to continue? y|[n]: " yn
[[ "$yn" == 'y' ]] && rsync -avi --delete $snapfile $(dirname $fpath)
[[ $(tr '[:lower:]' '[:upper:]' <<<"$ans") == 'Y' ]] \
&& rsync -ait --delete "$snapfile" "${fpath%/*}" \
|| echo "Aborted."
Loading

0 comments on commit 9012707

Please sign in to comment.