Advertisement
s243a

trim_puppy_retry.sh (2)

Mar 28th, 2019
275
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 9.82 KB | None | 0 0
  1. curdir=`pwd`
  2. s_rootfs="rootfs"
  3. alt_s_rootfs="/mnt/+mnt++root+Downloads+xslacko-slim-4.4r25.iso+puppy_xslacko_4.4.sfs"
  4. prefix=""
  5. action="mv"
  6. retry=1 #Default action for an array
  7. chroot_remove_builtin=0
  8.  
  9. #After we remove packages we might install some replacement versions
  10. #When distro not specified in path look for pkg dirs in dir containing distro name in this order
  11. post_rm_pkg_dirs=( "pupngo" ) #Replaceing bash with pupngo's version can save a fair bit of space
  12. #When arch not specified in path look for pkg arch dir containing distro name in this order
  13. arches=( "i386" "no-arch" "i486" "i586" "i686" )
  14.  
  15.  
  16. #Applications
  17. #Let's remove these applications and maybe put them into an sfs
  18. ALL=()
  19. ALL_ArrNames=()
  20.  
  21. #Printing, scaners and pdfs related stuff
  22. A_PrintScanPDF+=( "libgs9" )
  23. ALL+=( "${A_PrintScanPDF[@]}" )
  24. ALL_ArrNames+=( "A_PrintScanPDF" )
  25.  
  26. #I'm not sure what this stuff is fore
  27. A_Unknown=( "bbc_provided" )
  28. ALL+=( "${A_A_Unknown[@]}" )
  29. ALL_ArrNames+=( "A_A_Unknown" )
  30.  
  31. #Misc. Command line tools
  32. A_Commandline=( "testdisk" )
  33. ALL+=( "${A_Commandline[@]}" )
  34. ALL_ArrNames+=( "A_Commandline" )
  35.  
  36. Post_Remove_Install_Fm_Dir=( \
  37. "i386/pupngo/bash-4.2" \
  38. )
  39.  
  40. #source pkgs_arry
  41.  
  42. #Coppies a more recent version of remove_built_in
  43. #and also a list of icu related files (to be removed) which aren't associated
  44. #with any packages
  45. pre_remove_patch="pre_remove_patch"
  46. cp --remove-destination -arf $pre_remove_patch/* \
  47.                                  $s_rootfs/ 2>/dev/null
  48.  
  49. delete_petspec_fm_file(){
  50.  
  51.   local field_val=$1
  52.   local pkg_rootfs=${2:-"$s_rootfs"}
  53.   local spec_list_name=${3:-"woof-installed-packages"}
  54.   local field=${4:-1} #The first field is the default  
  55.   #TODO if prk_rootfs="/" or "" give warning
  56.   AWK_PRG="BEGIN{FS=\"|\"}
  57.  {
  58.    if ( \$$field != \"$field_val\" ) {
  59.     print
  60.    }
  61.  }"
  62.   #TODO we probably need logic to expand wildards
  63.   local file_spec_list=( "$pkg_rootfs/root/.packages/$spec_list_name" )
  64.   for spec_list in "${file_spec_list[@]}"; do
  65.     cat "$spec_list" | awk "$AWK_PRG"  >> "/tmp/$spec_list"
  66.     rm "$spec_list"
  67.     mv  "/tmp/$spec_list" "$spec_list"
  68.   done
  69. }
  70.  
  71. inst_builtin_fm_dir(){
  72.   local source_dir=$1
  73.   local dest_root=${2:-s_rootfs}
  74.  
  75.   local pkg_name=${3:-`basename $1`} #TODO add logic for different location of files.list
  76.  
  77.   source_file_list_prefixed="$source_dir/root/.packages/builtin_files/$pkg_name"
  78.   dest_file_list_prefixed="$dest_root/root/.packages/builtin_files/$pkg_name"
  79.   local spec_list_name=${4:-"woof-installed-packages"}
  80.   local file_spec_list_path="$dest_root/root/.packages/$spec_list_name"
  81.   cp --remove-destination -arf $source_dir/* \
  82.                                  $dest_root/ 2>/dev/null
  83.   if [ ! -f "$source_file_list_prefixed" ]; then
  84.      cd source_dir
  85.      find . -mindepth 1 -name '*' > "$dest_file_list_prefixed"
  86.   fi
  87.   if [ ! -f "$source_dir/pet.specs" ]; then
  88.      cd "source_dir"
  89.      
  90.      
  91.      dir2pet "$pkg_name"
  92.   fi  
  93.  
  94.   #match_cnt=$( grep -cF "$spec" "$inst_pkg_specs_target" )
  95.   #if [ $match_cnt -eq 0 ]; then
  96.   #  spec >> "$inst_pkg_specs_target"
  97.   #fi
  98.   if [ -f "source_dir/pet.specs" ]; then
  99.     #All args here except the first are default
  100.     delete_petspec_fm_file "$pkg_name" "$dest_root" "$spec_list_name" "1"
  101.     cat "$source_dir/pet.specs" >> "$file_spec_list_path"
  102.   fi
  103. }
  104.  
  105. curdir=`pwd`
  106. rootfs="$s_rootfs"
  107. xinteractive=1S
  108. prefix=""
  109. unmount_vfs(){
  110.  umount -l $curdir/$rootfs/dev 2>/dev/null
  111.  umount -l $curdir/$rootfs/sys 2>/dev/null
  112.  umount -l $curdir/$rootfs/proc 2>/dev/null
  113.  #umount /mnt/wktaz 2>/dev/null
  114.  #umount /mnt/wksfs 2>/dev/null
  115. }  
  116.  
  117. trap unmount_vfs EXIT
  118. trap unmount_vfs SIGKILL
  119. trap unmount_vfs SIGTERM
  120. xinteractive=1
  121. echo "PUPMODE='2'" > $curdir/$rootfs$prefix/etc/rc.d/PUPSTATE
  122. mkdir -p $curdir/$rootfs/proc;
  123. mkdir -p $curdir/$rootfs/sys
  124.  
  125. mount -o rbind /proc $curdir/$rootfs/proc
  126. mount -t sysfs none $curdir/$rootfs/sys
  127. if [ $xinteractive -eq 1 ]; then
  128.  echo "Removing block device files..."
  129.  #rm -rf $curdir/$rootfs/dev/*
  130.  #mount bind -t devtmpfs none $curdir/$rootfs/dev
  131.  mount -o rbind /dev $curdir/$rootfs/dev
  132.  cp -f /etc/resolv.conf $curdir/$rootfs/etc/resolv.conf
  133. fi
  134. copy_pet_specs(){
  135.   AWK_PRG="BEGIN{FS=\"|\"}
  136.  {
  137.    if ( \$2 == \"$app\" ) {
  138.     print
  139.    }
  140.  }"
  141.   file_spec_list=( "$rootfs_full/root/.packages/"*"-installed-packages" )
  142.   #cat "${file_spec_list[@]}" | awk -F'|' "$AWK_PRG" #'{print $2;}'
  143.   spec=$( cat "${file_spec_list[@]}" | awk "$AWK_PRG"  >> "$inst_pkg_specs_target" )
  144.   match_cnt=$( grep -cF "$spec" "$inst_pkg_specs_target" )
  145.   if [ $match_cnt -eq 0 ]; then
  146.     spec >> "$inst_pkg_specs_target"
  147.   fi
  148. }
  149. copy_built_in(){
  150.   set -x
  151.   app=$1
  152.   file_list="$curdir/$s_rootfs/root/.packages/builtin_files/$app"
  153.   if [ ! -f "$file_list" ] && [ $retry -eq 1 ]; then
  154.     file_list="$alt_s_rootfs/root/.packages/builtin_files/$app"
  155.     using_alt_list=1
  156.   else
  157.     using_alt_list=1=0
  158.   fi
  159.   if [ "${target:0:1}" = "/" ]; then
  160.     target_root="$target"      
  161.   else
  162.     target_root="$curdir/$target"    
  163.   fi  
  164.   target_file_list="$target_root/root/.packages/builtin_files/$app"  
  165.   echo "file_list=$file_list"
  166.   mkdir -p /tmp/trim_puppy
  167.   exec 10<> /tmp/trim_puppy/fd_10
  168.   subdir="/"
  169.   mkdir -p /tmp/trim_puppy/
  170.   rm "/tmp/trim_puppy/$app"
  171.   touch "/tmp/trim_puppy/$app"
  172.  
  173.   #mkdir -p
  174.   inst_pkg_specs_target="$target_root/root/.packages/"$arr_name"-installed-packages"
  175.   mkdir -p `dirname "$inst_pkg_specs_target"`
  176.   copy_pet_specs
  177.  
  178.   while IFS=$'\n' read  -r -d $'\n' -u10 line ; do
  179.        line=`echo "$line" | tr -d '[:space:]'`
  180.        if [ ! "${line:0:1}" = "/" ]; then
  181.            line=$subdir$line
  182.        fi
  183.        echo $line>>"/tmp/trim_puppy/$app"
  184.        if [ "${target:0:1}" = "/" ]; then
  185.          target_prefixed="$target$line"
  186.          #target_root="$target" #Defined above      
  187.        else
  188.          #target_root="$curdir/$target" #Defined above
  189.          target_prefixed="$curdir/$target$line"      
  190.        fi  
  191.  
  192.        if [ "${s_rootfs:0:1}" = "/" ]; then
  193.          source_prefixed="$s_rootfs$line"
  194.          cd "$s_rootfs" #this is necessary for the cpio command below    
  195.        else
  196.          source_prefixed="$curdir/$s_rootfs$line"    
  197.          cd "$curdir/$s_rootfs"  #this is necessary for the cpio command below
  198.        fi  
  199.  
  200.     if [ -d "$source_prefixed" ]; then
  201.        subdir="$line"
  202.        echo ".$line" | cpio -pd "$target_root"
  203.        subdir=${subdir%/}/
  204.     else
  205.        target_dir=`dirname $target_prefixed`
  206.        if [ ! -d "$target_dir" ]; then
  207.         source_dir=`dirname $line`
  208.         echo ".$source_dir" | cpio -pd "$target_root"      
  209.        fi
  210.        if [ ! -f "$source_prefixed" ] && [ $retry -eq 1 ]; then
  211.            source_prefixed="$alt_s_rootfs$line"
  212.            cp -a -u "$source_prefixed" "$target_prefixed"
  213.        elif [ ! "$arr_action" = "mv" ]; then
  214.          cp -a -u "$source_prefixed" "$target_prefixed"
  215.        else
  216.          mv -uf "$source_prefixed" "$target_prefixed"
  217.          if [ -f "$source_prefixed" ]; then
  218.            rm "$source_prefixed"
  219.          fi
  220.        fi
  221.     fi
  222.    
  223.   done 10< <( cat "$file_list" )
  224.   exec 10>&-
  225.   set +x
  226.   if [ ! "$arr_action" = "mv" ]; then
  227.     mkdir -p `dirname "$target_file_list"`
  228.     cp -a -u "$file_list" "$target_file_list"
  229.   else
  230.     mv -uf "$file_list" "$target_file_list"
  231.     sed -i "\%|${app}|%d" "$curdir/$s_rootfs/root/.packages/"*"-installed-packages"
  232.     if [ -f "$file_list" ]; then
  233.       rm "$file_list"
  234.     fi
  235.   fi
  236. }
  237.  
  238.  
  239. for arr_name in "${ALL_ArrNames[@]}"; do
  240.   eval "arr=( \"\${"$arr_name"[@]}\" )"
  241.   arr_action=`$"$arr_name"_action`
  242.   arr_action=${arr_action:-"$action"}
  243.   #Move is faster but perhaps copy is safer
  244.   arr_action=${arr_action:-cp}
  245.   set -x
  246.   eval 'target="$'$arr_name'_target"'
  247.   target=${target:-"$curdir/$arr_name"}
  248.   set +x
  249.   if [ "${s_rootfs:0:1}" = "/" ]; then
  250.     s_rootfs_prefixed="$s_rootfs"    
  251.   else
  252.     s_rootfs_prefixed="$curdir/$s_rootfs"    
  253.   fi  
  254.   for app in "${arr[@]}"; do
  255.       echo "arr_action=$arr_action"
  256.       case "$arr_action" in
  257.       cp|mv|retry)  
  258.           echo "app=$app"
  259.           copy_built_in "$app"        
  260.           ;;
  261.       #mv)
  262.       #    move_built_in()
  263.       #;;
  264.       #pet) #We might also want to convert to other package formats
  265.       #    mk_pet_fm_built_in()
  266.       #;;
  267.       esac
  268.       if [ -f "/tmp/trim_puppy/$app" ] && [ $chroot_remove_builtin -eq 1 ]; then
  269.         file_list="$curdir/$s_rootfs/root/.packages/builtin_files/$app"
  270.         rm "$file_list"
  271.         cp "/tmp/trim_puppy/$app" "$file_list"
  272.       fi
  273.       #chroot "$s_rootfs_prefixed" remove_builtin "$app"
  274.   done
  275. done
  276.  
  277.  
  278.  
  279. for pkg_dir in "${Post_Remove_Install_Fm_Dir[@]}"; do
  280.   if [ "${pkg_dir:0:1}" = "." ]; then
  281.     pkg_dir="$curdir/${pkg_dir:1:}"
  282.   elif [ ! "${pkg_dir:0:1}" = "/" ]; then
  283.         rel_path=${pkg_dir%/*}
  284.         pkg_name=`basename pkg_dir`
  285.         if [ -z "$rel_path" ]; then
  286.           for pkg_arch in "${arches[@]}" "."; do
  287.             for pkg_distro in "${post_rm_pkg_dirs[@]}" "."
  288.               rel_path="$pkg_arch/$distro"
  289.               a_pkg_dir="$curdir/post_patch/$pkg_arch/$pkg_distro/$pkg_name"
  290.               if [ -d "a_pkg_dir" ] || [ -d `readlink "$a_pkg_dir"` ]; then
  291.                  pkg_dir="$a_pkg_dir"
  292.                  break 2;
  293.               fi
  294.                  
  295.             done
  296.           done
  297.         else
  298.           pkg_dir="$curdir/post_patch/$arch/pupngo/$pkg_dir/$pkg_dir"
  299.         fi
  300.   fi
  301.   inst_builtin_fm_dir "$pkg_dir"
  302. done
  303. if [ chroot_remove_builtin = 1 ];then
  304.   chroot "$s_rootfs_prefixed" remove_builtin "${ALL[@]}"
  305. fi
  306. if [ "$(mount | grep "$curdir/$rootfs/dev")" != "" ]; then
  307.  umount -l $curdir/$rootfs/dev
  308. fi 
  309.  
  310. if [ "$(mount | grep "$curdir/$rootfs/sys")" != "" ]; then
  311.  umount -l $curdir/$rootfs/sys
  312. fi 
  313. if [ "$(mount | grep "$curdir$rootfs/proc")" != "" ]; then
  314.  umount -l $curdir/$rootfs/proc
  315. fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement