magictomagic / backup

Backup my configutation
GNU General Public License v3.0
0 stars 0 forks source link

Add script monitor file change and auto generate/delete .desktop file #4

Closed magictomagic closed 2 years ago

magictomagic commented 2 years ago
#!/bin/bash

LOG_OPEN=1

XDG_DATA_HOME="$HOME/.local/share"
primitive_scripts_location="$HOME/.config/wofi/scripts"
desktop_scripts_location="$XDG_DATA_HOME/applications"

log(){
  [ $LOG_OPEN -eq 1 ] && echo "$(date '+%Y-%m-%d %H:%M:%S') $@"
}

generate_script(){
  name=$(expr match $1 '\(.*\)\.sh')
  cat << EOF > $desktop_scripts_location/$name.desktop
[Desktop Entry]
Type=Application
Name=${name}
Exec=/bin/bash $2/$1
EOF
}

delete_script(){
  rm $desktop_scripts_location/$(expr match $1 '\(.*\)\.sh').desktop
}

concurrency_times=0
inotifywait -m -e create -e delete -e moved_from -e moved_to $primitive_scripts_location \
  | while read -r NEWFILE
do
  concurrency_times=`expr $concurrency + 1`
  log "------ time: $concurrency_times started ------"
  log "$NEWFILE"

  created=$(echo ${NEWFILE} \
    | grep -E "^.* (CREATE|MOVED_TO) .*\.sh" \
    | awk '{print $3}')
  deleted=$(echo ${NEWFILE} \
    | grep -E "^.* (DELETE|MOVED_FROM) .*\.sh" \
    | awk '{print $3}')
  if [ ${#created} -ge 1 ]
  then
    log "created: $created"
    generate_script $created $primitive_scripts_location
  fi
  if [ ${#deleted} -ge 1 ]
  then
    log "deleted: $deleted"
    delete_script $deleted
  fi

  log "------ time: $concurrency_times ended ------"
done
magictomagic commented 2 years ago

bask up some sctipts


#!/bin/bash

#  if ! [[ "${generated_files[@]}" =~ $name.desktop ]]; then
#    echo "create file"
#    touch $desktop_scripts_location/$name.desktop
#  fi
#  echo $desktop_scripts_location/$name.desktop

XDG_DATA_HOME="$HOME/.local/share"

script="notify-send 'cccc'"

primitive_scripts_location="$HOME/.config/wofi/scripts"
desktop_scripts_location="$XDG_DATA_HOME/applications"

# change it after notify only update or create
files_to_generate=$(ls $primitive_scripts_location)
generated_files=$(ls $desktop_scripts_location)

# $1: Where to generate .desktop file
# $2: Where is the initial script directory
generate_scripts(){
  for file in $1
  do
    # script_location=$2/$file
    name=$(expr match "$file" '\(.*\)\.sh')
   cat << EOF > $desktop_scripts_location/$name.desktop
[Desktop Entry]
Type=Application
Name=${name}
Exec=/bin/bash $2/$file
EOF
  done
}

generate_scripts "${files_to_generate[*]}" $primitive_scripts_location
magictomagic commented 2 years ago

add in sway config:

exec . $HOME/.config/wofi/generate.sh >> /tmp/generate_${UID}.log