update_scripts.sh 2.31 KB
Newer Older
Michael  's avatar
updates  
Michael committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#!/bin/sh
# Script to check for new versions of Michaels Scripts
GITURL="https://gitlab.com/_alphalpha_/dotfiles.git"
GITSUBDIR=".local/Scripts"
LOCALDIR="michaels-scripts-git"
TMPDIR="$HOME/.local/tmp"

# prep
[ -d "$TMPDIR" ] || mkdir -p "$TMPDIR"
[ -d "$TMPDIR/$LOCALDIR" ] && rm -rdf "$TMPDIR/$LOCALDIR"
CURRENTDIR="$(pwd)"


# git clone only .local/Scripts
cd "$TMPDIR"
git init "$LOCALDIR"
cd "$LOCALDIR"
git remote add origin "$GITURL"
git config core.sparsecheckout true
echo "$GITSUBDIR/*" >> .git/info/sparse-checkout
git pull --depth=1 origin master
mv "$GITSUBDIR"/* .
if [ "$(echo "$GITSUBDIR" | grep '/')" ]; then
  GITSUBDIR="$(echo "$GITSUBDIR" | cut -d '/' -f1)"
fi
rm -rdf "$GITSUBDIR" .git


# make a list of every script that has been updated
LIST=""
for f in $(ls -1A); do
  if [ -f "$HOME/.local/Scripts/$f" ]; then
    if [ "$(diff "$f" "$HOME/.local/Scripts/$f")" ]; then
      printf "found new version of %s\\n" "$f"
      LIST="$LIST $f"
    fi
Michael  's avatar
Michael committed
37
  elif [ -f "$TMPDIR/$f" ]; then
Michael  's avatar
updates  
Michael committed
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
    printf "found new script: %s\\n" "$f"
    LIST="$LIST $f"
  fi
done


# throw together a menu
#LIST="$(echo "$LIST" | xargs -n1 | sed 's/$/<s>/g')"
while [ "$(echo -n "$LIST" | wc -w)" -gt 0 ]; do
  [ -z "$OPT" ] && OPT="$(iselect -p4 \
  "Found new versions for Michaels scripts." \
  "Select individual script for more options:" "" \
  "$(echo "$LIST" | xargs -n1 | sed 's/$/<s>/g')" \
  "" "> Install everything<s:INSTALL_ALL>" "< Exit<s:EXIT>")"
  if [ "$OPT" = "INSTALL_ALL" ]; then
  for SCRIPT in $LIST; do
    cp -v "$TMPDIR/$LOCALDIR/$SCRIPT" "$HOME/.local/Scripts/"; 
  done
    exit 0
  elif [ "$OPT" = "EXIT" ]; then
    exit 0
  fi
  ACTION=""
  while [ "$OPT" ] ; do
    ACTION="$(iselect -p3 \
      "$OPT" "" \
      "view diff<s:DIFF>" \
      "open in editor<s:OPEN>" \
      "install<s:INSTALL>" \
      "skip<s:SKIP>")"
  case "$ACTION" in
Michael  's avatar
Michael committed
69
70
    DIFF)	diff "$TMPDIR/$LOCALDIR/$OPT" "$HOME/.local/Scripts/$OPT" | less;;
    OPEN)	featherpad "$TMPDIR/$LOCALDIR/$OPT";;
Michael  's avatar
updates  
Michael committed
71
    INSTALL) cp -v "$TMPDIR/$LOCALDIR/$OPT" "$HOME/.local/Scripts/$OPT";
Michael  's avatar
bugfix    
Michael committed
72
             LIST="$(echo "$LIST" | sed "s:$OPT::")"; OPT="";;
Michael  's avatar
Michael committed
73
    SKIP)	LIST="$(echo "$LIST" | sed "s:$OPT::")"; OPT="";;
Michael  's avatar
Michael committed
74
    *) ACTION="-";OPT="";;
Michael  's avatar
updates  
Michael committed
75
76
77
78
79
80
81
82
83
84
  esac
  done
done


# cleanup
cd "$CURRENTDIR"
rm -rdf "$TMPDIR/$LOCALDIR/"
[ ! -z "$(find "$TMPDIR" -maxdepth 0 -empty)" ] && rm -rdf "$TMPDIR"
exit 0